Skip to content

Commit

Permalink
Merge pull request #579 from PHPCSStandards/feature/abstractpatternsn…
Browse files Browse the repository at this point in the history
…iff-fix-bug

AbstractPatternSniff: prevent PHP notice
  • Loading branch information
jrfnl authored Aug 1, 2024
2 parents 0855bf2 + 49b8494 commit 2014d6f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/Sniffs/AbstractPatternSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,11 @@ protected function processPattern($patternInfo, File $phpcsFile, $stackPtr)
$lastAddedStackPtr = null;
$patternLen = count($pattern);

if (($stackPtr + $patternLen - $patternInfo['listen_pos']) > $phpcsFile->numTokens) {
// Pattern can never match as there are not enough tokens left in the file.
return false;
}

for ($i = $patternInfo['listen_pos']; $i < $patternLen; $i++) {
if (isset($tokens[$stackPtr]) === false) {
break;
Expand Down
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.
function
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.
function // Comment.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ final class FunctionDeclarationUnitTest extends AbstractSniffUnitTest
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [
55 => 1,
68 => 1,
];
switch ($testFile) {
case 'FunctionDeclarationUnitTest.1.inc':
return [
55 => 1,
68 => 1,
];

default:
return [];
}//end switch

}//end getErrorList()

Expand Down

0 comments on commit 2014d6f

Please sign in to comment.