Repository navigation
Expand file tree
/
Copy pathCallLikeReflectionNodeVisitor.php
More file actions
194 lines (162 loc) · 6.31 KB
/
Copy pathCallLikeReflectionNodeVisitor.php
File metadata and controls
194 lines (162 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
declare(strict_types=1);
namespace Rector\PhpParser\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Identifier;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\CallableType;
use PHPStan\Type\ObjectType;
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\PHPStan\ScopeFetcher;
use Rector\Reflection\ReflectionResolver;
/**
* Decorates call args using the resolved function-like reflection, resolved once per call:
* - Closure/arrow-fn args passed to a variadic callable parameter (HAS_CLOSURE_WITH_VARIADIC_ARGS)
* - 2-item array args passed to a parameter that cannot hold a Closure, so an array callable is kept as is
* (IS_ARG_NOT_ACCEPTING_CLOSURE), see https://github.com/rectorphp/rector/issues/9563
*/
final class CallLikeReflectionNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
{
public function __construct(
private readonly ReflectionResolver $reflectionResolver
) {
}
public function enterNode(Node $node): ?Node
{
if (! $node instanceof CallLike) {
return null;
}
if ($node->isFirstClassCallable()) {
return null;
}
$args = $node->getArgs();
if ($args === []) {
return null;
}
// only closure/arrow-fn or array callable shaped args are decorated below; skip the reflection otherwise
if (! $this->hasClosureArrowOrArrayCallableArg($args)) {
return null;
}
$functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
$this->decorateClosureWithVariadicArgs($args, $functionLikeReflection);
$this->decorateArgNotAcceptingClosure($node, $args, $functionLikeReflection);
return null;
}
/**
* @param Arg[] $args
*/
private function hasClosureArrowOrArrayCallableArg(array $args): bool
{
return array_any(
$args,
fn (Arg $arg): bool => $arg->value instanceof Closure
|| $arg->value instanceof ArrowFunction
|| $this->isArrayCallableShape($arg->value)
);
}
/**
* Only [$caller, 'method'] can be an array callable
*/
private function isArrayCallableShape(Expr $expr): bool
{
return $expr instanceof Array_ && count($expr->items) === 2;
}
/**
* @param Arg[] $args
*/
private function decorateClosureWithVariadicArgs(
array $args,
MethodReflection|FunctionReflection|null $functionLikeReflection
): void {
foreach ($args as $arg) {
if (! $arg->value instanceof Closure && ! $arg->value instanceof ArrowFunction) {
continue;
}
if ($functionLikeReflection instanceof NativeFunctionReflection) {
$parametersAcceptors = ParametersAcceptorSelector::combineAcceptors(
$functionLikeReflection->getVariants()
);
foreach ($parametersAcceptors->getParameters() as $extendedParameterReflection) {
if ($extendedParameterReflection->getType() instanceof CallableType && $extendedParameterReflection->getType()->isVariadic()) {
$arg->value->setAttribute(AttributeKey::HAS_CLOSURE_WITH_VARIADIC_ARGS, true);
}
}
return;
}
$arg->value->setAttribute(AttributeKey::HAS_CLOSURE_WITH_VARIADIC_ARGS, true);
}
}
/**
* @param Arg[] $args
*/
private function decorateArgNotAcceptingClosure(
CallLike $callLike,
array $args,
MethodReflection|FunctionReflection|null $functionLikeReflection
): void {
if (! $functionLikeReflection instanceof FunctionReflection && ! $functionLikeReflection instanceof MethodReflection) {
return;
}
if (! array_any($args, fn (Arg $arg): bool => $this->isArrayCallableShape($arg->value))) {
return;
}
$parameterReflections = ParametersAcceptorSelectorVariantsWrapper::select(
$functionLikeReflection,
$callLike,
ScopeFetcher::fetch($callLike)
)->getParameters();
$closureObjectType = new ObjectType('Closure');
foreach ($args as $position => $arg) {
if (! $this->isArrayCallableShape($arg->value)) {
continue;
}
$parameterReflection = $this->matchParameterReflection($arg, $position, $parameterReflections);
if (! $parameterReflection instanceof ParameterReflection) {
continue;
}
if (! $parameterReflection->getType()->accepts($closureObjectType, true)->no()) {
continue;
}
$arg->value->setAttribute(AttributeKey::IS_ARG_NOT_ACCEPTING_CLOSURE, true);
}
}
/**
* @param ParameterReflection[] $parameterReflections
*/
private function matchParameterReflection(
Arg $arg,
int $position,
array $parameterReflections
): ?ParameterReflection {
if ($arg->name instanceof Identifier) {
$argName = $arg->name->toString();
foreach ($parameterReflections as $parameterReflection) {
if ($parameterReflection->getName() === $argName) {
return $parameterReflection;
}
}
return null;
}
if (isset($parameterReflections[$position])) {
return $parameterReflections[$position];
}
$lastParameterReflection = end($parameterReflections);
if ($lastParameterReflection instanceof ParameterReflection && $lastParameterReflection->isVariadic()) {
return $lastParameterReflection;
}
return null;
}
}