Skip to content

Commit 8d7896c

Browse files
authored
Modify S134(apex): Add compliant example and fix non-compliant (#5944)
* Add compliant solution for rule S134 in Apex, fix non-compliant example - duplicate declaration of j - illegal shadowing of i - switch on / when -> if to simplify compliant solution * Fix spacing, put compliant comments in the right place * Added missing spaces between ) and {
1 parent cdb0a85 commit 8d7896c

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

rules/S134/apex/rule.adoc

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,47 @@ The following example demonstrates the behavior of the rule with the default thr
1212

1313
[source,apex]
1414
----
15-
switch on i { // Compliant - depth = 1
16-
when 1 {
15+
if (i == 1) // Compliant - depth = 1
16+
/* ... */
17+
for (Integer j = 0; j < 10; j++) { // Compliant - depth = 2
1718
/* ... */
18-
for (Integer i = 0, j = 0; i < 10; i++) { // Compliant - depth = 2
19+
Integer k = j + 1;
20+
if (k == 3) { // Compliant - depth = 3, not exceeding the limit
1921
/* ... */
20-
Integer j = i + 1;
21-
if (j == 0){// Compliant - depth = 3, not exceeding the limit
22+
while (k < 10) { // Noncompliant - depth = 4
2223
/* ... */
23-
while (j < 10){ // Noncompliant - depth = 4
24+
if (k == 1) { // Depth = 5, exceeding the limit, but issues are only reported on depth = 4
2425
/* ... */
25-
if (j == 1){ // Depth = 5, exceeding the limit, but issues are only reported on depth = 4
26-
/* ... */
27-
}
2826
}
2927
}
3028
}
3129
}
3230
}
3331
----
3432

33+
==== Compliant solution
34+
35+
[source,apex]
36+
----
37+
if (i != 1) {
38+
return;
39+
}
40+
for (Integer j = 0; j < 10; j++) { // Compliant - depth = 1
41+
/* ... */
42+
Integer k = j + 1;
43+
if (k != 3) {
44+
continue;
45+
}
46+
/* ... */
47+
while (k < 10) { // Compliant - depth = 2
48+
/* ... */
49+
if (k == 1) { // Compliant - depth = 3
50+
/* ... */
51+
}
52+
}
53+
}
54+
----
55+
3556
include::../resources.adoc[]
3657

3758
ifdef::env-github,rspecator-view[]

0 commit comments

Comments
 (0)