forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgumentsNormalizer.php
More file actions
340 lines (292 loc) · 8.06 KB
/
ArgumentsNormalizer.php
File metadata and controls
340 lines (292 loc) · 8.06 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantArrayType;
use function array_is_list;
use function array_key_exists;
use function array_keys;
use function array_values;
use function count;
use function ksort;
use function max;
use function sprintf;
/**
* @api
*/
final class ArgumentsNormalizer
{
public const ORIGINAL_ARG_ATTRIBUTE = 'originalArg';
/**
* @return array{ParametersAcceptor, FuncCall, TrinaryLogic}|null
*/
public static function reorderCallUserFuncArguments(
FuncCall $callUserFuncCall,
Scope $scope,
): ?array
{
$args = $callUserFuncCall->getArgs();
if (count($args) < 1) {
return null;
}
$passThruArgs = [];
$callbackArg = null;
foreach ($args as $i => $arg) {
if ($callbackArg === null) {
if ($arg->name === null && $i === 0) {
$callbackArg = $arg;
continue;
}
if ($arg->name !== null && $arg->name->toString() === 'callback') {
$callbackArg = $arg;
continue;
}
}
$passThruArgs[] = $arg;
}
if ($callbackArg === null) {
return null;
}
$calledOnType = $scope->getType($callbackArg->value);
if (!$calledOnType->isCallable()->yes()) {
return null;
}
$callableParametersAcceptors = $calledOnType->getCallableParametersAcceptors($scope);
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$passThruArgs,
$callableParametersAcceptors,
null,
);
$acceptsNamedArguments = TrinaryLogic::createYes();
foreach ($callableParametersAcceptors as $callableParametersAcceptor) {
$acceptsNamedArguments = $acceptsNamedArguments->and($callableParametersAcceptor->acceptsNamedArguments());
}
return [$parametersAcceptor, new FuncCall(
$callbackArg->value,
$passThruArgs,
$callUserFuncCall->getAttributes(),
), $acceptsNamedArguments];
}
public static function reorderFuncArguments(
ParametersAcceptor $parametersAcceptor,
FuncCall $functionCall,
): ?FuncCall
{
$args = $functionCall->getArgs();
$reorderedArgs = self::reorderArgs($parametersAcceptor, $args);
if ($reorderedArgs === null) {
return null;
}
// return identical object if not reordered, as TypeSpecifier relies on object identity
if ($reorderedArgs === $args) {
return $functionCall;
}
return new FuncCall(
$functionCall->name,
$reorderedArgs,
$functionCall->getAttributes(),
);
}
public static function reorderMethodArguments(
ParametersAcceptor $parametersAcceptor,
MethodCall $methodCall,
): ?MethodCall
{
$args = $methodCall->getArgs();
$reorderedArgs = self::reorderArgs($parametersAcceptor, $args);
if ($reorderedArgs === null) {
return null;
}
// return identical object if not reordered, as TypeSpecifier relies on object identity
if ($reorderedArgs === $args) {
return $methodCall;
}
return new MethodCall(
$methodCall->var,
$methodCall->name,
$reorderedArgs,
$methodCall->getAttributes(),
);
}
public static function reorderStaticCallArguments(
ParametersAcceptor $parametersAcceptor,
StaticCall $staticCall,
): ?StaticCall
{
$args = $staticCall->getArgs();
$reorderedArgs = self::reorderArgs($parametersAcceptor, $args);
if ($reorderedArgs === null) {
return null;
}
// return identical object if not reordered, as TypeSpecifier relies on object identity
if ($reorderedArgs === $args) {
return $staticCall;
}
return new StaticCall(
$staticCall->class,
$staticCall->name,
$reorderedArgs,
$staticCall->getAttributes(),
);
}
public static function reorderNewArguments(
ParametersAcceptor $parametersAcceptor,
New_ $new,
): ?New_
{
$args = $new->getArgs();
$reorderedArgs = self::reorderArgs($parametersAcceptor, $args);
if ($reorderedArgs === null) {
return null;
}
// return identical object if not reordered, as TypeSpecifier relies on object identity
if ($reorderedArgs === $args) {
return $new;
}
return new New_(
$new->class,
$reorderedArgs,
$new->getAttributes(),
);
}
/**
* @param Arg[] $callArgs
* @return ?list<Arg>
*/
public static function reorderArgs(ParametersAcceptor $parametersAcceptor, array $callArgs): ?array
{
if (count($callArgs) === 0) {
return [];
}
$hasNamedArgs = false;
foreach ($callArgs as $arg) {
if ($arg->name !== null) {
$hasNamedArgs = true;
break;
}
}
if (!$hasNamedArgs) {
return array_values($callArgs);
}
$hasVariadic = false;
$argumentPositions = [];
$signatureParameters = $parametersAcceptor->getParameters();
foreach ($signatureParameters as $i => $parameter) {
if ($hasVariadic) {
// variadic parameter must be last
return null;
}
$hasVariadic = $parameter->isVariadic();
$argumentPositions[$parameter->getName()] = $i;
}
$reorderedArgs = [];
$additionalNamedArgs = [];
$appendArgs = [];
foreach ($callArgs as $i => $arg) {
if ($arg->name === null) {
// add regular args as is
$attributes = $arg->getAttributes();
$attributes[self::ORIGINAL_ARG_ATTRIBUTE] = $arg;
$reorderedArgs[$i] = new Arg(
$arg->value,
$arg->byRef,
$arg->unpack,
$attributes,
null,
);
} elseif (array_key_exists($arg->name->toString(), $argumentPositions)) {
$argName = $arg->name->toString();
// order named args into the position the signature expects them
$attributes = $arg->getAttributes();
$attributes[self::ORIGINAL_ARG_ATTRIBUTE] = $arg;
if (array_key_exists($argumentPositions[$argName], $reorderedArgs)) {
continue;
}
$reorderedArgs[$argumentPositions[$argName]] = new Arg(
$arg->value,
$arg->byRef,
$arg->unpack,
$attributes,
null,
);
} else {
if (!$hasVariadic) {
$attributes = $arg->getAttributes();
$attributes[self::ORIGINAL_ARG_ATTRIBUTE] = $arg;
$appendArgs[] = new Arg(
$arg->value,
$arg->byRef,
$arg->unpack,
$attributes,
null,
);
continue;
}
$attributes = $arg->getAttributes();
$attributes[self::ORIGINAL_ARG_ATTRIBUTE] = $arg;
$additionalNamedArgs[] = new Arg(
$arg->value,
$arg->byRef,
$arg->unpack,
$attributes,
null,
);
}
}
// replace variadic parameter with additional named args, except if it is already set
$additionalNamedArgsOffset = count($argumentPositions) - 1;
if (array_key_exists($additionalNamedArgsOffset, $reorderedArgs)) {
$additionalNamedArgsOffset++;
}
foreach ($additionalNamedArgs as $i => $additionalNamedArg) {
$reorderedArgs[$additionalNamedArgsOffset + $i] = $additionalNamedArg;
}
if (count($reorderedArgs) === 0) {
foreach ($appendArgs as $arg) {
$reorderedArgs[] = $arg;
}
return $reorderedArgs;
}
// fill up all holes with default values until the last given argument
for ($j = 0; $j < max(array_keys($reorderedArgs)); $j++) {
if (array_key_exists($j, $reorderedArgs)) {
continue;
}
if (!array_key_exists($j, $signatureParameters)) {
throw new ShouldNotHappenException('Parameter signatures cannot have holes');
}
$parameter = $signatureParameters[$j];
// we can only fill up optional parameters with default values
if (!$parameter->isOptional()) {
return null;
}
$defaultValue = $parameter->getDefaultValue();
if ($defaultValue === null) {
if (!$parameter->isVariadic()) {
throw new ShouldNotHappenException(sprintf('An optional parameter $%s must have a default value', $parameter->getName()));
}
$defaultValue = new ConstantArrayType([], []);
}
$reorderedArgs[$j] = new Arg(
new TypeExpr($defaultValue),
);
}
ksort($reorderedArgs);
foreach ($appendArgs as $arg) {
$reorderedArgs[] = $arg;
}
if (!array_is_list($reorderedArgs)) {
$reorderedArgs = array_values($reorderedArgs);
}
return $reorderedArgs;
}
}