Skip to content

Commit 1fbd83d

Browse files
committed
Squiz/ForLoopDeclaration: bug fix - prevent sniff erroring out
... on an _"Undefined array key "parenthesis_closer" "_ notice during live coding. Includes unit test.
1 parent 1a48781 commit 1fbd83d

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function process(File $phpcsFile, $stackPtr)
7676
$tokens = $phpcsFile->getTokens();
7777

7878
$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
79-
if ($openingBracket === false) {
79+
if ($openingBracket === false || isset($tokens[$openingBracket]['parenthesis_closer']) === false) {
8080
$error = 'Possible parse error: no opening parenthesis for FOR keyword';
8181
$phpcsFile->addWarning($error, $stackPtr, 'NoOpenBracket');
8282
return;
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

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public function getWarningList($testFile='')
122122
{
123123
switch ($testFile) {
124124
case 'ForLoopDeclarationUnitTest.2.inc':
125+
case 'ForLoopDeclarationUnitTest.3.inc':
125126
return [6 => 1];
126127

127128
case 'ForLoopDeclarationUnitTest.js':

0 commit comments

Comments
 (0)