-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathImmediatelyInvokedCallableHelper.php
More file actions
204 lines (170 loc) · 6.75 KB
/
Copy pathImmediatelyInvokedCallableHelper.php
File metadata and controls
204 lines (170 loc) · 6.75 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
195
196
197
198
199
200
201
202
203
204
<?php declare(strict_types = 1);
namespace ShipMonk\PHPStan\Helper;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ExtendedParameterReflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use function array_values;
use function spl_object_hash;
class ImmediatelyInvokedCallableHelper
{
private ReflectionProvider $reflectionProvider;
public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}
/**
* Returns spl_object_hashes of callable argument nodes that are
* param-immediately-invoked-callable or directly invoked.
*
* @return array<string, true>
*/
public function getImmediatelyInvokedHashes(
CallLike $node,
Scope $scope
): array
{
// Directly invoked callable syntax: (function(){...})(), (fn() => ...)()
if ($node instanceof FuncCall && $this->isCallableExpression($node->name)) {
return [spl_object_hash($node->name) => true];
}
$analysis = $this->analyzeCall($node, $scope);
return $analysis !== null ? $analysis->immediatelyInvokedHashes : [];
}
/**
* Full analysis of a call — returns the resolved reflection, caller type,
* reordered arguments, and which argument hashes are immediately invoked.
*
* Returns null for calls that cannot be resolved (e.g. dynamic method names).
*/
public function analyzeCall(
CallLike $node,
Scope $scope
): ?CallAnalysisResult
{
if ($node instanceof MethodCall && $node->name instanceof Identifier) {
$callerType = $scope->getType($node->var);
$methodReflection = $scope->getMethodReflection($callerType, $node->name->name);
} elseif ($node instanceof StaticCall && $node->name instanceof Identifier && $node->class instanceof Name) {
$callerType = $scope->resolveTypeByName($node->class);
$methodReflection = $scope->getMethodReflection($callerType, $node->name->name);
} elseif ($node instanceof New_ && $node->class instanceof Name) {
$callerType = $scope->resolveTypeByName($node->class);
$methodReflection = $scope->getMethodReflection($callerType, '__construct');
} elseif ($node instanceof FuncCall && $node->name instanceof Name) {
$callerType = null;
$methodReflection = $this->getFunctionReflection($node->name, $scope);
} else {
return null;
}
if ($methodReflection === null) {
return null;
}
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$node->getArgs(),
$methodReflection->getVariants(),
$methodReflection->getNamedArgumentsVariants(),
);
if ($node instanceof New_) {
$arguments = (ArgumentsNormalizer::reorderNewArguments($parametersAcceptor, $node) ?? $node)->getArgs();
} elseif ($node instanceof FuncCall) {
$arguments = (ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node) ?? $node)->getArgs();
} elseif ($node instanceof MethodCall) {
$arguments = (ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $node) ?? $node)->getArgs();
} else {
$arguments = (ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $node) ?? $node)->getArgs();
}
/** @var list<Arg> $args */
$args = array_values($arguments);
$parameters = $parametersAcceptor->getParameters();
$immediatelyInvokedHashes = [];
foreach ($args as $index => $arg) {
$parameterIndex = $this->getParameterIndex($arg, $index, $parameters) ?? -1;
$parameter = $parameters[$parameterIndex] ?? null;
if ($this->isImmediatelyInvokedCallable($methodReflection, $parameter)) {
$immediatelyInvokedHashes[spl_object_hash($arg->value)] = true;
}
}
return new CallAnalysisResult(
$methodReflection,
$callerType,
$args,
$immediatelyInvokedHashes,
);
}
public function isCallableExpression(Node $node): bool
{
return $node instanceof Closure
|| $node instanceof ArrowFunction
|| ($node instanceof MethodCall && $node->isFirstClassCallable())
|| ($node instanceof NullsafeMethodCall && $node->isFirstClassCallable())
|| ($node instanceof StaticCall && $node->isFirstClassCallable())
|| ($node instanceof FuncCall && $node->isFirstClassCallable());
}
/**
* Copied from phpstan
*
* @param FunctionReflection|MethodReflection $reflection
*
* @see https://github.com/phpstan/phpstan-src/commit/cefa296f24b8c0b7d4dc3d383cbceea35267cb3f
*/
private function isImmediatelyInvokedCallable(
object $reflection,
?ParameterReflection $parameter
): bool
{
if ($parameter instanceof ExtendedParameterReflection) {
$parameterCallImmediately = $parameter->isImmediatelyInvokedCallable();
if ($parameterCallImmediately->maybe()) {
return $reflection instanceof FunctionReflection;
}
return $parameterCallImmediately->yes();
}
return $reflection instanceof FunctionReflection;
}
/**
* @param array<int, ParameterReflection> $parameters
*/
private function getParameterIndex(
Arg $arg,
int $argumentIndex,
array $parameters
): ?int
{
if ($arg->name === null) {
return $argumentIndex;
}
foreach ($parameters as $parameterIndex => $parameter) {
if ($parameter->getName() === $arg->name->toString()) {
return $parameterIndex;
}
}
return null;
}
private function getFunctionReflection(
Name $functionName,
Scope $scope
): ?FunctionReflection
{
return $this->reflectionProvider->hasFunction($functionName, $scope)
? $this->reflectionProvider->getFunction($functionName, $scope)
: null;
}
}