@@ -105,32 +105,45 @@ public static function isTokenInsideFunctionDefinitionArgumentList(File $phpcsFi
105105 *
106106 * Does not work for tokens inside the "use".
107107 *
108+ * Will also work for the parenthesis that make up the function definition's arguments list.
109+ *
108110 * @param File $phpcsFile
109111 * @param int $stackPtr
110112 *
111113 * @return ?int
112114 */
113115 public static function getFunctionIndexForFunctionArgument (File $ phpcsFile , $ stackPtr ) {
114116 $ tokens = $ phpcsFile ->getTokens ();
117+ $ token = $ tokens [$ stackPtr ];
118+ if ($ token ['code ' ] === 'PHPCS_T_OPEN_PARENTHESIS ' ) {
119+ $ startOfArguments = $ stackPtr ;
120+ } elseif ($ token ['code ' ] === 'PHPCS_T_CLOSE_PARENTHESIS ' ) {
121+ if (empty ($ token ['parenthesis_opener ' ])) {
122+ return null ;
123+ }
124+ $ startOfArguments = $ token ['parenthesis_opener ' ];
125+ } else {
126+ if (empty ($ token ['nested_parenthesis ' ])) {
127+ return null ;
128+ }
129+ $ startingParenthesis = array_keys ($ token ['nested_parenthesis ' ]);
130+ $ startOfArguments = end ($ startingParenthesis );
131+ }
115132
116133 $ nonFunctionTokenTypes = array_values (Tokens::$ emptyTokens );
117- $ nonFunctionTokenTypes [] = T_OPEN_PARENTHESIS ;
118- $ nonFunctionTokenTypes [] = T_VARIABLE ;
119- $ nonFunctionTokenTypes [] = T_ELLIPSIS ;
120- $ nonFunctionTokenTypes [] = T_COMMA ;
121134 $ nonFunctionTokenTypes [] = T_STRING ;
122135 $ nonFunctionTokenTypes [] = T_BITWISE_AND ;
123- $ nonFunctionTokenTypes [] = T_NS_SEPARATOR ;
124- $ functionPtr = self ::getIntOrNull ($ phpcsFile ->findPrevious ($ nonFunctionTokenTypes , $ stackPtr - 1 , null , true , null , true ));
136+ $ functionPtr = self ::getIntOrNull ($ phpcsFile ->findPrevious ($ nonFunctionTokenTypes , $ startOfArguments - 1 , null , true , null , true ));
125137 if (! is_int ($ functionPtr )) {
126138 return null ;
127139 }
140+ $ functionToken = $ tokens [$ functionPtr ];
128141
129142 $ functionTokenTypes = [
130143 T_FUNCTION ,
131144 T_CLOSURE ,
132145 ];
133- if (!in_array ($ tokens [ $ functionPtr ] ['code ' ], $ functionTokenTypes , true ) && ! FunctionDeclarations::isArrowFunction ($ phpcsFile , $ functionPtr )) {
146+ if (!in_array ($ functionToken ['code ' ], $ functionTokenTypes , true ) && ! FunctionDeclarations::isArrowFunction ($ phpcsFile , $ functionPtr )) {
134147 return null ;
135148 }
136149 return $ functionPtr ;
0 commit comments