diff --git a/src/Sniffs/AbstractPatternSniff.php b/src/Sniffs/AbstractPatternSniff.php index e895365688..d9528dccf8 100644 --- a/src/Sniffs/AbstractPatternSniff.php +++ b/src/Sniffs/AbstractPatternSniff.php @@ -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; diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationUnitTest.2.inc b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationUnitTest.2.inc new file mode 100644 index 0000000000..51a55e92ad --- /dev/null +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationUnitTest.2.inc @@ -0,0 +1,5 @@ +