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 committed Mar 12, 2024
1 parent 5e59b86 commit 257630b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,62 @@ 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 (; $i < 10; $i++) {
for ($j = 0;; $i++) {
if ($j > 5) {
break;
}
for (;; $i++) {
if ($k > 5) {
break;
}
}
}
}

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

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

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

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

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

for ($i = 20; $i > 0; $i--) {
for ($j = 5; $j > 0; $i -= 2) {
for ($k = 3; $k > 0; $i--) {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Intentional parse error (missing for body).
// 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,12 @@ public function getWarningList($testFile='')
3 => 2,
4 => 1,
20 => 1,
40 => 2,
41 => 1,
58 => 1,
69 => 1,
79 => 2,
80 => 1,
];

default:
Expand Down

0 comments on commit 257630b

Please sign in to comment.