Skip to content

Commit

Permalink
Fix possible index error in NonExecutableCodeSniff
Browse files Browse the repository at this point in the history
I was running into this while working on another sniff. One trivial example I found to demonstrate the bug is `<?php return array_map(`. This can happen when PHPCS runs on a file I'm currently typing in, but the file is not complete yet.

I believe it's safe to assume the 'parenthesis_closer' array key only exists on T_OPEN_PARENTHESIS tokens, and the 'bracket_closer' key only on T_OPEN_CURLY_BRACKET tokens. I had a quick look at the tokenizer and I believe this is indeed the case. But I might be wrong.

Sorry for not providing a test. I would love to, but I'm not familiar enough with the test setup here.
  • Loading branch information
thiemowmde authored Jun 17, 2020
1 parent 75ff420 commit 3b9282a
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ public function process(File $phpcsFile, $stackPtr)
break;
}

if ($tokens[$start]['code'] === T_OPEN_PARENTHESIS) {
if (isset($tokens[$start]['parenthesis_closer']) === true) {
$start = $tokens[$start]['parenthesis_closer'];
continue;
}

if ($tokens[$start]['code'] === T_OPEN_CURLY_BRACKET) {
if (isset($tokens[$start]['bracket_closer']) === true) {
$start = $tokens[$start]['bracket_closer'];
continue;
}
Expand Down

0 comments on commit 3b9282a

Please sign in to comment.