Skip to content

Commit 5207a3e

Browse files
authored
Merge pull request #226 from rodrigoprimo/test-coverage-for-loop-should-be-while-loop
Generic/ForLoopShouldBeWhileLoop: improve sniff code coverage + fix potential PHP 8.3 deprecation notice
2 parents d6d3ae9 + 1ebc09e commit 5207a3e

File tree

6 files changed

+61
-19
lines changed

6 files changed

+61
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function process(File $phpcsFile, $stackPtr)
5757
$token = $tokens[$stackPtr];
5858

5959
// Skip invalid statement.
60-
if (isset($token['parenthesis_opener']) === false) {
60+
if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
6161
return;
6262
}
6363

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
for ($i = 0; $i < 10; $i++) {
3+
// Everything is fine
4+
}
5+
6+
for (; $it->valid();) {
7+
$it->next();
8+
}
9+
10+
for (;(($it1->valid() && $foo) || (!$it2->value && ($bar || false)));/*Could be ignored*/) {
11+
$it1->next();
12+
$it2->next();
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
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Issue PHPCSStandards/PHP_CodeSniffer#226
4+
// Intentional parse error (missing close parenthesis). Testing that the sniff is *not* triggered
5+
// in this case and that no PHP 8.3+ deprecation notice is thrown.
6+
for ($i = 0; $i < 10; $i++

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

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

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,22 @@ 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 test file being tested.
45+
*
4446
* @return array<int, int>
4547
*/
46-
public function getWarningList()
48+
public function getWarningList($testFile='')
4749
{
48-
return [
49-
6 => 1,
50-
10 => 1,
51-
];
50+
switch ($testFile) {
51+
case 'ForLoopShouldBeWhileLoopUnitTest.1.inc':
52+
return [
53+
6 => 1,
54+
10 => 1,
55+
34 => 1,
56+
];
57+
default:
58+
return [];
59+
}
5260

5361
}//end getWarningList()
5462

0 commit comments

Comments
 (0)