@@ -37,31 +37,31 @@ class PhpcsHelpers
37
37
* @param int $position
38
38
* @return mixed[]
39
39
*/
40
- public static function classPropertiesTokenIndexes (
41
- File $ file ,
42
- int $ position
43
- ): array {
40
+ public static function classPropertiesTokenIndexes (File $ file , int $ position ): array
41
+ {
44
42
$ tokens = $ file ->getTokens ();
45
- $ token = $ tokens [$ position ] ?? [];
46
43
47
- if (!array_key_exists ('scope_opener ' , $ token )
48
- || !array_key_exists ('scope_closer ' , $ token )
44
+ if (!in_array ($ tokens [$ position ]['code ' ] ?? '' , [T_CLASS , T_ANON_CLASS , T_TRAIT ], true )) {
45
+ return [];
46
+ }
47
+
48
+ $ opener = $ tokens [$ position ]['scope_opener ' ] ?? -1 ;
49
+ $ closer = $ tokens [$ position ]['scope_closer ' ] ?? -1 ;
50
+
51
+ if ($ opener <= 0
52
+ || $ closer <= 0
53
+ || $ closer <= $ opener
54
+ || $ closer <= $ position
55
+ || $ opener >= $ position
49
56
) {
50
57
return [];
51
58
}
52
59
53
60
$ propertyList = [];
54
- $ pointer = (int )$ token ['scope_opener ' ];
55
-
56
- while ($ pointer ) {
57
- if (self ::variableIsProperty ($ file , $ pointer )) {
58
- $ propertyList [] = $ pointer ;
61
+ for ($ i = $ opener + 1 ; $ i < $ closer ; $ i ++) {
62
+ if (self ::variableIsProperty ($ file , $ i )) {
63
+ $ propertyList [] = $ i ;
59
64
}
60
- $ pointer = (int )$ file ->findNext (
61
- T_VARIABLE ,
62
- ($ pointer + 1 ),
63
- $ token ['scope_closer ' ]
64
- );
65
65
}
66
66
67
67
return $ propertyList ;
@@ -75,8 +75,8 @@ public static function classPropertiesTokenIndexes(
75
75
public static function variableIsProperty (File $ file , int $ position ): bool
76
76
{
77
77
$ tokens = $ file ->getTokens ();
78
- $ token = $ tokens [$ position ];
79
- if ($ token ['code ' ] !== T_VARIABLE ) {
78
+ $ varToken = $ tokens [$ position ];
79
+ if ($ varToken ['code ' ] !== T_VARIABLE ) {
80
80
return false ;
81
81
}
82
82
@@ -85,6 +85,7 @@ public static function variableIsProperty(File $file, int $position): bool
85
85
$ classPointer = $ file ->findPrevious ($ classes , $ position - 1 );
86
86
if (!$ classPointer
87
87
|| !array_key_exists ($ classPointer , $ tokens )
88
+ || $ tokens [$ classPointer ]['level ' ] ?? -1 !== (($ varToken ['level ' ] ?? -1 ) - 1 )
88
89
|| !in_array ($ tokens [$ classPointer ]['code ' ], $ classes , true )
89
90
) {
90
91
return false ;
@@ -125,33 +126,29 @@ public static function functionIsMethod(File $file, int $position)
125
126
{
126
127
$ tokens = $ file ->getTokens ();
127
128
$ functionToken = $ tokens [$ position ];
128
- if ($ functionToken ['code ' ] !== T_FUNCTION ) {
129
+ if (( $ functionToken ['code ' ] ?? '' ) !== T_VARIABLE ) {
129
130
return false ;
130
131
}
131
132
132
- $ classPointer = $ file ->findPrevious (
133
- [T_CLASS , T_INTERFACE , T_TRAIT ],
134
- $ position - 1
135
- );
136
-
137
- if (!$ classPointer ) {
138
- return false ;
139
- }
133
+ $ classes = [T_CLASS , T_ANON_CLASS , T_TRAIT , T_INTERFACE ];
134
+ $ classPointer = $ file ->findPrevious ($ classes , $ position - 1 );
140
135
141
- $ classToken = $ tokens [$ classPointer ];
142
- if ($ classToken ['level ' ] !== $ functionToken ['level ' ] - 1 ) {
136
+ if (!$ classPointer
137
+ || !array_key_exists ($ classPointer , $ tokens )
138
+ || $ tokens [$ classPointer ]['level ' ] ?? -1 !== (($ functionToken ['level ' ] ?? -1 ) - 1 )
139
+ || !in_array ($ tokens [$ classPointer ]['code ' ] ?? '' , $ classes , true )
140
+ ) {
143
141
return false ;
144
142
}
145
143
146
- $ openerPosition = $ classToken ['scope_opener ' ] ?? -1 ;
147
- $ closerPosition = $ classToken ['scope_closer ' ] ?? -1 ;
144
+ $ opener = $ tokens [ $ classPointer ] ['scope_opener ' ] ?? -1 ;
145
+ $ closer = $ tokens [ $ classPointer ] ['scope_closer ' ] ?? -1 ;
148
146
149
147
return
150
- $ openerPosition > 0
151
- && $ closerPosition > 0
152
- && $ closerPosition > ($ openerPosition + 1 )
153
- && $ openerPosition < ($ position - 1 )
154
- && $ closerPosition > $ position + 4 ; // 4 because: (){}
148
+ $ opener > 0
149
+ && $ closer > 1
150
+ && $ closer > ($ position + 3 )
151
+ && $ opener < $ position ;
155
152
}
156
153
157
154
/**
@@ -162,7 +159,7 @@ public static function functionIsMethod(File $file, int $position)
162
159
public static function functionIsArrayAccess (File $ file , int $ position )
163
160
{
164
161
$ token = $ file ->getTokens ()[$ position ] ?? null ;
165
- if (!$ token || $ token ['code ' ] !== T_FUNCTION ) {
162
+ if (!$ token || $ token ['code ' ] !== T_FUNCTION || ! self :: functionIsMethod ( $ file , $ position ) ) {
166
163
return false ;
167
164
}
168
165
@@ -376,7 +373,7 @@ public static function isHookClosure(
376
373
$ lookForFilters and $ actions [] = 'add_filter ' ;
377
374
$ lookForActions and $ actions [] = 'add_action ' ;
378
375
379
- return in_array (( $ tokens [$ functionCall ]['content ' ] ?? '' ) , $ actions , true );
376
+ return in_array ($ tokens [$ functionCall ]['content ' ] ?? '' , $ actions , true );
380
377
}
381
378
382
379
/**
@@ -472,7 +469,7 @@ public static function countReturns(File $file, int $functionPosition): array
472
469
continue ;
473
470
}
474
471
475
- if (! $ scopeClosers -> count () && $ tokens [$ i ]['code ' ] === T_RETURN ) {
472
+ if ($ tokens [$ i ]['code ' ] === T_RETURN && ! $ scopeClosers -> count () ) {
476
473
PhpcsHelpers::isVoidReturn ($ file , $ i ) ? $ voidReturnCount ++ : $ nonVoidReturnCount ++;
477
474
PhpcsHelpers::isNullReturn ($ file , $ i ) and $ nullReturnCount ++;
478
475
}
@@ -581,6 +578,11 @@ public static function functionDocBlockTag(
581
578
return $ tags ;
582
579
}
583
580
581
+ /**
582
+ * @param File $file
583
+ * @param int $position
584
+ * @return array
585
+ */
584
586
public static function findNamespace (File $ file , int $ position ): array
585
587
{
586
588
$ tokens = $ file ->getTokens ();
@@ -603,7 +605,7 @@ public static function findNamespace(File $file, int $position): array
603
605
}
604
606
605
607
if ($ tokens [$ end ]['code ' ] === T_OPEN_CURLY_BRACKET
606
- && ! empty ($ tokens [$ end ]['scope_closer ' ])
608
+ && !empty ($ tokens [$ end ]['scope_closer ' ])
607
609
&& $ tokens [$ end ]['scope_closer ' ] < $ position
608
610
) {
609
611
return [null , null ];
0 commit comments