Skip to content

Commit

Permalink
Merge pull request #430 from PHPCSStandards/feature/squiz-forloopdecl…
Browse files Browse the repository at this point in the history
…aration-prevent-php-notice

Squiz/ForLoopDeclaration: bug fix - prevent sniff erroring out
  • Loading branch information
jrfnl authored Apr 6, 2024
2 parents dbfe1c3 + 9634005 commit b131294
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public function process(File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();

$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
if ($openingBracket === false) {
$error = 'Possible parse error: no opening parenthesis for FOR keyword';
if ($openingBracket === false || isset($tokens[$openingBracket]['parenthesis_closer']) === false) {
$error = 'Possible parse error: no opening/closing parenthesis for FOR keyword';
$phpcsFile->addWarning($error, $stackPtr, 'NoOpenBracket');
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,3 @@ for (
// body here
}
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration ignoreNewlines false

// This test has to be the last one in the file! Intentional parse error check.
for
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,3 @@ for (
// body here
}
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration ignoreNewlines false

// This test has to be the last one in the file! Intentional parse error check.
for
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Parse error/live coding test (no parentheses).
// This test has to be the only test in the file!
// Testing the "NoOpenBracket" warning.
for
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Parse error/live coding test (no close parenthesis).
// This test has to be the only test in the file!
// Testing the "NoOpenBracket" warning.
for ($i = 10;
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class ForLoopDeclarationUnitTest extends AbstractSniffUnitTest
public function getErrorList($testFile='')
{
switch ($testFile) {
case 'ForLoopDeclarationUnitTest.inc':
case 'ForLoopDeclarationUnitTest.1.inc':
return [
8 => 2,
11 => 2,
Expand Down Expand Up @@ -121,8 +121,9 @@ public function getErrorList($testFile='')
public function getWarningList($testFile='')
{
switch ($testFile) {
case 'ForLoopDeclarationUnitTest.inc':
return [129 => 1];
case 'ForLoopDeclarationUnitTest.2.inc':
case 'ForLoopDeclarationUnitTest.3.inc':
return [6 => 1];

case 'ForLoopDeclarationUnitTest.js':
return [125 => 1];
Expand Down

0 comments on commit b131294

Please sign in to comment.