@@ -125,34 +125,24 @@ public function refactor(Node $node): MethodCall|null
125125 continue ;
126126 }
127127
128- $ assertExpressions = $ this ->fromBinaryAndAssertExpressionsFactory ->create ($ joinedExprs );
129- if ($ assertExpressions === []) {
128+ $ assertExprStmts = $ this ->fromBinaryAndAssertExpressionsFactory ->create ($ joinedExprs );
129+ if ($ assertExprStmts === []) {
130130 continue ;
131131 }
132132
133133 $ nonReturnCallbackStmts = $ this ->resolveNonReturnCallbackStmts ($ argAndFunctionLike );
134134
135135 // last si return true;
136- $ assertExpressions [] = new Return_ ($ this ->nodeFactory ->createTrue ());
137-
136+ $ assertExprStmts [] = new Return_ ($ this ->nodeFactory ->createTrue ());
138137 $ innerFunctionLike = $ argAndFunctionLike ->getFunctionLike ();
139138
140139 if ($ innerFunctionLike instanceof Closure) {
141- $ innerFunctionLike ->stmts = array_merge ($ nonReturnCallbackStmts , $ assertExpressions );
140+ $ innerFunctionLike ->stmts = array_merge ($ nonReturnCallbackStmts , $ assertExprStmts );
142141 } else {
143142 // arrow function -> flip to closure
144143 $ functionLikeInArg = $ argAndFunctionLike ->getArg ();
145144
146- $ externalVariables = $ this ->closureUsesResolver ->resolveFromArrowFunction ($ innerFunctionLike );
147-
148- $ closure = new Closure ([
149- 'params ' => $ argAndFunctionLike ->getFunctionLike ()
150- ->params ,
151- 'stmts ' => $ assertExpressions ,
152- 'returnType ' => new Identifier ('bool ' ),
153- 'uses ' => $ externalVariables ,
154- ]);
155-
145+ $ closure = $ this ->createClosure ($ innerFunctionLike , $ argAndFunctionLike , $ assertExprStmts );
156146 $ functionLikeInArg ->value = $ closure ;
157147 }
158148
@@ -273,4 +263,23 @@ private function resolveNonReturnCallbackStmts(ArgAndFunctionLike $argAndFunctio
273263
274264 return [];
275265 }
266+
267+ /**
268+ * @param Stmt[] $assertExprStmts
269+ */
270+ private function createClosure (
271+ ArrowFunction $ arrowFunction ,
272+ ArgAndFunctionLike $ argAndFunctionLike ,
273+ array $ assertExprStmts
274+ ): Closure {
275+ $ externalVariables = $ this ->closureUsesResolver ->resolveFromArrowFunction ($ arrowFunction );
276+
277+ return new Closure ([
278+ 'params ' => $ argAndFunctionLike ->getFunctionLike ()
279+ ->params ,
280+ 'stmts ' => $ assertExprStmts ,
281+ 'returnType ' => new Identifier ('bool ' ),
282+ 'uses ' => $ externalVariables ,
283+ ]);
284+ }
276285}
0 commit comments