88use PhpParser \Node \Arg ;
99use PhpParser \Node \ArrayItem ;
1010use PhpParser \Node \Attribute ;
11+ use PhpParser \Node \ComplexType ;
1112use PhpParser \Node \Expr \Array_ ;
1213use PhpParser \Node \Expr \ArrayDimFetch ;
14+ use PhpParser \Node \Expr \ArrowFunction ;
1315use PhpParser \Node \Expr \BinaryOp \BooleanAnd ;
1416use PhpParser \Node \Expr \Closure ;
1517use PhpParser \Node \Expr \Isset_ ;
2325use PhpParser \Node \Expr \Variable ;
2426use PhpParser \Node \FunctionLike ;
2527use PhpParser \Node \Identifier ;
28+ use PhpParser \Node \Name ;
2629use PhpParser \Node \Name \FullyQualified ;
30+ use PhpParser \Node \NullableType ;
2731use PhpParser \Node \Param ;
2832use PhpParser \Node \Scalar \String_ ;
2933use PhpParser \Node \Stmt \Break_ ;
3640use PhpParser \Node \Stmt \For_ ;
3741use PhpParser \Node \Stmt \Foreach_ ;
3842use PhpParser \Node \Stmt \If_ ;
43+ use PhpParser \Node \Stmt \Return_ ;
3944use PhpParser \Node \Stmt \Switch_ ;
4045use PhpParser \Node \Stmt \TryCatch ;
4146use PhpParser \Node \Stmt \Unset_ ;
4247use PhpParser \Node \Stmt \While_ ;
48+ use PhpParser \Node \UnionType ;
4349use PhpParser \NodeVisitorAbstract ;
4450use Rector \Contract \PhpParser \DecoratingNodeVisitorInterface ;
4551use Rector \NodeTypeResolver \Node \AttributeKey ;
@@ -61,6 +67,13 @@ final class ContextNodeVisitor extends NodeVisitorAbstract implements Decorating
6167 */
6268 private array $ outerLoopAndTryDepths = [];
6369
70+ /**
71+ * Whether the enclosing function-like declares a native return type not accepting a Closure
72+ *
73+ * @var bool[]
74+ */
75+ private array $ returnsArrayStack = [];
76+
6477 /**
6578 * @param Node[] $nodes
6679 */
@@ -70,6 +83,7 @@ public function beforeTraverse(array $nodes): null
7083 $ this ->tryDepth = 0 ;
7184 $ this ->attributeDepth = 0 ;
7285 $ this ->outerLoopAndTryDepths = [];
86+ $ this ->returnsArrayStack = [];
7387
7488 return null ;
7589 }
@@ -84,6 +98,15 @@ public function enterNode(Node $node): ?Node
8498 return null ;
8599 }
86100
101+ if ($ node instanceof FunctionLike) {
102+ $ this ->processReturnTypeArray ($ node );
103+ }
104+
105+ if ($ node instanceof Return_ && $ node ->expr instanceof Array_ && end ($ this ->returnsArrayStack )) {
106+ $ node ->expr ->setAttribute (AttributeKey::IS_RETURN_NOT_ACCEPTING_CLOSURE , true );
107+ return null ;
108+ }
109+
87110 if ($ node instanceof ArrayDimFetch) {
88111 $ this ->processInsideArrayDimFetch ($ node );
89112 return null ;
@@ -181,6 +204,10 @@ public function leaveNode(Node $node): null
181204 return null ;
182205 }
183206
207+ if ($ node instanceof FunctionLike) {
208+ array_pop ($ this ->returnsArrayStack );
209+ }
210+
184211 if ($ node instanceof Class_ || $ node instanceof FunctionLike) {
185212 [$ this ->loopDepth , $ this ->tryDepth ] = array_pop ($ this ->outerLoopAndTryDepths ) ?? [0 , 0 ];
186213 }
@@ -219,6 +246,40 @@ private function decorateEnclosingContext(Node $node): void
219246 }
220247 }
221248
249+ private function processReturnTypeArray (FunctionLike $ functionLike ): void
250+ {
251+ $ returnsArray = $ this ->isArrayNotAcceptingClosureType ($ functionLike ->getReturnType ());
252+ $ this ->returnsArrayStack [] = $ returnsArray ;
253+
254+ if ($ returnsArray && $ functionLike instanceof ArrowFunction && $ functionLike ->expr instanceof Array_) {
255+ $ functionLike ->expr ->setAttribute (AttributeKey::IS_RETURN_NOT_ACCEPTING_CLOSURE , true );
256+ }
257+ }
258+
259+ private function isArrayNotAcceptingClosureType (Identifier |Name |ComplexType |null $ type ): bool
260+ {
261+ if ($ type instanceof NullableType) {
262+ $ type = $ type ->type ;
263+ }
264+
265+ $ types = $ type instanceof UnionType ? $ type ->types : [$ type ];
266+
267+ $ typeNames = [];
268+ foreach ($ types as $ singleType ) {
269+ if (! $ singleType instanceof Identifier && ! $ singleType instanceof Name) {
270+ return false ;
271+ }
272+
273+ $ typeNames [] = strtolower ($ singleType ->toString ());
274+ }
275+
276+ if (array_intersect ($ typeNames , ['callable ' , 'closure ' , 'mixed ' , 'object ' ]) !== []) {
277+ return false ;
278+ }
279+
280+ return array_intersect ($ typeNames , ['array ' , 'iterable ' ]) !== [];
281+ }
282+
222283 private function processInsideArrayDimFetch (ArrayDimFetch $ arrayDimFetch ): void
223284 {
224285 if ($ arrayDimFetch ->var instanceof PropertyFetch || $ arrayDimFetch ->var instanceof StaticPropertyFetch) {
0 commit comments