Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic/ForLoopShouldBeWhileLoop: improve sniff code coverage + fix potential PHP 8.3 deprecation notice #226

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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