Skip to content

Commit b3a6b32

Browse files
authored
Merge pull request #385 from rodrigoprimo/test-coverage-jumbled-incrementer
Generic/JumbledIncrementer: fix non-problematic bug and improve test coverage
2 parents 2d4783c + fd7b175 commit b3a6b32

File tree

7 files changed

+133
-32
lines changed

7 files changed

+133
-32
lines changed

src/Standards/Generic/Sniffs/CodeAnalysis/JumbledIncrementerSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function process(File $phpcsFile, $stackPtr)
108108
protected function findIncrementers(array $tokens, array $token)
109109
{
110110
// Skip invalid statement.
111-
if (isset($token['parenthesis_opener']) === false) {
111+
if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
112112
return [];
113113
}
114114

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
for ($same = 0; $same < 20; $same++) {
4+
for ($j = 0; $j < 5; $same += 2) {
5+
for ($k = 0; $k > 3; $same++) {
6+
7+
}
8+
}
9+
}
10+
11+
for ($i = 0; $i < 20; $i++) {
12+
for ($j = 0; $j < 5; $j += 2) {
13+
for ($k = 0; $k > 3; $k++) {
14+
15+
}
16+
}
17+
}
18+
19+
for ($i = 0; $i < 20; $i++) {
20+
for ($same = 0; $same < 5; $same += 2) {
21+
for ($k = 0; $k > 3; $same++) {
22+
23+
}
24+
}
25+
}
26+
27+
for (; $i < 10; $i++) {
28+
for ($j = 0;; $j++) {
29+
if ($j > 5) {
30+
break;
31+
}
32+
for (;; $k++) {
33+
if ($k > 5) {
34+
break;
35+
}
36+
}
37+
}
38+
}
39+
40+
for (; $same < 10; $same++) {
41+
for ($j = 0;; $same++) {
42+
if ($j > 5) {
43+
break;
44+
}
45+
for (;; $same++) {
46+
if ($k > 5) {
47+
break;
48+
}
49+
}
50+
}
51+
}
52+
53+
for ($i = 0; $i < 20; $i++) :
54+
for ($j = 0; $j < 5; $j += 2) :
55+
endfor;
56+
endfor;
57+
58+
for ($same = 0; $same < 20; $same++) :
59+
for ($j = 0; $j < 5; $same += 2) :
60+
endfor;
61+
endfor;
62+
63+
// Sniff bails early when there is no incrementor in the third expression of the outer for loop.
64+
for ($same = 0; $same < 10;) {
65+
++$same;
66+
for ($j = 0; $j < 5; $same++) {}
67+
}
68+
69+
for ($i = 1, $same = 0; $i <= 10; $i++, $same++) {
70+
for ($same = 0, $k = 0; $k < 5; $same++, $k++) {}
71+
}
72+
73+
for ($i = 20; $i > 0; $i--) {
74+
for ($j = 5; $j > 0; $j -= 2) {
75+
for ($k = 3; $k > 0; $k--) {}
76+
}
77+
}
78+
79+
for ($same = 20; $same > 0; $same--) {
80+
for ($j = 5; $j > 0; $same -= 2) {
81+
for ($k = 3; $k > 0; $same--) {}
82+
}
83+
}
84+
85+
for ($i = 0; $i < 20; $i++);
86+
87+
for ($same = 0; $same < 20; $same++) {
88+
for ($j = 0; $j < 20; $same++);
89+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Intentional parse error (inner for loop missing closing parenthesis).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
for ($i = 0; $i < 20; $i++) {
7+
for ($i = 0; $i < 20; $i++
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Intentional parse error (missing for conditions).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
for
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Intentional parse error (inner for loop missing opening parenthesis).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
for ($i = 0; $i < 20; $i++) {
7+
for
8+
}

src/Standards/Generic/Tests/CodeAnalysis/JumbledIncrementerUnitTest.inc

Lines changed: 0 additions & 25 deletions
This file was deleted.

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,30 @@ public function getErrorList()
4141
* The key of the array should represent the line number and the value
4242
* should represent the number of warnings that should occur on that line.
4343
*
44+
* @param string $testFile The name of the file being tested.
45+
*
4446
* @return array<int, int>
4547
*/
46-
public function getWarningList()
48+
public function getWarningList($testFile='')
4749
{
48-
return [
49-
3 => 2,
50-
4 => 1,
51-
20 => 1,
52-
];
50+
switch ($testFile) {
51+
case 'JumbledIncrementerUnitTest.1.inc':
52+
return [
53+
3 => 2,
54+
4 => 1,
55+
20 => 1,
56+
40 => 2,
57+
41 => 1,
58+
58 => 1,
59+
69 => 1,
60+
79 => 2,
61+
80 => 1,
62+
87 => 1,
63+
];
64+
65+
default:
66+
return [];
67+
}
5368

5469
}//end getWarningList()
5570

0 commit comments

Comments
 (0)