Skip to content

Commit 6e61c85

Browse files
rodrigoprimojrfnl
authored andcommitted
Generic/ForLoopWithTestFunctionCall: improve test coverage
This commit adds more test cases related to the Generic/ForLoopWithTestFunctionCall sniff including test cases for the alternative for syntax, more types of function calls in the test part of the for loop and a test case to check defensive code when the for loop doesn't have a opening parenthesis.
1 parent e3dcefe commit 6e61c85

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

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

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,84 @@ for ($i = 0, $c = sizeof($a); $i < $c; ++$i) {
1212
$it = new ArrayIterator($a);
1313
for ($it->rewind(); $it->valid(); $it->next()) {
1414
echo $it->current();
15-
}
15+
}
16+
17+
for ($i = 0; MyClass::staticMethod($value); $i++) {
18+
echo $i;
19+
}
20+
21+
for ($i = 0; $countFunction($value); $i++) {
22+
echo $i;
23+
}
24+
25+
$a = array(1, 2, 3, 4);
26+
for ($i = 0; $i < count($a); $i++):
27+
$a[$i] *= $i;
28+
endfor;
29+
30+
for ($i = 0, $c = sizeof($a); $i < $c; ++$i):
31+
$a[$i] *= $i;
32+
endfor;
33+
34+
$it = new ArrayIterator($a);
35+
for ($it->rewind(); $it->valid(); $it->next()):
36+
echo $it->current();
37+
endfor;
38+
39+
for ($i = 0; MyClass::staticMethod($value); $i++) :
40+
echo $i;
41+
endfor;
42+
43+
for ($i = 0; $countFunction($value); $i++):
44+
echo $i;
45+
endfor;
46+
47+
for ($i = 0; (new MyClass)->method(); $i++) {
48+
}
49+
50+
for (; $i < 10; ++$i) {}
51+
52+
for (; count($a); ++$i) {}
53+
54+
for ($i = 0;; ++$i) {}
55+
56+
for ($i = 0; $i < 10;) {}
57+
58+
for ($i = 0; count($a);) {}
59+
60+
for (;; $i++) {}
61+
62+
for ($i = 0;;) {}
63+
64+
for (;;) {}
65+
66+
for ($i = 0; (new MyClass)->method(); $i++):
67+
endfor;
68+
69+
for (; $i < 10; ++$i) :
70+
endfor;
71+
72+
for (; count($a); ++$i) :
73+
endfor;
74+
75+
for ($i = 0;; ++$i) :
76+
endfor;
77+
78+
for ($i = 0; $i < 10;) :
79+
endfor;
80+
81+
for ($i = 0; count($a);) :
82+
endfor;
83+
84+
for (;; $i++) :
85+
endfor;
86+
87+
for ($i = 0;;) :
88+
endfor;
89+
90+
for (;;) :
91+
endfor;
92+
93+
for ($i = 0; $i < 10; $i = increment($i)) {}
94+
95+
for ($i = initialValue(); $i < 10; $i = increment($i)) {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// Intentional parse error (missing open parenthesis). Testing that the sniff is *not* triggered
4+
// in this case.
5+
for

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,22 @@ public function getWarningList($testFile='')
5252
return [
5353
4 => 1,
5454
13 => 1,
55+
17 => 1,
56+
21 => 1,
57+
26 => 1,
58+
35 => 1,
59+
39 => 1,
60+
43 => 1,
61+
47 => 1,
62+
52 => 1,
63+
58 => 1,
64+
66 => 1,
65+
72 => 1,
66+
81 => 1,
5567
];
5668
default:
5769
return [];
58-
}
70+
}//end switch
5971

6072
}//end getWarningList()
6173

0 commit comments

Comments
 (0)