Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbstractPatternSniff: prevent PHP notice #579

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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