Skip to content

Commit 601b296

Browse files
committed
Modernize: Tokenizers\PHP: use class constant for constant array
Note: there are a couple of other properties in this class, which only contain static information. These, by nature, should be constants. however, those properties are overloading empty array properties from the abstract parent `Tokenizer` class, so I'm leaving those alone for now.
1 parent cbe573e commit 601b296

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

src/Tokenizers/PHP.php

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,32 @@
1616
class PHP extends Tokenizer
1717
{
1818

19+
/**
20+
* Contexts in which keywords should always be tokenized as T_STRING.
21+
*
22+
* @var array<int|string, true>
23+
*/
24+
protected const T_STRING_CONTEXTS = [
25+
T_OBJECT_OPERATOR => true,
26+
T_NULLSAFE_OBJECT_OPERATOR => true,
27+
T_FUNCTION => true,
28+
T_CLASS => true,
29+
T_INTERFACE => true,
30+
T_TRAIT => true,
31+
T_ENUM => true,
32+
T_ENUM_CASE => true,
33+
T_EXTENDS => true,
34+
T_IMPLEMENTS => true,
35+
T_ATTRIBUTE => true,
36+
T_NEW => true,
37+
T_CONST => true,
38+
T_NS_SEPARATOR => true,
39+
T_USE => true,
40+
T_NAMESPACE => true,
41+
T_PAAMAYIM_NEKUDOTAYIM => true,
42+
T_GOTO => true,
43+
];
44+
1945
/**
2046
* Regular expression to check if a given identifier name is valid for use in PHP.
2147
*
@@ -482,27 +508,10 @@ class PHP extends Tokenizer
482508
* Contexts in which keywords should always be tokenized as T_STRING.
483509
*
484510
* @var array
511+
*
512+
* @deprecated 4.0.0 Use the PHP::T_STRING_CONTEXTS constant instead.
485513
*/
486-
protected $tstringContexts = [
487-
T_OBJECT_OPERATOR => true,
488-
T_NULLSAFE_OBJECT_OPERATOR => true,
489-
T_FUNCTION => true,
490-
T_CLASS => true,
491-
T_INTERFACE => true,
492-
T_TRAIT => true,
493-
T_ENUM => true,
494-
T_ENUM_CASE => true,
495-
T_EXTENDS => true,
496-
T_IMPLEMENTS => true,
497-
T_ATTRIBUTE => true,
498-
T_NEW => true,
499-
T_CONST => true,
500-
T_NS_SEPARATOR => true,
501-
T_USE => true,
502-
T_NAMESPACE => true,
503-
T_PAAMAYIM_NEKUDOTAYIM => true,
504-
T_GOTO => true,
505-
];
514+
protected $tstringContexts = self::T_STRING_CONTEXTS;
506515

507516
/**
508517
* A cache of different token types, resolved into arrays.
@@ -623,11 +632,11 @@ protected function tokenize($string)
623632

624633
if ($tokenIsArray === true
625634
&& isset(Tokens::CONTEXT_SENSITIVE_KEYWORDS[$token[0]]) === true
626-
&& (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true
635+
&& (isset(static::T_STRING_CONTEXTS[$finalTokens[$lastNotEmptyToken]['code']]) === true
627636
|| $finalTokens[$lastNotEmptyToken]['content'] === '&'
628637
|| $insideConstDeclaration === true)
629638
) {
630-
if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
639+
if (isset(static::T_STRING_CONTEXTS[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
631640
$preserveKeyword = false;
632641

633642
// `new class`, and `new static` should be preserved.
@@ -683,7 +692,7 @@ protected function tokenize($string)
683692
}//end if
684693

685694
// Types in typed constants should not be touched, but the constant name should be.
686-
if ((isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true
695+
if ((isset(static::T_STRING_CONTEXTS[$finalTokens[$lastNotEmptyToken]['code']]) === true
687696
&& $finalTokens[$lastNotEmptyToken]['code'] === T_CONST)
688697
|| $insideConstDeclaration === true
689698
) {
@@ -1251,7 +1260,7 @@ protected function tokenize($string)
12511260

12521261
if ($tokenIsArray === true
12531262
&& $token[0] === T_CASE
1254-
&& isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false
1263+
&& isset(static::T_STRING_CONTEXTS[$finalTokens[$lastNotEmptyToken]['code']]) === false
12551264
) {
12561265
$isEnumCase = false;
12571266
$scope = 1;
@@ -1497,7 +1506,7 @@ protected function tokenize($string)
14971506

14981507
if ($tokenIsArray === true
14991508
&& strtolower($token[1]) === 'readonly'
1500-
&& (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false
1509+
&& (isset(static::T_STRING_CONTEXTS[$finalTokens[$lastNotEmptyToken]['code']]) === false
15011510
|| $finalTokens[$lastNotEmptyToken]['code'] === T_NEW)
15021511
) {
15031512
// Get the next non-whitespace token.
@@ -1885,7 +1894,7 @@ protected function tokenize($string)
18851894
break;
18861895
}
18871896

1888-
if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
1897+
if (isset(static::T_STRING_CONTEXTS[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
18891898
// Also not a match expression.
18901899
break;
18911900
}
@@ -1932,7 +1941,7 @@ protected function tokenize($string)
19321941

19331942
if ($tokenIsArray === true
19341943
&& $token[0] === T_DEFAULT
1935-
&& isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false
1944+
&& isset(static::T_STRING_CONTEXTS[$finalTokens[$lastNotEmptyToken]['code']]) === false
19361945
) {
19371946
for ($x = ($stackPtr + 1); $x < $numTokens; $x++) {
19381947
if ($tokens[$x] === ',') {
@@ -2364,7 +2373,7 @@ function return types. We want to keep the parenthesis map clean,
23642373

23652374
// True/false/parent/self/static in typed constants should be fixed to their own token,
23662375
// but the constant name should not be.
2367-
if ((isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true
2376+
if ((isset(static::T_STRING_CONTEXTS[$finalTokens[$lastNotEmptyToken]['code']]) === true
23682377
&& $finalTokens[$lastNotEmptyToken]['code'] === T_CONST)
23692378
|| $insideConstDeclaration === true
23702379
) {
@@ -2383,7 +2392,7 @@ function return types. We want to keep the parenthesis map clean,
23832392
$preserveTstring = true;
23842393
$insideConstDeclaration = false;
23852394
}
2386-
} else if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true
2395+
} else if (isset(static::T_STRING_CONTEXTS[$finalTokens[$lastNotEmptyToken]['code']]) === true
23872396
&& $finalTokens[$lastNotEmptyToken]['code'] !== T_CONST
23882397
) {
23892398
$preserveTstring = true;
@@ -3445,7 +3454,7 @@ protected function processAdditional()
34453454
}
34463455

34473456
if ($x !== $numTokens
3448-
&& isset($this->tstringContexts[$this->tokens[$x]['code']]) === true
3457+
&& isset(static::T_STRING_CONTEXTS[$this->tokens[$x]['code']]) === true
34493458
) {
34503459
if (PHP_CODESNIFFER_VERBOSITY > 1) {
34513460
$line = $this->tokens[$i]['line'];

0 commit comments

Comments
 (0)