Skip to content

Commit

Permalink
Merge pull request #226 from rodrigoprimo/test-coverage-for-loop-shou…
Browse files Browse the repository at this point in the history
…ld-be-while-loop

Generic/ForLoopShouldBeWhileLoop: improve sniff code coverage + fix potential PHP 8.3 deprecation notice
  • Loading branch information
jrfnl committed Jan 5, 2024
2 parents d6d3ae9 + 1ebc09e commit 5207a3e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function process(File $phpcsFile, $stackPtr)
$token = $tokens[$stackPtr];

// Skip invalid statement.
if (isset($token['parenthesis_opener']) === false) {
if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
for ($i = 0; $i < 10; $i++) {
// Everything is fine
}

for (; $it->valid();) {
$it->next();
}

for (;(($it1->valid() && $foo) || (!$it2->value && ($bar || false)));/*Could be ignored*/) {
$it1->next();
$it2->next();
}

for ($i = 0, $j = 10; $i < $j; $i++, $j--) {
echo "i: $i, j: $j\n";
}

for (;;) {
if ($i >= 10) {
break;
}
echo $i++;
}

for ($i = 0; $i < 10; $i++): ?>
<p><?php echo $i; ?></p>
<?php endfor;

for ($i = 0, $len = count($array); $i < $len; $i++):
echo $array[$i];
endfor;

for (; $i < 10;):
echo $i;
$i++;
endfor;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// Intentional parse error. Testing that the sniff is *not* triggered in this case.
for
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Issue PHPCSStandards/PHP_CodeSniffer#226
// Intentional parse error (missing close parenthesis). Testing that the sniff is *not* triggered
// in this case and that no PHP 8.3+ deprecation notice is thrown.
for ($i = 0; $i < 10; $i++

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,22 @@ public function getErrorList()
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @param string $testFile The name of the test file being tested.
*
* @return array<int, int>
*/
public function getWarningList()
public function getWarningList($testFile='')
{
return [
6 => 1,
10 => 1,
];
switch ($testFile) {
case 'ForLoopShouldBeWhileLoopUnitTest.1.inc':
return [
6 => 1,
10 => 1,
34 => 1,
];
default:
return [];
}

}//end getWarningList()

Expand Down

0 comments on commit 5207a3e

Please sign in to comment.