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

Squiz/ForLoopDeclaration: bug fix - prevent sniff erroring out #430

Merged
merged 3 commits into from
Apr 6, 2024
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 @@ -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