-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathUnnamedArgumentResolver.php
More file actions
74 lines (60 loc) · 2.1 KB
/
UnnamedArgumentResolver.php
File metadata and controls
74 lines (60 loc) · 2.1 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
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp80\NodeAnalyzer;
use PhpParser\Node\Arg;
use PhpParser\Node\Identifier;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use Rector\NodeNameResolver\NodeNameResolver;
use ReflectionFunction;
final readonly class UnnamedArgumentResolver
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private NamedToUnnamedArgs $namedToUnnamedArgs
) {
}
/**
* @param Arg[] $currentArgs
* @return Arg[]
*/
public function resolveFromReflection(
FunctionReflection | MethodReflection $functionLikeReflection,
array $currentArgs
): array {
$extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors(
$functionLikeReflection->getVariants()
);
$parameters = $extendedParametersAcceptor->getParameters();
if ($functionLikeReflection instanceof NativeFunctionReflection) {
$functionLikeReflection = new ReflectionFunction($functionLikeReflection->getName());
}
/** @var array<int, Arg> $unnamedArgs */
$unnamedArgs = [];
$toFillArgs = [];
foreach ($currentArgs as $key => $arg) {
if (! $arg->name instanceof Identifier) {
$unnamedArgs[$key] = new Arg($arg->value, $arg->byRef, $arg->unpack, [], null);
continue;
}
/** @var string $argName */
$argName = $this->nodeNameResolver->getName($arg->name);
$toFillArgs[] = $argName;
}
$unnamedArgs = $this->namedToUnnamedArgs->fillFromNamedArgs(
$parameters,
$currentArgs,
$toFillArgs,
$unnamedArgs
);
$unnamedArgs = $this->namedToUnnamedArgs->fillFromJumpedNamedArgs(
$functionLikeReflection,
$unnamedArgs,
$parameters
);
ksort($unnamedArgs);
return $unnamedArgs;
}
}