diff --git a/src/Tokenizers/PHP.php b/src/Tokenizers/PHP.php index 60c89c4507..2dd7d135b6 100644 --- a/src/Tokenizers/PHP.php +++ b/src/Tokenizers/PHP.php @@ -454,6 +454,29 @@ class PHP extends Tokenizer T_TYPE_UNION => 1, ]; + /** + * Contexts in which keywords should always be tokenized as T_STRING. + * + * @var array + */ + protected $tstringContexts = [ + T_OBJECT_OPERATOR => true, + T_NULLSAFE_OBJECT_OPERATOR => true, + T_FUNCTION => true, + T_CLASS => true, + T_INTERFACE => true, + T_TRAIT => true, + T_EXTENDS => true, + T_IMPLEMENTS => true, + T_ATTRIBUTE => true, + T_NEW => true, + T_CONST => true, + T_NS_SEPARATOR => true, + T_USE => true, + T_NAMESPACE => true, + T_PAAMAYIM_NEKUDOTAYIM => true, + ]; + /** * A cache of different token types, resolved into arrays. * @@ -1332,16 +1355,7 @@ protected function tokenize($string) break; } - $notMatchContext = [ - T_PAAMAYIM_NEKUDOTAYIM => true, - T_OBJECT_OPERATOR => true, - T_NULLSAFE_OBJECT_OPERATOR => true, - T_NS_SEPARATOR => true, - T_NEW => true, - T_FUNCTION => true, - ]; - - if (isset($notMatchContext[$finalTokens[$lastNotEmptyToken]['code']]) === true) { + if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true) { // Also not a match expression. break; } @@ -1389,14 +1403,7 @@ protected function tokenize($string) if ($tokenIsArray === true && $token[0] === T_DEFAULT ) { - $ignoreContext = [ - T_OBJECT_OPERATOR => true, - T_NULLSAFE_OBJECT_OPERATOR => true, - T_NS_SEPARATOR => true, - T_PAAMAYIM_NEKUDOTAYIM => true, - ]; - - if (isset($ignoreContext[$finalTokens[$lastNotEmptyToken]['code']]) === false) { + if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false) { for ($x = ($stackPtr + 1); $x < $numTokens; $x++) { if ($tokens[$x] === ',') { // Skip over potential trailing comma (supported in PHP). @@ -1894,23 +1901,7 @@ function return types. We want to keep the parenthesis map clean, if ($tokenIsArray === true && $token[0] === T_STRING) { // Some T_STRING tokens should remain that way // due to their context. - $context = [ - T_OBJECT_OPERATOR => true, - T_NULLSAFE_OBJECT_OPERATOR => true, - T_FUNCTION => true, - T_CLASS => true, - T_EXTENDS => true, - T_IMPLEMENTS => true, - T_ATTRIBUTE => true, - T_NEW => true, - T_CONST => true, - T_NS_SEPARATOR => true, - T_USE => true, - T_NAMESPACE => true, - T_PAAMAYIM_NEKUDOTAYIM => true, - ]; - - if (isset($context[$finalTokens[$lastNotEmptyToken]['code']]) === true) { + if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true) { // Special case for syntax like: return new self // where self should not be a string. if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW @@ -2781,13 +2772,7 @@ protected function processAdditional() } } - $context = [ - T_OBJECT_OPERATOR => true, - T_NULLSAFE_OBJECT_OPERATOR => true, - T_NS_SEPARATOR => true, - T_PAAMAYIM_NEKUDOTAYIM => true, - ]; - if (isset($context[$this->tokens[$x]['code']]) === true) { + if (isset($this->tstringContexts[$this->tokens[$x]['code']]) === true) { if (PHP_CODESNIFFER_VERBOSITY > 1) { $line = $this->tokens[$i]['line']; $type = $this->tokens[$i]['type'];