Skip to content

Commit 9c4b38c

Browse files
committed
Generic/ForLoopShouldBeWhileLoop: improve test coverage
This commit adds the following groups of tests: - A few tests that do not trigger the sniff but should help to protect against regressions in the future. Including tests using the for loop alternative syntax. - A test to exercise code in the sniff to bail early if the `for` keyword is found without a opening parenthesis. This part was not covered before.
1 parent e50784d commit 9c4b38c

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/Standards/Generic/Tests/CodeAnalysis/ForLoopShouldBeWhileLoopUnitTest.1.inc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,28 @@ for (; $it->valid();) {
1010
for (;(($it1->valid() && $foo) || (!$it2->value && ($bar || false)));/*Could be ignored*/) {
1111
$it1->next();
1212
$it2->next();
13-
}
13+
}
14+
15+
for ($i = 0, $j = 10; $i < $j; $i++, $j--) {
16+
echo "i: $i, j: $j\n";
17+
}
18+
19+
for (;;) {
20+
if ($i >= 10) {
21+
break;
22+
}
23+
echo $i++;
24+
}
25+
26+
for ($i = 0; $i < 10; $i++): ?>
27+
<p><?php echo $i; ?></p>
28+
<?php endfor;
29+
30+
for ($i = 0, $len = count($array); $i < $len; $i++):
31+
echo $array[$i];
32+
endfor;
33+
34+
for (; $i < 10;):
35+
echo $i;
36+
$i++;
37+
endfor;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
// Intentional parse error. Testing that the sniff is *not* triggered in this case.
4+
for

src/Standards/Generic/Tests/CodeAnalysis/ForLoopShouldBeWhileLoopUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function getWarningList($testFile='')
5252
return [
5353
6 => 1,
5454
10 => 1,
55+
34 => 1,
5556
];
5657
default:
5758
return [];

0 commit comments

Comments
 (0)