Skip to content

Commit

Permalink
Squiz/ForLoopDeclaration: bug fix - prevent sniff erroring out
Browse files Browse the repository at this point in the history
... on an _"Undefined array key "parenthesis_closer" "_ notice during live coding.

Includes unit test.
  • Loading branch information
jrfnl committed Mar 31, 2024
1 parent 1a48781 commit 1fbd83d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function process(File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();

$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
if ($openingBracket === false) {
if ($openingBracket === false || isset($tokens[$openingBracket]['parenthesis_closer']) === false) {
$error = 'Possible parse error: no opening parenthesis for FOR keyword';
$phpcsFile->addWarning($error, $stackPtr, 'NoOpenBracket');
return;
Expand Down
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 @@ -122,6 +122,7 @@ public function getWarningList($testFile='')
{
switch ($testFile) {
case 'ForLoopDeclarationUnitTest.2.inc':
case 'ForLoopDeclarationUnitTest.3.inc':
return [6 => 1];

case 'ForLoopDeclarationUnitTest.js':
Expand Down

0 comments on commit 1fbd83d

Please sign in to comment.