Skip to content

Commit

Permalink
Merge pull request #580 from PHPCSStandards/feature/squiz-operatorbra…
Browse files Browse the repository at this point in the history
…cket-fix-php-notice-live-coding

Squiz/OperatorBracket: prevent PHP notices during live coding
  • Loading branch information
jrfnl authored Aug 1, 2024
2 parents 2014d6f + 7a4c787 commit d49ea71
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/Standards/Squiz/Sniffs/Formatting/OperatorBracketSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,23 @@ public function addMissingBracketsError($phpcsFile, $stackPtr)
}

if ($tokens[$after]['code'] === T_OPEN_PARENTHESIS) {
if (isset($tokens[$after]['parenthesis_closer']) === false) {
// Live coding/parse error. Ignore.
return;
}

$after = $tokens[$after]['parenthesis_closer'];
continue;
}

if ($tokens[$after]['code'] === T_OPEN_SQUARE_BRACKET) {
$after = $tokens[$after]['bracket_closer'];
continue;
}
if (($tokens[$after]['code'] === T_OPEN_SQUARE_BRACKET
|| $tokens[$after]['code'] === T_OPEN_SHORT_ARRAY)
) {
if (isset($tokens[$after]['bracket_closer']) === false) {
// Live coding/parse error. Ignore.
return;
}

if ($tokens[$after]['code'] === T_OPEN_SHORT_ARRAY) {
$after = $tokens[$after]['bracket_closer'];
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error. This has to be the last (and only) test in the file.
// Live coding test. The sniff should stay silent.
class ParseErrors {
const A|(B PARSE_ERROR = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

// Intentional parse error. This has to be the last (and only) test in the file.
// Live coding test. The sniff should stay silent.
$a = [10, 20] + [
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class OperatorBracketUnitTest extends AbstractSniffUnitTest
public function getErrorList($testFile='')
{
switch ($testFile) {
case 'OperatorBracketUnitTest.inc':
case 'OperatorBracketUnitTest.1.inc':
return [
3 => 1,
6 => 1,
Expand Down

0 comments on commit d49ea71

Please sign in to comment.