16
16
class PHP extends Tokenizer
17
17
{
18
18
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
+
19
45
/**
20
46
* Regular expression to check if a given identifier name is valid for use in PHP.
21
47
*
@@ -482,27 +508,10 @@ class PHP extends Tokenizer
482
508
* Contexts in which keywords should always be tokenized as T_STRING.
483
509
*
484
510
* @var array
511
+ *
512
+ * @deprecated 4.0.0 Use the PHP::T_STRING_CONTEXTS constant instead.
485
513
*/
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 ;
506
515
507
516
/**
508
517
* A cache of different token types, resolved into arrays.
@@ -623,11 +632,11 @@ protected function tokenize($string)
623
632
624
633
if ($ tokenIsArray === true
625
634
&& 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
627
636
|| $ finalTokens [$ lastNotEmptyToken ]['content ' ] === '& '
628
637
|| $ insideConstDeclaration === true )
629
638
) {
630
- if (isset ($ this -> tstringContexts [$ finalTokens [$ lastNotEmptyToken ]['code ' ]]) === true ) {
639
+ if (isset (static :: T_STRING_CONTEXTS [$ finalTokens [$ lastNotEmptyToken ]['code ' ]]) === true ) {
631
640
$ preserveKeyword = false ;
632
641
633
642
// `new class`, and `new static` should be preserved.
@@ -683,7 +692,7 @@ protected function tokenize($string)
683
692
}//end if
684
693
685
694
// 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
687
696
&& $ finalTokens [$ lastNotEmptyToken ]['code ' ] === T_CONST )
688
697
|| $ insideConstDeclaration === true
689
698
) {
@@ -1251,7 +1260,7 @@ protected function tokenize($string)
1251
1260
1252
1261
if ($ tokenIsArray === true
1253
1262
&& $ token [0 ] === T_CASE
1254
- && isset ($ this -> tstringContexts [$ finalTokens [$ lastNotEmptyToken ]['code ' ]]) === false
1263
+ && isset (static :: T_STRING_CONTEXTS [$ finalTokens [$ lastNotEmptyToken ]['code ' ]]) === false
1255
1264
) {
1256
1265
$ isEnumCase = false ;
1257
1266
$ scope = 1 ;
@@ -1497,7 +1506,7 @@ protected function tokenize($string)
1497
1506
1498
1507
if ($ tokenIsArray === true
1499
1508
&& strtolower ($ token [1 ]) === 'readonly '
1500
- && (isset ($ this -> tstringContexts [$ finalTokens [$ lastNotEmptyToken ]['code ' ]]) === false
1509
+ && (isset (static :: T_STRING_CONTEXTS [$ finalTokens [$ lastNotEmptyToken ]['code ' ]]) === false
1501
1510
|| $ finalTokens [$ lastNotEmptyToken ]['code ' ] === T_NEW )
1502
1511
) {
1503
1512
// Get the next non-whitespace token.
@@ -1885,7 +1894,7 @@ protected function tokenize($string)
1885
1894
break ;
1886
1895
}
1887
1896
1888
- if (isset ($ this -> tstringContexts [$ finalTokens [$ lastNotEmptyToken ]['code ' ]]) === true ) {
1897
+ if (isset (static :: T_STRING_CONTEXTS [$ finalTokens [$ lastNotEmptyToken ]['code ' ]]) === true ) {
1889
1898
// Also not a match expression.
1890
1899
break ;
1891
1900
}
@@ -1932,7 +1941,7 @@ protected function tokenize($string)
1932
1941
1933
1942
if ($ tokenIsArray === true
1934
1943
&& $ token [0 ] === T_DEFAULT
1935
- && isset ($ this -> tstringContexts [$ finalTokens [$ lastNotEmptyToken ]['code ' ]]) === false
1944
+ && isset (static :: T_STRING_CONTEXTS [$ finalTokens [$ lastNotEmptyToken ]['code ' ]]) === false
1936
1945
) {
1937
1946
for ($ x = ($ stackPtr + 1 ); $ x < $ numTokens ; $ x ++) {
1938
1947
if ($ tokens [$ x ] === ', ' ) {
@@ -2364,7 +2373,7 @@ function return types. We want to keep the parenthesis map clean,
2364
2373
2365
2374
// True/false/parent/self/static in typed constants should be fixed to their own token,
2366
2375
// 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
2368
2377
&& $ finalTokens [$ lastNotEmptyToken ]['code ' ] === T_CONST )
2369
2378
|| $ insideConstDeclaration === true
2370
2379
) {
@@ -2383,7 +2392,7 @@ function return types. We want to keep the parenthesis map clean,
2383
2392
$ preserveTstring = true ;
2384
2393
$ insideConstDeclaration = false ;
2385
2394
}
2386
- } else if (isset ($ this -> tstringContexts [$ finalTokens [$ lastNotEmptyToken ]['code ' ]]) === true
2395
+ } else if (isset (static :: T_STRING_CONTEXTS [$ finalTokens [$ lastNotEmptyToken ]['code ' ]]) === true
2387
2396
&& $ finalTokens [$ lastNotEmptyToken ]['code ' ] !== T_CONST
2388
2397
) {
2389
2398
$ preserveTstring = true ;
@@ -3445,7 +3454,7 @@ protected function processAdditional()
3445
3454
}
3446
3455
3447
3456
if ($ x !== $ numTokens
3448
- && isset ($ this -> tstringContexts [$ this ->tokens [$ x ]['code ' ]]) === true
3457
+ && isset (static :: T_STRING_CONTEXTS [$ this ->tokens [$ x ]['code ' ]]) === true
3449
3458
) {
3450
3459
if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
3451
3460
$ line = $ this ->tokens [$ i ]['line ' ];
0 commit comments