@@ -112,6 +112,12 @@ public static function areConditionsWithinFunctionBeforeClass(array $conditions)
112
112
}
113
113
114
114
/**
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
+ *
115
121
* @param File $phpcsFile
116
122
* @param int $openPtr
117
123
*
@@ -122,11 +128,24 @@ public static function findPreviousFunctionPtr(File $phpcsFile, $openPtr) {
122
128
// so we look backwards from the opening bracket for the first thing that
123
129
// isn't a function name, reference sigil or whitespace and check if it's a
124
130
// 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
+ }
128
139
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 ;
130
149
}
131
150
132
151
/**
@@ -275,11 +294,7 @@ public static function findFunctionDefinition(File $phpcsFile, $stackPtr) {
275
294
if (! is_int ($ openPtr )) {
276
295
return null ;
277
296
}
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 );
283
298
}
284
299
285
300
/**
0 commit comments