Skip to content

Commit

Permalink
Fixed bug #1381 : Tokenizer: derefencing incorrectly identified as sh…
Browse files Browse the repository at this point in the history
…ort array
  • Loading branch information
gsherwood committed Mar 9, 2017
1 parent bfbf317 commit 3e651f1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,7 @@ function test() : array
$fields = [
'id' => ['type' => 'INT'],
'value' => ['type' => 'VARCHAR']];

echo [1][0];
echo 'PHP'[0];
echo [1][0, 1, 2]; // not valid code, but should not be picked up here
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,7 @@ $fields = [
'id' => ['type' => 'INT'],
'value' => ['type' => 'VARCHAR'],
];

echo [1][0];
echo 'PHP'[0];
echo [1][0, 1, 2]; // not valid code, but should not be picked up here
20 changes: 7 additions & 13 deletions CodeSniffer/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1277,11 +1277,12 @@ public function processAdditional(&$tokens, $eolChar)
// it is the start of an array being defined using the short syntax.
$isShortArray = false;
$allowed = array(
T_CLOSE_SQUARE_BRACKET => T_CLOSE_SQUARE_BRACKET,
T_CLOSE_PARENTHESIS => T_CLOSE_PARENTHESIS,
T_VARIABLE => T_VARIABLE,
T_OBJECT_OPERATOR => T_OBJECT_OPERATOR,
T_STRING => T_STRING,
T_CLOSE_SQUARE_BRACKET => T_CLOSE_SQUARE_BRACKET,
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
T_CLOSE_PARENTHESIS => T_CLOSE_PARENTHESIS,
T_VARIABLE => T_VARIABLE,
T_OBJECT_OPERATOR => T_OBJECT_OPERATOR,
T_CONSTANT_ENCAPSED_STRING => T_CONSTANT_ENCAPSED_STRING,
);

for ($x = ($i - 1); $x > 0; $x--) {
Expand All @@ -1293,21 +1294,14 @@ public function processAdditional(&$tokens, $eolChar)
break;
}

if (isset($tokens[$x]['bracket_opener']) === true
&& $x > $tokens[$x]['bracket_opener']
) {
$x = $tokens[$x]['bracket_opener'];
continue;
}

if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) {
if (isset($allowed[$tokens[$x]['code']]) === false) {
$isShortArray = true;
}

break;
}
}//end for
}

if ($isShortArray === true) {
$tokens[$i]['code'] = T_OPEN_SHORT_ARRAY;
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #1364 : Yield From values are not recognised as returned values in Squiz FunctionComment sniff
- Fixed bug #1373 : Error in tab expansion results in white-space of incorrect size
-- Thanks to Mark Clements for the patch
- Fixed bug #1381 : Tokenizer: derefencing incorrectly identified as short array
</notes>
<contents>
<dir name="/">
Expand Down

0 comments on commit 3e651f1

Please sign in to comment.