@@ -37,31 +37,31 @@ class PhpcsHelpers
3737 * @param int $position
3838 * @return mixed[]
3939 */
40- public static function classPropertiesTokenIndexes (
41- File $ file ,
42- int $ position
43- ): array {
40+ public static function classPropertiesTokenIndexes (File $ file , int $ position ): array
41+ {
4442 $ tokens = $ file ->getTokens ();
45- $ token = $ tokens [$ position ] ?? [];
4643
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
4956 ) {
5057 return [];
5158 }
5259
5360 $ 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 ;
5964 }
60- $ pointer = (int )$ file ->findNext (
61- T_VARIABLE ,
62- ($ pointer + 1 ),
63- $ token ['scope_closer ' ]
64- );
6565 }
6666
6767 return $ propertyList ;
@@ -75,8 +75,8 @@ public static function classPropertiesTokenIndexes(
7575 public static function variableIsProperty (File $ file , int $ position ): bool
7676 {
7777 $ tokens = $ file ->getTokens ();
78- $ token = $ tokens [$ position ];
79- if ($ token ['code ' ] !== T_VARIABLE ) {
78+ $ varToken = $ tokens [$ position ];
79+ if ($ varToken ['code ' ] !== T_VARIABLE ) {
8080 return false ;
8181 }
8282
@@ -85,6 +85,7 @@ public static function variableIsProperty(File $file, int $position): bool
8585 $ classPointer = $ file ->findPrevious ($ classes , $ position - 1 );
8686 if (!$ classPointer
8787 || !array_key_exists ($ classPointer , $ tokens )
88+ || $ tokens [$ classPointer ]['level ' ] ?? -1 !== (($ varToken ['level ' ] ?? -1 ) - 1 )
8889 || !in_array ($ tokens [$ classPointer ]['code ' ], $ classes , true )
8990 ) {
9091 return false ;
@@ -125,33 +126,29 @@ public static function functionIsMethod(File $file, int $position)
125126 {
126127 $ tokens = $ file ->getTokens ();
127128 $ functionToken = $ tokens [$ position ];
128- if ($ functionToken ['code ' ] !== T_FUNCTION ) {
129+ if (( $ functionToken ['code ' ] ?? '' ) !== T_VARIABLE ) {
129130 return false ;
130131 }
131132
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 );
140135
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+ ) {
143141 return false ;
144142 }
145143
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 ;
148146
149147 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 ;
155152 }
156153
157154 /**
@@ -162,7 +159,7 @@ public static function functionIsMethod(File $file, int $position)
162159 public static function functionIsArrayAccess (File $ file , int $ position )
163160 {
164161 $ token = $ file ->getTokens ()[$ position ] ?? null ;
165- if (!$ token || $ token ['code ' ] !== T_FUNCTION ) {
162+ if (!$ token || $ token ['code ' ] !== T_FUNCTION || ! self :: functionIsMethod ( $ file , $ position ) ) {
166163 return false ;
167164 }
168165
@@ -376,7 +373,7 @@ public static function isHookClosure(
376373 $ lookForFilters and $ actions [] = 'add_filter ' ;
377374 $ lookForActions and $ actions [] = 'add_action ' ;
378375
379- return in_array (( $ tokens [$ functionCall ]['content ' ] ?? '' ) , $ actions , true );
376+ return in_array ($ tokens [$ functionCall ]['content ' ] ?? '' , $ actions , true );
380377 }
381378
382379 /**
@@ -472,7 +469,7 @@ public static function countReturns(File $file, int $functionPosition): array
472469 continue ;
473470 }
474471
475- if (! $ scopeClosers -> count () && $ tokens [$ i ]['code ' ] === T_RETURN ) {
472+ if ($ tokens [$ i ]['code ' ] === T_RETURN && ! $ scopeClosers -> count () ) {
476473 PhpcsHelpers::isVoidReturn ($ file , $ i ) ? $ voidReturnCount ++ : $ nonVoidReturnCount ++;
477474 PhpcsHelpers::isNullReturn ($ file , $ i ) and $ nullReturnCount ++;
478475 }
@@ -581,6 +578,11 @@ public static function functionDocBlockTag(
581578 return $ tags ;
582579 }
583580
581+ /**
582+ * @param File $file
583+ * @param int $position
584+ * @return array
585+ */
584586 public static function findNamespace (File $ file , int $ position ): array
585587 {
586588 $ tokens = $ file ->getTokens ();
@@ -603,7 +605,7 @@ public static function findNamespace(File $file, int $position): array
603605 }
604606
605607 if ($ tokens [$ end ]['code ' ] === T_OPEN_CURLY_BRACKET
606- && ! empty ($ tokens [$ end ]['scope_closer ' ])
608+ && !empty ($ tokens [$ end ]['scope_closer ' ])
607609 && $ tokens [$ end ]['scope_closer ' ] < $ position
608610 ) {
609611 return [null , null ];
0 commit comments