Skip to content

Commit

Permalink
Generic/JumbledIncrementer: improve test coverage
Browse files Browse the repository at this point in the history
This commit adds tests for:
- for alternative syntax
- for loops missing one or more condition parts
- sniff bails early when it encounters a parse error (missing for body
  and inner for loop missing opening parenthesis
- for loops with more than one incrementer
- for loops with decrementers
  • Loading branch information
rodrigoprimo authored and jrfnl committed Apr 10, 2024
1 parent 6dc74f2 commit fd7b175
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,68 @@ for ($i = 0; $i < 20; $i++) {

}
}
}
}

for (; $i < 10; $i++) {
for ($j = 0;; $j++) {
if ($j > 5) {
break;
}
for (;; $k++) {
if ($k > 5) {
break;
}
}
}
}

for (; $same < 10; $same++) {
for ($j = 0;; $same++) {
if ($j > 5) {
break;
}
for (;; $same++) {
if ($k > 5) {
break;
}
}
}
}

for ($i = 0; $i < 20; $i++) :
for ($j = 0; $j < 5; $j += 2) :
endfor;
endfor;

for ($same = 0; $same < 20; $same++) :
for ($j = 0; $j < 5; $same += 2) :
endfor;
endfor;

// Sniff bails early when there is no incrementor in the third expression of the outer for loop.
for ($same = 0; $same < 10;) {
++$same;
for ($j = 0; $j < 5; $same++) {}
}

for ($i = 1, $same = 0; $i <= 10; $i++, $same++) {
for ($same = 0, $k = 0; $k < 5; $same++, $k++) {}
}

for ($i = 20; $i > 0; $i--) {
for ($j = 5; $j > 0; $j -= 2) {
for ($k = 3; $k > 0; $k--) {}
}
}

for ($same = 20; $same > 0; $same--) {
for ($j = 5; $j > 0; $same -= 2) {
for ($k = 3; $k > 0; $same--) {}
}
}

for ($i = 0; $i < 20; $i++);

for ($same = 0; $same < 20; $same++) {
for ($j = 0; $j < 20; $same++);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Intentional parse error (missing for conditions).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.
for
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// Intentional parse error (inner for loop missing opening parenthesis).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.
for ($i = 0; $i < 20; $i++) {
for
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public function getWarningList($testFile='')
3 => 2,
4 => 1,
20 => 1,
40 => 2,
41 => 1,
58 => 1,
69 => 1,
79 => 2,
80 => 1,
87 => 1,
];

default:
Expand Down

0 comments on commit fd7b175

Please sign in to comment.