Skip to content

Commit b2050fa

Browse files
committed
Added more guard code for syntax errors to various sniffs (ref #580)
1 parent 462d831 commit b2050fa

File tree

6 files changed

+34
-1
lines changed

6 files changed

+34
-1
lines changed

CodeSniffer/Standards/AbstractPatternSniff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,10 @@ protected function processPattern(
440440
$patternLen = count($pattern);
441441

442442
for ($i = $patternInfo['listen_pos']; $i < $patternLen; $i++) {
443+
if (isset($tokens[$stackPtr]) === false) {
444+
break;
445+
}
446+
443447
if ($pattern[$i]['type'] === 'token') {
444448
if ($pattern[$i]['token'] === T_WHITESPACE) {
445449
if ($this->ignoreComments === true) {
@@ -452,7 +456,9 @@ protected function processPattern(
452456
// If the next token is a comment, the we need to skip the
453457
// current token as we should allow a space before a
454458
// comment for readability.
455-
if (isset(PHP_CodeSniffer_Tokens::$commentTokens[$tokens[($stackPtr + 1)]['code']]) === true) {
459+
if (isset($tokens[($stackPtr + 1)]) === true
460+
&& isset(PHP_CodeSniffer_Tokens::$commentTokens[$tokens[($stackPtr + 1)]['code']]) === true
461+
) {
456462
continue;
457463
}
458464
}

CodeSniffer/Standards/PEAR/Sniffs/ControlStructures/MultiLineConditionSniff.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7474
{
7575
$tokens = $phpcsFile->getTokens();
7676

77+
if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) {
78+
return;
79+
}
80+
7781
$openBracket = $tokens[$stackPtr]['parenthesis_opener'];
7882
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
7983
$spaceAfterOpen = 0;

CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7272
{
7373
$tokens = $phpcsFile->getTokens();
7474

75+
if (isset($tokens[($stackPtr + 1)]) === false) {
76+
return;
77+
}
78+
7579
// Single space after the keyword.
7680
$found = 1;
7781
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
@@ -202,6 +206,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
202206

203207
// Only want to check multi-keyword structures from here on.
204208
if ($tokens[$stackPtr]['code'] === T_DO) {
209+
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
210+
return;
211+
}
212+
205213
$closer = $tokens[$stackPtr]['scope_closer'];
206214
} else if ($tokens[$stackPtr]['code'] === T_ELSE
207215
|| $tokens[$stackPtr]['code'] === T_ELSEIF

CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/ForEachLoopDeclarationSniff.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7474
$tokens = $phpcsFile->getTokens();
7575

7676
$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
77+
if ($openingBracket === false) {
78+
$error = 'Possible parse error: FOREACH has no opening parenthesis';
79+
$phpcsFile->addWarning($error, $stackPtr, 'MissingParenthesis');
80+
return;
81+
}
82+
7783
$closingBracket = $tokens[$openingBracket]['parenthesis_closer'];
7884

7985
if ($this->requiredSpacesAfterOpen === 0 && $tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {

CodeSniffer/Standards/Squiz/Sniffs/Operators/ComparisonOperatorUsageSniff.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,17 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
152152

153153
$start = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($i + 1), null, true);
154154
} else {
155+
if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) {
156+
return;
157+
}
158+
155159
$start = $tokens[$end]['parenthesis_opener'];
156160
}//end if
157161
} else {
162+
if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) {
163+
return;
164+
}
165+
158166
$start = $tokens[$stackPtr]['parenthesis_opener'];
159167
$end = $tokens[$stackPtr]['parenthesis_closer'];
160168
}//end if

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3030
-- Thanks to Klaus Purer for the patch
3131
- Defined tokens for lower PHP versions are now phpcs-specific string instead of ints
3232
-- Stops conflict with other projects, like PHP_CodeCoverage
33+
- Added more guard code for syntax errors to various sniffs
3334
- Fixed bug #584 : Squiz.Arrays.ArrayDeclaration sniff gives incorrect NoComma error for multiline string values
3435
</notes>
3536
<contents>

0 commit comments

Comments
 (0)