@@ -112,6 +112,12 @@ public static function areConditionsWithinFunctionBeforeClass(array $conditions)
112112 }
113113
114114 /**
115+ * Find the token index of the function keyword for an open brace
116+ *
117+ * Given the token index of an open curly brace, return the index of the
118+ * function keyword for that brace, or null if the brace is not for a
119+ * function.
120+ *
115121 * @param File $phpcsFile
116122 * @param int $openPtr
117123 *
@@ -122,11 +128,24 @@ public static function findPreviousFunctionPtr(File $phpcsFile, $openPtr) {
122128 // so we look backwards from the opening bracket for the first thing that
123129 // isn't a function name, reference sigil or whitespace and check if it's a
124130 // function keyword.
125- $ functionPtrTypes = Tokens::$ emptyTokens ;
126- $ functionPtrTypes [T_STRING ] = T_STRING ;
127- $ functionPtrTypes [T_BITWISE_AND ] = T_BITWISE_AND ;
131+ $ nonFunctionTokenTypes = Tokens::$ emptyTokens ;
132+ $ nonFunctionTokenTypes [T_STRING ] = T_STRING ;
133+ $ nonFunctionTokenTypes [T_BITWISE_AND ] = T_BITWISE_AND ;
134+
135+ $ functionPtr = self ::getIntOrNull ($ phpcsFile ->findPrevious ($ nonFunctionTokenTypes , $ openPtr - 1 , null , true , null , true ));
136+ if (! is_int ($ functionPtr )) {
137+ return null ;
138+ }
128139
129- return self ::getIntOrNull ($ phpcsFile ->findPrevious ($ functionPtrTypes , $ openPtr - 1 , null , true , null , true ));
140+ $ functionTokenTypes = [
141+ T_FUNCTION ,
142+ T_CLOSURE ,
143+ ];
144+ $ tokens = $ phpcsFile ->getTokens ();
145+ if (in_array ($ tokens [$ functionPtr ]['code ' ], $ functionTokenTypes , true )) {
146+ return $ functionPtr ;
147+ }
148+ return null ;
130149 }
131150
132151 /**
@@ -275,11 +294,7 @@ public static function findFunctionDefinition(File $phpcsFile, $stackPtr) {
275294 if (! is_int ($ openPtr )) {
276295 return null ;
277296 }
278- $ functionPtr = Helpers::findPreviousFunctionPtr ($ phpcsFile , $ openPtr );
279- if (($ functionPtr !== null ) && ($ tokens [$ functionPtr ]['code ' ] === T_FUNCTION )) {
280- return $ functionPtr ;
281- }
282- return null ;
297+ return Helpers::findPreviousFunctionPtr ($ phpcsFile , $ openPtr );
283298 }
284299
285300 /**
0 commit comments