9
9
use PHP_CodeSniffer \Util \Tokens ;
10
10
11
11
class Helpers {
12
+ /**
13
+ * return int[]
14
+ */
15
+ public static function getEmptyTokens () {
16
+ return array_merge (
17
+ array_values (Tokens::$ emptyTokens ),
18
+ [
19
+ T_INLINE_HTML ,
20
+ T_CLOSE_TAG ,
21
+ ]
22
+ );
23
+ }
24
+
12
25
/**
13
26
* @param int|bool $value
14
27
*
@@ -146,7 +159,7 @@ public static function getFunctionIndexForFunctionArgument(File $phpcsFile, $sta
146
159
return null ;
147
160
}
148
161
149
- $ nonFunctionTokenTypes = array_values (Tokens:: $ emptyTokens );
162
+ $ nonFunctionTokenTypes = self :: getEmptyTokens ( );
150
163
$ nonFunctionTokenTypes [] = T_STRING ;
151
164
$ nonFunctionTokenTypes [] = T_BITWISE_AND ;
152
165
$ functionPtr = self ::getIntOrNull ($ phpcsFile ->findPrevious ($ nonFunctionTokenTypes , $ startOfArguments - 1 , null , true , null , true ));
@@ -186,7 +199,7 @@ public static function isTokenInsideFunctionUseImport(File $phpcsFile, $stackPtr
186
199
public static function getUseIndexForUseImport (File $ phpcsFile , $ stackPtr ) {
187
200
$ tokens = $ phpcsFile ->getTokens ();
188
201
189
- $ nonUseTokenTypes = array_values (Tokens:: $ emptyTokens );
202
+ $ nonUseTokenTypes = self :: getEmptyTokens ( );
190
203
$ nonUseTokenTypes [] = T_VARIABLE ;
191
204
$ nonUseTokenTypes [] = T_ELLIPSIS ;
192
205
$ nonUseTokenTypes [] = T_COMMA ;
@@ -215,7 +228,7 @@ public static function findFunctionCall(File $phpcsFile, $stackPtr) {
215
228
$ openPtr = Helpers::findContainingOpeningBracket ($ phpcsFile , $ stackPtr );
216
229
if (is_int ($ openPtr )) {
217
230
// First non-whitespace thing and see if it's a T_STRING function name
218
- $ functionPtr = $ phpcsFile ->findPrevious (Tokens:: $ emptyTokens , $ openPtr - 1 , null , true , null , true );
231
+ $ functionPtr = $ phpcsFile ->findPrevious (self :: getEmptyTokens () , $ openPtr - 1 , null , true , null , true );
219
232
if (is_int ($ functionPtr ) && $ tokens [$ functionPtr ]['code ' ] === T_STRING ) {
220
233
return $ functionPtr ;
221
234
}
@@ -242,7 +255,7 @@ public static function findFunctionCallArguments(File $phpcsFile, $stackPtr) {
242
255
}
243
256
244
257
// $stackPtr is the function name, find our brackets after it
245
- $ openPtr = $ phpcsFile ->findNext (Tokens:: $ emptyTokens , $ stackPtr + 1 , null , true , null , true );
258
+ $ openPtr = $ phpcsFile ->findNext (self :: getEmptyTokens () , $ stackPtr + 1 , null , true , null , true );
246
259
if (($ openPtr === false ) || ($ tokens [$ openPtr ]['code ' ] !== T_OPEN_PARENTHESIS )) {
247
260
return [];
248
261
}
@@ -280,7 +293,7 @@ public static function getNextAssignPointer(File $phpcsFile, $stackPtr) {
280
293
$ tokens = $ phpcsFile ->getTokens ();
281
294
282
295
// Is the next non-whitespace an assignment?
283
- $ nextPtr = $ phpcsFile ->findNext (Tokens:: $ emptyTokens , $ stackPtr + 1 , null , true , null , true );
296
+ $ nextPtr = $ phpcsFile ->findNext (self :: getEmptyTokens () , $ stackPtr + 1 , null , true , null , true );
284
297
if (is_int ($ nextPtr )
285
298
&& isset (Tokens::$ assignmentTokens [$ tokens [$ nextPtr ]['code ' ]])
286
299
// Ignore double arrow to prevent triggering on `foreach ( $array as $k => $v )`.
@@ -508,14 +521,14 @@ public static function isArrowFunction(File $phpcsFile, $stackPtr) {
508
521
return false ;
509
522
}
510
523
// Make sure next non-space token is an open parenthesis
511
- $ openParenIndex = $ phpcsFile ->findNext (Tokens:: $ emptyTokens , $ stackPtr + 1 , null , true );
524
+ $ openParenIndex = $ phpcsFile ->findNext (self :: getEmptyTokens () , $ stackPtr + 1 , null , true );
512
525
if (! is_int ($ openParenIndex ) || $ tokens [$ openParenIndex ]['code ' ] !== T_OPEN_PARENTHESIS ) {
513
526
return false ;
514
527
}
515
528
// Find the associated close parenthesis
516
529
$ closeParenIndex = $ tokens [$ openParenIndex ]['parenthesis_closer ' ];
517
530
// Make sure the next token is a fat arrow
518
- $ fatArrowIndex = $ phpcsFile ->findNext (Tokens:: $ emptyTokens , $ closeParenIndex + 1 , null , true );
531
+ $ fatArrowIndex = $ phpcsFile ->findNext (self :: getEmptyTokens () , $ closeParenIndex + 1 , null , true );
519
532
if (! is_int ($ fatArrowIndex )) {
520
533
return false ;
521
534
}
@@ -543,14 +556,14 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr) {
543
556
return null ;
544
557
}
545
558
// Make sure next non-space token is an open parenthesis
546
- $ openParenIndex = $ phpcsFile ->findNext (Tokens:: $ emptyTokens , $ stackPtr + 1 , null , true );
559
+ $ openParenIndex = $ phpcsFile ->findNext (self :: getEmptyTokens () , $ stackPtr + 1 , null , true );
547
560
if (! is_int ($ openParenIndex ) || $ tokens [$ openParenIndex ]['code ' ] !== T_OPEN_PARENTHESIS ) {
548
561
return null ;
549
562
}
550
563
// Find the associated close parenthesis
551
564
$ closeParenIndex = $ tokens [$ openParenIndex ]['parenthesis_closer ' ];
552
565
// Make sure the next token is a fat arrow
553
- $ fatArrowIndex = $ phpcsFile ->findNext (Tokens:: $ emptyTokens , $ closeParenIndex + 1 , null , true );
566
+ $ fatArrowIndex = $ phpcsFile ->findNext (self :: getEmptyTokens () , $ closeParenIndex + 1 , null , true );
554
567
if (! is_int ($ fatArrowIndex )) {
555
568
return null ;
556
569
}
@@ -600,7 +613,7 @@ public static function getListAssignments(File $phpcsFile, $listOpenerIndex) {
600
613
}
601
614
602
615
// Find the assignment (equals sign) which, if this is a list assignment, should be the next non-space token
603
- $ assignPtr = $ phpcsFile ->findNext (Tokens:: $ emptyTokens , $ closePtr + 1 , null , true );
616
+ $ assignPtr = $ phpcsFile ->findNext (self :: getEmptyTokens () , $ closePtr + 1 , null , true );
604
617
605
618
// If the next token isn't an assignment, check for nested brackets because we might be a nested assignment
606
619
if (! is_int ($ assignPtr ) || $ tokens [$ assignPtr ]['code ' ] !== T_EQUAL ) {
@@ -715,7 +728,7 @@ public static function isVariableANumericVariable($varName) {
715
728
*/
716
729
public static function isVariableInsideElseCondition (File $ phpcsFile , $ stackPtr ) {
717
730
$ tokens = $ phpcsFile ->getTokens ();
718
- $ nonFunctionTokenTypes = array_values (Tokens:: $ emptyTokens );
731
+ $ nonFunctionTokenTypes = self :: getEmptyTokens ( );
719
732
$ nonFunctionTokenTypes [] = T_OPEN_PARENTHESIS ;
720
733
$ nonFunctionTokenTypes [] = T_VARIABLE ;
721
734
$ nonFunctionTokenTypes [] = T_ELLIPSIS ;
@@ -837,7 +850,7 @@ public static function getScopeCloseForScopeOpen(File $phpcsFile, $scopeStartInd
837
850
public static function getLastNonEmptyTokenIndexInFile (File $ phpcsFile ) {
838
851
$ tokens = $ phpcsFile ->getTokens ();
839
852
foreach (array_reverse ($ tokens , true ) as $ index => $ token ) {
840
- if (! in_array ($ token ['code ' ], Tokens:: $ emptyTokens , true )) {
853
+ if (! in_array ($ token ['code ' ], self :: getEmptyTokens () , true )) {
841
854
return $ index ;
842
855
}
843
856
}
@@ -921,7 +934,7 @@ public static function getFunctionIndexForFunctionCallArgument(File $phpcsFile,
921
934
return null ;
922
935
}
923
936
924
- $ nonFunctionTokenTypes = array_values (Tokens:: $ emptyTokens );
937
+ $ nonFunctionTokenTypes = self :: getEmptyTokens ( );
925
938
$ functionPtr = self ::getIntOrNull ($ phpcsFile ->findPrevious ($ nonFunctionTokenTypes , $ startOfArguments - 1 , null , true , null , true ));
926
939
if (! is_int ($ functionPtr ) || ! isset ($ tokens [$ functionPtr ]['code ' ])) {
927
940
return null ;
@@ -965,7 +978,7 @@ public static function isVariableInsideIssetOrEmpty(File $phpcsFile, $stackPtr)
965
978
*/
966
979
public static function isVariableArrayPushShortcut (File $ phpcsFile , $ stackPtr ) {
967
980
$ tokens = $ phpcsFile ->getTokens ();
968
- $ nonFunctionTokenTypes = array_values (Tokens:: $ emptyTokens );
981
+ $ nonFunctionTokenTypes = self :: getEmptyTokens ( );
969
982
970
983
$ arrayPushOperatorIndex1 = self ::getIntOrNull ($ phpcsFile ->findNext ($ nonFunctionTokenTypes , $ stackPtr + 1 , null , true , null , true ));
971
984
if (! is_int ($ arrayPushOperatorIndex1 )) {
@@ -1063,7 +1076,7 @@ public static function isTokenInsideAssignmentLHS(File $phpcsFile, $stackPtr) {
1063
1076
public static function isTokenVariableVariable (File $ phpcsFile , $ stackPtr ) {
1064
1077
$ tokens = $ phpcsFile ->getTokens ();
1065
1078
1066
- $ prev = $ phpcsFile ->findPrevious (Tokens:: $ emptyTokens , ($ stackPtr - 1 ), null , true );
1079
+ $ prev = $ phpcsFile ->findPrevious (self :: getEmptyTokens () , ($ stackPtr - 1 ), null , true );
1067
1080
if ($ prev === false ) {
1068
1081
return false ;
1069
1082
}
@@ -1074,7 +1087,7 @@ public static function isTokenVariableVariable(File $phpcsFile, $stackPtr) {
1074
1087
return false ;
1075
1088
}
1076
1089
1077
- $ prevPrev = $ phpcsFile ->findPrevious (Tokens:: $ emptyTokens , ($ prev - 1 ), null , true );
1090
+ $ prevPrev = $ phpcsFile ->findPrevious (self :: getEmptyTokens () , ($ prev - 1 ), null , true );
1078
1091
if ($ prevPrev !== false && $ tokens [$ prevPrev ]['code ' ] === T_DOLLAR ) {
1079
1092
return true ;
1080
1093
}
0 commit comments