From 1dbc333797a43c3e36697c24c66d3c1f8364faa2 Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Tue, 10 Mar 2020 08:32:33 +1100 Subject: [PATCH] Fixed bug #2865 : Double arrow tokenized as T_STRING when placed after function named "fn" --- package.xml | 1 + .../Arrays/ArrayDeclarationUnitTest.1.inc | 5 ++++ .../ArrayDeclarationUnitTest.1.inc.fixed | 5 ++++ .../Arrays/ArrayDeclarationUnitTest.2.inc | 5 ++++ .../ArrayDeclarationUnitTest.2.inc.fixed | 5 ++++ .../Tests/Arrays/ArrayDeclarationUnitTest.php | 2 ++ src/Tokenizers/PHP.php | 30 ++++++++++++------- 7 files changed, 43 insertions(+), 10 deletions(-) diff --git a/package.xml b/package.xml index 1f2ced9b58..3df44228ca 100644 --- a/package.xml +++ b/package.xml @@ -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 diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc index 5950a2745d..8671c5da16 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc @@ -424,6 +424,11 @@ $array = array( 3 => '3', ); +$foo = array( + $this->fn => 'value', + $foo->fn => 'value', + ); + // Intentional syntax error. $a = array( 'a' => diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed index 760f795546..de5325c2e1 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed @@ -452,6 +452,11 @@ $array = array( 3 => '3', ); +$foo = array( + $this->fn => 'value', + $foo->fn => 'value', + ); + // Intentional syntax error. $a = array( 'a' => diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc index c958e1e1a6..942b9b9de1 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc @@ -413,6 +413,11 @@ $array = [ 3 => '3', ]; +$foo = [ + $this->fn => 'value', + $foo->fn => 'value', + ]; + // Intentional syntax error. $a = [ 'a' => diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed index 7c8eae9d67..d563408d88 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed @@ -439,6 +439,11 @@ $array = [ 3 => '3', ]; +$foo = [ + $this->fn => 'value', + $foo->fn => 'value', + ]; + // Intentional syntax error. $a = [ 'a' => diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php index d3df3313ee..9c0bf7bd50 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php @@ -113,6 +113,7 @@ public function getErrorList($testFile='') 370 => 1, 383 => 1, 394 => 1, + 429 => 1, ]; case 'ArrayDeclarationUnitTest.2.inc': return [ @@ -188,6 +189,7 @@ public function getErrorList($testFile='') 358 => 1, 372 => 1, 383 => 1, + 418 => 1, ]; default: return []; diff --git a/src/Tokenizers/PHP.php b/src/Tokenizers/PHP.php index e63bbc84c0..7f58125030 100644 --- a/src/Tokenizers/PHP.php +++ b/src/Tokenizers/PHP.php @@ -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; + } } /* @@ -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