Skip to content

Commit 60130ec

Browse files
authored
[Php81] Skip ArrayToFirstClassCallableRector when the arg type does not accept a Closure (#8288)
1 parent a693af0 commit 60130ec

8 files changed

Lines changed: 226 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;
4+
5+
use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeObjectWithArrayArgument;
6+
7+
final class CallableParam
8+
{
9+
public function run(SomeObjectWithArrayArgument $someObjectWithArrayArgument)
10+
{
11+
$someObjectWithArrayArgument->withCallable([$this, 'name']);
12+
}
13+
14+
public function name()
15+
{
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;
24+
25+
use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeObjectWithArrayArgument;
26+
27+
final class CallableParam
28+
{
29+
public function run(SomeObjectWithArrayArgument $someObjectWithArrayArgument)
30+
{
31+
$someObjectWithArrayArgument->withCallable($this->name(...));
32+
}
33+
34+
public function name()
35+
{
36+
}
37+
}
38+
39+
?>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;
4+
5+
use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeObjectWithArrayArgument;
6+
7+
final class SkipArrayParam
8+
{
9+
public function run(SomeObjectWithArrayArgument $someObjectWithArrayArgument)
10+
{
11+
$someObjectWithArrayArgument->withArray([$this, 'name']);
12+
13+
SomeObjectWithArrayArgument::staticWithArray([$this, 'name']);
14+
}
15+
16+
public function name()
17+
{
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;
4+
5+
use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeObjectWithArrayArgument;
6+
7+
final class SkipUnionArrayParam
8+
{
9+
public function run()
10+
{
11+
return new SomeObjectWithArrayArgument([$this, 'name']);
12+
}
13+
14+
public function name()
15+
{
16+
}
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source;
5+
6+
final class SomeObjectWithArrayArgument
7+
{
8+
public function __construct(
9+
public string|array|SomeExternalObject|null $argument
10+
) {
11+
}
12+
13+
public function withArray(array $argument): void
14+
{
15+
}
16+
17+
public function withCallable(callable $argument): void
18+
{
19+
}
20+
21+
public static function staticWithArray(array $argument): void
22+
{
23+
}
24+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public function getNodeTypes(): array
9090
*/
9191
public function refactor(Node $node): StaticCall|MethodCall|null
9292
{
93+
if ($node->getAttribute(AttributeKey::IS_ARG_NOT_ACCEPTING_CLOSURE)) {
94+
return null;
95+
}
96+
9397
if ($node->getAttribute(AttributeKey::IS_INSIDE_SYMFONY_PHP_CLOSURE)) {
9498
return null;
9599
}

‎src/DependencyInjection/LazyContainerFactory.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
use Rector\PhpParser\Node\NodeFactory;
118118
use Rector\PhpParser\NodeTraverser\RectorNodeTraverser;
119119
use Rector\PhpParser\NodeVisitor\ArgNodeVisitor;
120+
use Rector\PhpParser\NodeVisitor\ArgNotAcceptingClosureNodeVisitor;
120121
use Rector\PhpParser\NodeVisitor\AssignedToNodeVisitor;
121122
use Rector\PhpParser\NodeVisitor\ByRefReturnNodeVisitor;
122123
use Rector\PhpParser\NodeVisitor\ByRefVariableNodeVisitor;
@@ -250,6 +251,7 @@ final class LazyContainerFactory
250251
ParamDefaultNodeVisitor::class,
251252
ClassConstFetchNodeVisitor::class,
252253
CallLikeThisBoundClosureArgsNodeVisitor::class,
254+
ArgNotAcceptingClosureNodeVisitor::class,
253255
];
254256

255257
/**

‎src/NodeTypeResolver/Node/AttributeKey.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,10 @@ final class AttributeKey
176176
public const string IS_IN_TRY_BLOCK = 'is_in_try_block';
177177

178178
public const string NEWLINE_ON_FLUENT_CALL = 'newline_on_fluent_call';
179+
180+
/**
181+
* The arg value is passed to a parameter whose type does not accept a Closure,
182+
* e.g. an array callable passed to a "string|array|null" parameter
183+
*/
184+
public const string IS_ARG_NOT_ACCEPTING_CLOSURE = 'is_arg_not_accepting_closure';
179185
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PhpParser\NodeVisitor;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\Array_;
10+
use PhpParser\Node\Expr\CallLike;
11+
use PhpParser\Node\Identifier;
12+
use PhpParser\NodeVisitorAbstract;
13+
use PHPStan\Reflection\FunctionReflection;
14+
use PHPStan\Reflection\MethodReflection;
15+
use PHPStan\Reflection\ParameterReflection;
16+
use PHPStan\Type\ObjectType;
17+
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
18+
use Rector\NodeTypeResolver\Node\AttributeKey;
19+
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
20+
use Rector\PHPStan\ScopeFetcher;
21+
use Rector\Reflection\ReflectionResolver;
22+
23+
/**
24+
* Mark array arguments passed to a parameter that cannot hold a Closure,
25+
* e.g. "array" or "string|array|null", so an array callable is kept as is
26+
*
27+
* @see https://github.com/rectorphp/rector/issues/9563
28+
*/
29+
final class ArgNotAcceptingClosureNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
30+
{
31+
public function __construct(
32+
private readonly ReflectionResolver $reflectionResolver
33+
) {
34+
}
35+
36+
public function enterNode(Node $node): ?Node
37+
{
38+
if (! $node instanceof CallLike) {
39+
return null;
40+
}
41+
42+
if ($node->isFirstClassCallable()) {
43+
return null;
44+
}
45+
46+
$args = $node->getArgs();
47+
if (! array_any($args, static fn (Arg $arg): bool => $arg->value instanceof Array_)) {
48+
return null;
49+
}
50+
51+
$functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
52+
if (! $functionLikeReflection instanceof FunctionReflection && ! $functionLikeReflection instanceof MethodReflection) {
53+
return null;
54+
}
55+
56+
$parameterReflections = ParametersAcceptorSelectorVariantsWrapper::select(
57+
$functionLikeReflection,
58+
$node,
59+
ScopeFetcher::fetch($node)
60+
)->getParameters();
61+
62+
$closureObjectType = new ObjectType('Closure');
63+
64+
foreach ($args as $position => $arg) {
65+
if (! $arg->value instanceof Array_) {
66+
continue;
67+
}
68+
69+
$parameterReflection = $this->matchParameterReflection($arg, $position, $parameterReflections);
70+
if (! $parameterReflection instanceof ParameterReflection) {
71+
continue;
72+
}
73+
74+
if (! $parameterReflection->getType()->accepts($closureObjectType, true)->no()) {
75+
continue;
76+
}
77+
78+
$arg->value->setAttribute(AttributeKey::IS_ARG_NOT_ACCEPTING_CLOSURE, true);
79+
}
80+
81+
return $node;
82+
}
83+
84+
/**
85+
* @param ParameterReflection[] $parameterReflections
86+
*/
87+
private function matchParameterReflection(
88+
Arg $arg,
89+
int $position,
90+
array $parameterReflections
91+
): ?ParameterReflection {
92+
if ($arg->name instanceof Identifier) {
93+
$argName = $arg->name->toString();
94+
95+
foreach ($parameterReflections as $parameterReflection) {
96+
if ($parameterReflection->getName() === $argName) {
97+
return $parameterReflection;
98+
}
99+
}
100+
101+
return null;
102+
}
103+
104+
if (isset($parameterReflections[$position])) {
105+
return $parameterReflections[$position];
106+
}
107+
108+
$lastParameterReflection = end($parameterReflections);
109+
if ($lastParameterReflection instanceof ParameterReflection && $lastParameterReflection->isVariadic()) {
110+
return $lastParameterReflection;
111+
}
112+
113+
return null;
114+
}
115+
}

0 commit comments

Comments
 (0)