Skip to content

Commit

Permalink
Fixed bug #2865 : Double arrow tokenized as T_STRING when placed afte…
Browse files Browse the repository at this point in the history
…r function named "fn"
  • Loading branch information
gsherwood committed Mar 9, 2020
1 parent c6fe936 commit 1dbc333
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #2850 : Generic.PHP.LowerCaseKeyword complains __HALT_COMPILER is uppercase
- Fixed bug #2853 : Undefined variable error when using Info report
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #2865 : Double arrow tokenized as T_STRING when placed after function named "fn"
- Fixed bug #2868 : phpcs:ignore annotation doesnt work inside a docblock
- Fixed bug #2878 : PSR12.Files.FileHeader conflicts with Generic.Files.LineEndings
</notes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,11 @@ $array = array(
3 => '3',
);

$foo = array(
$this->fn => 'value',
$foo->fn => 'value',
);

// Intentional syntax error.
$a = array(
'a' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ $array = array(
3 => '3',
);

$foo = array(
$this->fn => 'value',
$foo->fn => 'value',
);

// Intentional syntax error.
$a = array(
'a' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ $array = [
3 => '3',
];

$foo = [
$this->fn => 'value',
$foo->fn => 'value',
];

// Intentional syntax error.
$a = [
'a' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@ $array = [
3 => '3',
];

$foo = [
$this->fn => 'value',
$foo->fn => 'value',
];

// Intentional syntax error.
$a = [
'a' =>
Expand Down
2 changes: 2 additions & 0 deletions src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function getErrorList($testFile='')
370 => 1,
383 => 1,
394 => 1,
429 => 1,
];
case 'ArrayDeclarationUnitTest.2.inc':
return [
Expand Down Expand Up @@ -188,6 +189,7 @@ public function getErrorList($testFile='')
358 => 1,
372 => 1,
383 => 1,
418 => 1,
];
default:
return [];
Expand Down
30 changes: 20 additions & 10 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,9 @@ protected function tokenize($string)
// detect the T_FN token more easily.
$tokens[$stackPtr][0] = T_FN;
$token[0] = T_FN;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t\t* token $stackPtr changed from T_STRING to T_FN".PHP_EOL;
}
}

/*
Expand All @@ -1241,18 +1244,25 @@ protected function tokenize($string)
|| $token[0] === T_FN)
&& $finalTokens[$lastNotEmptyToken]['code'] !== T_USE
) {
for ($x = ($stackPtr + 1); $x < $numTokens; $x++) {
if (is_array($tokens[$x]) === false
|| isset(Util\Tokens::$emptyTokens[$tokens[$x][0]]) === false
) {
// Non-empty content.
break;
if ($token[0] === T_FUNCTION) {
for ($x = ($stackPtr + 1); $x < $numTokens; $x++) {
if (is_array($tokens[$x]) === false
|| isset(Util\Tokens::$emptyTokens[$tokens[$x][0]]) === false
) {
// Non-empty content.
break;
}
}
}

if ($x < $numTokens && is_array($tokens[$x]) === true) {
$tokens[$x][0] = T_STRING;
}
if ($x < $numTokens && is_array($tokens[$x]) === true) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$oldType = Util\Tokens::tokenName($tokens[$x][0]);
echo "\t\t* token $x changed from $oldType to T_STRING".PHP_EOL;
}

$tokens[$x][0] = T_STRING;
}
}//end if

/*
This is a special condition for T_ARRAY tokens used for
Expand Down

0 comments on commit 1dbc333

Please sign in to comment.