From 7b9192ff1eba8e763a67303f277b43a059a28640 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 23 Nov 2026 22:47:22 +0100 Subject: [PATCH] [Php81] Skip array returned as array type in ArrayToFirstClassCallableRector --- ...nc => return_array_callable_union.php.inc} | 12 ++-- .../Fixture/return_in_untyped_method.php.inc | 49 +++++++++++++++ .../Fixture/skip_return_array_types.php.inc | 32 ++++++++++ ...ject_const_fetch_returned_as_array.php.inc | 15 +++++ .../ArrayToFirstClassCallableRector.php | 4 ++ src/NodeTypeResolver/Node/AttributeKey.php | 6 ++ .../NodeVisitor/ContextNodeVisitor.php | 61 +++++++++++++++++++ 7 files changed, 173 insertions(+), 6 deletions(-) rename rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/{static_with_other_object_const_fetch.php.inc => return_array_callable_union.php.inc} (57%) create mode 100644 rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/return_in_untyped_method.php.inc create mode 100644 rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/skip_return_array_types.php.inc create mode 100644 rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/skip_static_with_other_object_const_fetch_returned_as_array.php.inc diff --git a/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/static_with_other_object_const_fetch.php.inc b/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/return_array_callable_union.php.inc similarity index 57% rename from rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/static_with_other_object_const_fetch.php.inc rename to rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/return_array_callable_union.php.inc index 0bb4efb661d..ee9db091b72 100644 --- a/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/static_with_other_object_const_fetch.php.inc +++ b/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/return_array_callable_union.php.inc @@ -4,11 +4,11 @@ namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixtu use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject; -final class StaticWithOtherObjectConstFetch +final class ReturnArrayCallableUnion { - public function provideListenerPriority(): array + public function provideListener(): array|callable { - return [SomeExternalObject::class, 'sleepStatic']; + return [SomeExternalObject::class, 'sleepStatic']; } } @@ -20,11 +20,11 @@ namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixtu use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject; -final class StaticWithOtherObjectConstFetch +final class ReturnArrayCallableUnion { - public function provideListenerPriority(): array + public function provideListener(): array|callable { - return SomeExternalObject::sleepStatic(...); + return SomeExternalObject::sleepStatic(...); } } diff --git a/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/return_in_untyped_method.php.inc b/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/return_in_untyped_method.php.inc new file mode 100644 index 00000000000..17ceaf7cf47 --- /dev/null +++ b/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/return_in_untyped_method.php.inc @@ -0,0 +1,49 @@ + function () { + return [SomeExternalObject::class, 'sleepStatic']; + }, + ]; + } +} + +?> +----- + function () { + return SomeExternalObject::sleepStatic(...); + }, + ]; + } +} + +?> diff --git a/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/skip_return_array_types.php.inc b/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/skip_return_array_types.php.inc new file mode 100644 index 00000000000..018ee427d0a --- /dev/null +++ b/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/skip_return_array_types.php.inc @@ -0,0 +1,32 @@ + [SomeExternalObject::class, 'sleepStatic']; + } +} diff --git a/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/skip_static_with_other_object_const_fetch_returned_as_array.php.inc b/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/skip_static_with_other_object_const_fetch_returned_as_array.php.inc new file mode 100644 index 00000000000..1168f76b40f --- /dev/null +++ b/rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/skip_static_with_other_object_const_fetch_returned_as_array.php.inc @@ -0,0 +1,15 @@ + diff --git a/rules/Php81/Rector/Array_/ArrayToFirstClassCallableRector.php b/rules/Php81/Rector/Array_/ArrayToFirstClassCallableRector.php index 0d1e6fc19a6..841db3ecd08 100644 --- a/rules/Php81/Rector/Array_/ArrayToFirstClassCallableRector.php +++ b/rules/Php81/Rector/Array_/ArrayToFirstClassCallableRector.php @@ -103,6 +103,10 @@ public function refactor(Node $node): StaticCall|MethodCall|null return null; } + if ($node->getAttribute(AttributeKey::IS_RETURN_NOT_ACCEPTING_CLOSURE)) { + return null; + } + if ($node->getAttribute(AttributeKey::IS_ARRAY_AS_STRING_CALLABLE)) { return null; } diff --git a/src/NodeTypeResolver/Node/AttributeKey.php b/src/NodeTypeResolver/Node/AttributeKey.php index 06263ee24d1..e5bd13fd675 100644 --- a/src/NodeTypeResolver/Node/AttributeKey.php +++ b/src/NodeTypeResolver/Node/AttributeKey.php @@ -175,6 +175,12 @@ final class AttributeKey */ public const string IS_ARG_NOT_ACCEPTING_CLOSURE = 'is_arg_not_accepting_closure'; + /** + * The array is returned from a function-like whose native return type does not accept a Closure, + * e.g. a "return [$this, 'method'];" in a method returning "array" + */ + public const string IS_RETURN_NOT_ACCEPTING_CLOSURE = 'is_return_not_accepting_closure'; + /** * Closure or arrow function with rebound $this, e.g. (fn () => ...)->call($object) */ diff --git a/src/PhpParser/NodeVisitor/ContextNodeVisitor.php b/src/PhpParser/NodeVisitor/ContextNodeVisitor.php index b3ec45d146b..4ce89b35e75 100644 --- a/src/PhpParser/NodeVisitor/ContextNodeVisitor.php +++ b/src/PhpParser/NodeVisitor/ContextNodeVisitor.php @@ -8,8 +8,10 @@ use PhpParser\Node\Arg; use PhpParser\Node\ArrayItem; use PhpParser\Node\Attribute; +use PhpParser\Node\ComplexType; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayDimFetch; +use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\Isset_; @@ -23,7 +25,9 @@ use PhpParser\Node\Expr\Variable; use PhpParser\Node\FunctionLike; use PhpParser\Node\Identifier; +use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; +use PhpParser\Node\NullableType; use PhpParser\Node\Param; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Break_; @@ -36,10 +40,12 @@ use PhpParser\Node\Stmt\For_; use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; +use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; use PhpParser\Node\Stmt\TryCatch; use PhpParser\Node\Stmt\Unset_; use PhpParser\Node\Stmt\While_; +use PhpParser\Node\UnionType; use PhpParser\NodeVisitorAbstract; use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface; use Rector\NodeTypeResolver\Node\AttributeKey; @@ -61,6 +67,13 @@ final class ContextNodeVisitor extends NodeVisitorAbstract implements Decorating */ private array $outerLoopAndTryDepths = []; + /** + * Whether the enclosing function-like declares a native return type not accepting a Closure + * + * @var bool[] + */ + private array $returnsArrayStack = []; + /** * @param Node[] $nodes */ @@ -70,6 +83,7 @@ public function beforeTraverse(array $nodes): null $this->tryDepth = 0; $this->attributeDepth = 0; $this->outerLoopAndTryDepths = []; + $this->returnsArrayStack = []; return null; } @@ -84,6 +98,15 @@ public function enterNode(Node $node): ?Node return null; } + if ($node instanceof FunctionLike) { + $this->processReturnTypeArray($node); + } + + if ($node instanceof Return_ && $node->expr instanceof Array_ && end($this->returnsArrayStack)) { + $node->expr->setAttribute(AttributeKey::IS_RETURN_NOT_ACCEPTING_CLOSURE, true); + return null; + } + if ($node instanceof ArrayDimFetch) { $this->processInsideArrayDimFetch($node); return null; @@ -181,6 +204,10 @@ public function leaveNode(Node $node): null return null; } + if ($node instanceof FunctionLike) { + array_pop($this->returnsArrayStack); + } + if ($node instanceof Class_ || $node instanceof FunctionLike) { [$this->loopDepth, $this->tryDepth] = array_pop($this->outerLoopAndTryDepths) ?? [0, 0]; } @@ -219,6 +246,40 @@ private function decorateEnclosingContext(Node $node): void } } + private function processReturnTypeArray(FunctionLike $functionLike): void + { + $returnsArray = $this->isArrayNotAcceptingClosureType($functionLike->getReturnType()); + $this->returnsArrayStack[] = $returnsArray; + + if ($returnsArray && $functionLike instanceof ArrowFunction && $functionLike->expr instanceof Array_) { + $functionLike->expr->setAttribute(AttributeKey::IS_RETURN_NOT_ACCEPTING_CLOSURE, true); + } + } + + private function isArrayNotAcceptingClosureType(Identifier|Name|ComplexType|null $type): bool + { + if ($type instanceof NullableType) { + $type = $type->type; + } + + $types = $type instanceof UnionType ? $type->types : [$type]; + + $typeNames = []; + foreach ($types as $singleType) { + if (! $singleType instanceof Identifier && ! $singleType instanceof Name) { + return false; + } + + $typeNames[] = strtolower($singleType->toString()); + } + + if (array_intersect($typeNames, ['callable', 'closure', 'mixed', 'object']) !== []) { + return false; + } + + return array_intersect($typeNames, ['array', 'iterable']) !== []; + } + private function processInsideArrayDimFetch(ArrayDimFetch $arrayDimFetch): void { if ($arrayDimFetch->var instanceof PropertyFetch || $arrayDimFetch->var instanceof StaticPropertyFetch) {