@@ -146,13 +146,32 @@ public function refactor(Node $node): ?Class_
146146 return null ;
147147 }
148148
149- if (! $ this ->isName ($ node ->name , self ::WILL_RETURN_CALLBACK )) {
149+ $ innerClosure = null ;
150+
151+ if ($ this ->isName ($ node ->name , 'with ' )) {
152+ // special case for nested callback
153+ $ withFirstArg = $ node ->getArgs ()[0 ];
154+
155+ if ($ withFirstArg ->value instanceof MethodCall) {
156+ $ nestedMethodCall = $ withFirstArg ->value ;
157+ if ($ this ->isName ($ nestedMethodCall ->name , 'callback ' )) {
158+ $ innerClosure = $ nestedMethodCall ->getArgs ()[0 ]
159+ ->value ;
160+ }
161+ }
162+ } elseif ($ this ->isName ($ node ->name , self ::WILL_RETURN_CALLBACK )) {
163+ $ innerArg = $ node ->getArgs ()[0 ];
164+ if (! $ innerArg ->value instanceof ArrowFunction && ! $ innerArg ->value instanceof Closure) {
165+ return null ;
166+ }
167+
168+ $ innerClosure = $ innerArg ->value ;
169+ } else {
150170 return null ;
151171 }
152172
153- $ innerArg = $ node ->getArgs ()[0 ]
154- ->value ;
155- if (! $ innerArg instanceof ArrowFunction && ! $ innerArg instanceof Closure) {
173+ // no match
174+ if (! $ innerClosure instanceof Node \FunctionLike) {
156175 return null ;
157176 }
158177
@@ -205,7 +224,7 @@ public function refactor(Node $node): ?Class_
205224 return null ;
206225 }
207226
208- foreach ($ innerArg ->params as $ key => $ param ) {
227+ foreach ($ innerClosure ->params as $ key => $ param ) {
209228 // avoid typing variadic parameters
210229 if ($ param ->variadic ) {
211230 continue ;
@@ -239,14 +258,14 @@ public function refactor(Node $node): ?Class_
239258 $ hasChanged = true ;
240259 }
241260
242- if (! $ innerArg ->returnType instanceof Node) {
261+ if (! $ innerClosure ->returnType instanceof Node) {
243262 $ returnTypeNode = $ this ->staticTypeMapper ->mapPHPStanTypeToPhpParserNode (
244263 $ parameterTypesAndReturnType ->getReturnType (),
245264 TypeKind::RETURN
246265 );
247266
248267 if ($ returnTypeNode instanceof Node) {
249- $ innerArg ->returnType = $ returnTypeNode ;
268+ $ innerClosure ->returnType = $ returnTypeNode ;
250269 $ hasChanged = true ;
251270 }
252271 }
0 commit comments