Skip to content

Commit 30cd83a

Browse files
authored
[Php81] Skip array returned as array type in ArrayToFirstClassCallableRector (#9035)
1 parent 9c8e623 commit 30cd83a

7 files changed

Lines changed: 173 additions & 6 deletions

File tree

rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/static_with_other_object_const_fetch.php.inc renamed to rules-tests/Php81/Rector/Array_/ArrayToFirstClassCallableRector/Fixture/return_array_callable_union.php.inc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixtu
44

55
use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject;
66

7-
final class StaticWithOtherObjectConstFetch
7+
final class ReturnArrayCallableUnion
88
{
9-
public function provideListenerPriority(): array
9+
public function provideListener(): array|callable
1010
{
11-
return [SomeExternalObject::class, 'sleepStatic'];
11+
return [SomeExternalObject::class, 'sleepStatic'];
1212
}
1313
}
1414

@@ -20,11 +20,11 @@ namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixtu
2020

2121
use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject;
2222

23-
final class StaticWithOtherObjectConstFetch
23+
final class ReturnArrayCallableUnion
2424
{
25-
public function provideListenerPriority(): array
25+
public function provideListener(): array|callable
2626
{
27-
return SomeExternalObject::sleepStatic(...);
27+
return SomeExternalObject::sleepStatic(...);
2828
}
2929
}
3030

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;
4+
5+
use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject;
6+
7+
final class ReturnInUntypedMethod
8+
{
9+
public function provideListener()
10+
{
11+
return [SomeExternalObject::class, 'sleepStatic'];
12+
}
13+
14+
public function provideNestedClosure(): array
15+
{
16+
return [
17+
'listener' => function () {
18+
return [SomeExternalObject::class, 'sleepStatic'];
19+
},
20+
];
21+
}
22+
}
23+
24+
?>
25+
-----
26+
<?php
27+
28+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;
29+
30+
use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject;
31+
32+
final class ReturnInUntypedMethod
33+
{
34+
public function provideListener()
35+
{
36+
return SomeExternalObject::sleepStatic(...);
37+
}
38+
39+
public function provideNestedClosure(): array
40+
{
41+
return [
42+
'listener' => function () {
43+
return SomeExternalObject::sleepStatic(...);
44+
},
45+
];
46+
}
47+
}
48+
49+
?>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;
4+
5+
use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject;
6+
7+
final class SkipReturnArrayTypes
8+
{
9+
public function provideIterable(): iterable
10+
{
11+
return [SomeExternalObject::class, 'sleepStatic'];
12+
}
13+
14+
public function provideNullable(): ?array
15+
{
16+
return [SomeExternalObject::class, 'sleepStatic'];
17+
}
18+
19+
public function provideUnion(): array|string
20+
{
21+
return [SomeExternalObject::class, 'sleepStatic'];
22+
}
23+
24+
public function provideClosures()
25+
{
26+
$closure = function (): array {
27+
return [SomeExternalObject::class, 'sleepStatic'];
28+
};
29+
30+
$arrowFunction = fn (): array => [SomeExternalObject::class, 'sleepStatic'];
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;
4+
5+
use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject;
6+
7+
final class SkipStaticWithOtherObjectConstFetchReturnedAsArray
8+
{
9+
public function provideListenerPriority(): array
10+
{
11+
return [SomeExternalObject::class, 'sleepStatic'];
12+
}
13+
}
14+
15+
?>

‎rules/Php81/Rector/Array_/ArrayToFirstClassCallableRector.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public function refactor(Node $node): StaticCall|MethodCall|null
103103
return null;
104104
}
105105

106+
if ($node->getAttribute(AttributeKey::IS_RETURN_NOT_ACCEPTING_CLOSURE)) {
107+
return null;
108+
}
109+
106110
if ($node->getAttribute(AttributeKey::IS_ARRAY_AS_STRING_CALLABLE)) {
107111
return null;
108112
}

‎src/NodeTypeResolver/Node/AttributeKey.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ final class AttributeKey
175175
*/
176176
public const string IS_ARG_NOT_ACCEPTING_CLOSURE = 'is_arg_not_accepting_closure';
177177

178+
/**
179+
* The array is returned from a function-like whose native return type does not accept a Closure,
180+
* e.g. a "return [$this, 'method'];" in a method returning "array"
181+
*/
182+
public const string IS_RETURN_NOT_ACCEPTING_CLOSURE = 'is_return_not_accepting_closure';
183+
178184
/**
179185
* Closure or arrow function with rebound $this, e.g. (fn () => ...)->call($object)
180186
*/

‎src/PhpParser/NodeVisitor/ContextNodeVisitor.php‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use PhpParser\Node\Arg;
99
use PhpParser\Node\ArrayItem;
1010
use PhpParser\Node\Attribute;
11+
use PhpParser\Node\ComplexType;
1112
use PhpParser\Node\Expr\Array_;
1213
use PhpParser\Node\Expr\ArrayDimFetch;
14+
use PhpParser\Node\Expr\ArrowFunction;
1315
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
1416
use PhpParser\Node\Expr\Closure;
1517
use PhpParser\Node\Expr\Isset_;
@@ -23,7 +25,9 @@
2325
use PhpParser\Node\Expr\Variable;
2426
use PhpParser\Node\FunctionLike;
2527
use PhpParser\Node\Identifier;
28+
use PhpParser\Node\Name;
2629
use PhpParser\Node\Name\FullyQualified;
30+
use PhpParser\Node\NullableType;
2731
use PhpParser\Node\Param;
2832
use PhpParser\Node\Scalar\String_;
2933
use PhpParser\Node\Stmt\Break_;
@@ -36,10 +40,12 @@
3640
use PhpParser\Node\Stmt\For_;
3741
use PhpParser\Node\Stmt\Foreach_;
3842
use PhpParser\Node\Stmt\If_;
43+
use PhpParser\Node\Stmt\Return_;
3944
use PhpParser\Node\Stmt\Switch_;
4045
use PhpParser\Node\Stmt\TryCatch;
4146
use PhpParser\Node\Stmt\Unset_;
4247
use PhpParser\Node\Stmt\While_;
48+
use PhpParser\Node\UnionType;
4349
use PhpParser\NodeVisitorAbstract;
4450
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
4551
use 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

Comments
 (0)