Skip to content

Commit b131294

Browse files
authored
Merge pull request #430 from PHPCSStandards/feature/squiz-forloopdeclaration-prevent-php-notice
Squiz/ForLoopDeclaration: bug fix - prevent sniff erroring out
2 parents dbfe1c3 + 9634005 commit b131294

File tree

6 files changed

+18
-11
lines changed

6 files changed

+18
-11
lines changed

src/Standards/Squiz/Sniffs/ControlStructures/ForLoopDeclarationSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public function process(File $phpcsFile, $stackPtr)
7676
$tokens = $phpcsFile->getTokens();
7777

7878
$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
79-
if ($openingBracket === false) {
80-
$error = 'Possible parse error: no opening parenthesis for FOR keyword';
79+
if ($openingBracket === false || isset($tokens[$openingBracket]['parenthesis_closer']) === false) {
80+
$error = 'Possible parse error: no opening/closing parenthesis for FOR keyword';
8181
$phpcsFile->addWarning($error, $stackPtr, 'NoOpenBracket');
8282
return;
8383
}

src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.inc renamed to src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.1.inc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,3 @@ for (
124124
// body here
125125
}
126126
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration ignoreNewlines false
127-
128-
// This test has to be the last one in the file! Intentional parse error check.
129-
for

src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.inc.fixed renamed to src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.1.inc.fixed

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,3 @@ for (
9090
// body here
9191
}
9292
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration ignoreNewlines false
93-
94-
// This test has to be the last one in the file! Intentional parse error check.
95-
for
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Parse error/live coding test (no parentheses).
4+
// This test has to be the only test in the file!
5+
// Testing the "NoOpenBracket" warning.
6+
for
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Parse error/live coding test (no close parenthesis).
4+
// This test has to be the only test in the file!
5+
// Testing the "NoOpenBracket" warning.
6+
for ($i = 10;

src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class ForLoopDeclarationUnitTest extends AbstractSniffUnitTest
3333
public function getErrorList($testFile='')
3434
{
3535
switch ($testFile) {
36-
case 'ForLoopDeclarationUnitTest.inc':
36+
case 'ForLoopDeclarationUnitTest.1.inc':
3737
return [
3838
8 => 2,
3939
11 => 2,
@@ -121,8 +121,9 @@ public function getErrorList($testFile='')
121121
public function getWarningList($testFile='')
122122
{
123123
switch ($testFile) {
124-
case 'ForLoopDeclarationUnitTest.inc':
125-
return [129 => 1];
124+
case 'ForLoopDeclarationUnitTest.2.inc':
125+
case 'ForLoopDeclarationUnitTest.3.inc':
126+
return [6 => 1];
126127

127128
case 'ForLoopDeclarationUnitTest.js':
128129
return [125 => 1];

0 commit comments

Comments
 (0)