Skip to content

Commit

Permalink
PHP/UselessParentheses: prevent error for parse error
Browse files Browse the repository at this point in the history
```
An error occurred during processing; checking has been aborted. The error message was: Undefined array key "parenthesis_closer" in path\to\SlevomatCodingStandard\Sniffs\PHP\UselessParenthesesSniff.php on line 178```
  • Loading branch information
jrfnl authored and kukulich committed Oct 5, 2023
1 parent f150966 commit 2d45e24
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public function process(File $phpcsFile, $parenthesisOpenerPointer): void
return;
}

if (!array_key_exists('parenthesis_closer', $tokens[$parenthesisOpenerPointer])) {
return;
}

/** @var int $pointerBeforeParenthesisOpener */
$pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1);
if (in_array($tokens[$pointerBeforeParenthesisOpener]['code'], array_merge(
Expand Down Expand Up @@ -386,7 +390,7 @@ private function checkParenthesesAroundVariableOrFunctionCall(File $phpcsFile, i
} while (true);
} else {
$nextPointer = TokenHelper::findNext($phpcsFile, T_OPEN_PARENTHESIS, $notBooleanNotOperatorPointer + 1);
if ($nextPointer === null) {
if ($nextPointer === null || !isset($tokens[$nextPointer]['parenthesis_closer'])) {
return;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/Sniffs/PHP/UselessParenthesesSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ public function testNoErrorsWithIgnoredComplexTernaryConditions(): void
self::assertNoSniffErrorInFile($report);
}

public function testNoErrorsLiveCoding(): void
{
$report = self::checkFile(__DIR__ . '/data/uselessParenthesesLiveCoding.php');
self::assertNoSniffErrorInFile($report);
}

}
5 changes: 5 additions & 0 deletions tests/Sniffs/PHP/data/uselessParenthesesLiveCoding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php // lint >= 99.0

// Live coding/parse error.
// This must be the last test in the file.
if ($a = 1
1 change: 0 additions & 1 deletion tests/Sniffs/PHP/data/uselessParenthesesNoErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,3 @@ function ($value) {
return true
? 100
: (int) ((100 <=> 50) * 100);

0 comments on commit 2d45e24

Please sign in to comment.