|
5 | 5 | use Closure; |
6 | 6 | use PhpParser\Node; |
7 | 7 | use PhpParser\Node\Expr; |
8 | | -use PHPStan\Analyser\ArgumentsNormalizer; |
9 | 8 | use PHPStan\Analyser\CollectedDataEmitter; |
10 | 9 | use PHPStan\Analyser\DependencyTracker; |
11 | 10 | use PHPStan\Analyser\MutatingScope; |
12 | 11 | use PHPStan\Analyser\NodeCallbackInvoker; |
13 | 12 | use PHPStan\Analyser\Scope; |
14 | 13 | use PHPStan\DependencyInjection\AutowiredParameter; |
15 | 14 | use PHPStan\DependencyInjection\AutowiredService; |
16 | | -use PHPStan\Node\Expr\TypeExpr; |
17 | 15 | use PHPStan\Reflection\ConstantReflection; |
18 | 16 | use PHPStan\Reflection\ExtendedParameterReflection; |
19 | 17 | use PHPStan\Reflection\FunctionReflection; |
@@ -918,69 +916,68 @@ private function createPositionalArgumentsFix( |
918 | 916 | return false; |
919 | 917 | } |
920 | 918 |
|
921 | | - $args = $funcCall->getArgs(); |
922 | | - foreach ($args as $arg) { |
| 919 | + $parameters = $parametersAcceptor->getParameters(); |
| 920 | + $parameterPositions = []; |
| 921 | + foreach ($parameters as $i => $parameter) { |
| 922 | + $parameterPositions[$parameter->getName()] = $i; |
| 923 | + } |
| 924 | + |
| 925 | + /** @var array<int, int> $argIndexByPosition */ |
| 926 | + $argIndexByPosition = []; |
| 927 | + $lastPosition = 0; |
| 928 | + foreach ($funcCall->getArgs() as $i => $arg) { |
923 | 929 | if ($arg->unpack) { |
924 | 930 | return false; |
925 | 931 | } |
926 | | - } |
| 932 | + if ($arg->name === null) { |
| 933 | + $argIndexByPosition[$i] = $i; |
| 934 | + $lastPosition = max($lastPosition, $i); |
| 935 | + continue; |
| 936 | + } |
927 | 937 |
|
928 | | - $reorderedArgs = ArgumentsNormalizer::reorderArgs($parametersAcceptor, $args); |
929 | | - if ($reorderedArgs === null) { |
930 | | - return false; |
| 938 | + $position = $parameterPositions[$arg->name->toString()] ?? null; |
| 939 | + if ( |
| 940 | + $position === null |
| 941 | + // a named argument that is collected by the variadic parameter keeps its string key |
| 942 | + || $parameters[$position]->isVariadic() |
| 943 | + || array_key_exists($position, $argIndexByPosition) |
| 944 | + ) { |
| 945 | + return false; |
| 946 | + } |
| 947 | + $argIndexByPosition[$position] = $i; |
| 948 | + $lastPosition = max($lastPosition, $position); |
931 | 949 | } |
932 | 950 |
|
933 | | - $parameters = $parametersAcceptor->getParameters(); |
934 | | - $passedArgsCount = 0; |
935 | 951 | /** @var array<int, Expr> $defaultValues */ |
936 | 952 | $defaultValues = []; |
937 | | - foreach ($reorderedArgs as $position => $reorderedArg) { |
938 | | - $parameter = $parameters[$position] ?? null; |
939 | | - if ($reorderedArg->value instanceof TypeExpr) { |
940 | | - // reorderArgs() fills skipped optional parameters with their default value types |
941 | | - $defaultValue = $this->createDefaultValueExpr($reorderedArg->value->getExprType()); |
942 | | - if ($parameter === null || !$parameter->passedByReference()->no() || $defaultValue === null) { |
943 | | - return false; |
944 | | - } |
945 | | - $defaultValues[$position] = $defaultValue; |
| 953 | + for ($position = 0; $position < $lastPosition; $position++) { |
| 954 | + if (array_key_exists($position, $argIndexByPosition)) { |
946 | 955 | continue; |
947 | 956 | } |
948 | 957 |
|
949 | | - $passedArgsCount++; |
950 | | - /** @var Node\Arg $originalArg */ |
951 | | - $originalArg = $reorderedArg->getAttribute(ArgumentsNormalizer::ORIGINAL_ARG_ATTRIBUTE); |
952 | | - if ($originalArg->name === null) { |
953 | | - continue; |
| 958 | + $parameter = $parameters[$position]; |
| 959 | + $defaultValue = $parameter->getDefaultValue(); |
| 960 | + if (!$parameter->isOptional() || !$parameter->passedByReference()->no() || $defaultValue === null) { |
| 961 | + return false; |
954 | 962 | } |
955 | 963 |
|
956 | | - // reorderArgs() appends unknown named arguments and puts the ones collected by the variadic parameter at its position, |
957 | | - // passing them positionally would change their meaning |
958 | | - if ($parameter === null || $parameter->isVariadic() || $parameter->getName() !== $originalArg->name->toString()) { |
| 964 | + $defaultValueExpr = $this->createDefaultValueExpr($defaultValue); |
| 965 | + if ($defaultValueExpr === null) { |
959 | 966 | return false; |
960 | 967 | } |
| 968 | + $defaultValues[$position] = $defaultValueExpr; |
961 | 969 | } |
962 | 970 |
|
963 | | - // reorderArgs() drops duplicate named arguments |
964 | | - if ($passedArgsCount !== count($args)) { |
965 | | - return false; |
966 | | - } |
967 | | - |
968 | | - return static function (Node\Expr\FuncCall|Node\Expr\MethodCall|Node\Expr\StaticCall|Node\Expr\New_ $call) use ($parametersAcceptor, $defaultValues): Node\Expr\FuncCall|Node\Expr\MethodCall|Node\Expr\StaticCall|Node\Expr\New_ { |
969 | | - $reorderedArgs = ArgumentsNormalizer::reorderArgs($parametersAcceptor, $call->getArgs()); |
970 | | - if ($reorderedArgs === null) { |
971 | | - throw new ShouldNotHappenException(); |
972 | | - } |
973 | | - |
| 971 | + return static function (Node\Expr\FuncCall|Node\Expr\MethodCall|Node\Expr\StaticCall|Node\Expr\New_ $call) use ($argIndexByPosition, $defaultValues, $lastPosition): Node\Expr\FuncCall|Node\Expr\MethodCall|Node\Expr\StaticCall|Node\Expr\New_ { |
| 972 | + $callArgs = $call->getArgs(); |
974 | 973 | $args = []; |
975 | | - foreach ($reorderedArgs as $position => $reorderedArg) { |
976 | | - if (array_key_exists($position, $defaultValues)) { |
| 974 | + for ($position = 0; $position <= $lastPosition; $position++) { |
| 975 | + if (!array_key_exists($position, $argIndexByPosition)) { |
977 | 976 | $args[] = new Node\Arg(clone $defaultValues[$position]); |
978 | 977 | continue; |
979 | 978 | } |
980 | 979 |
|
981 | | - /** @var Node\Arg $originalArg */ |
982 | | - $originalArg = $reorderedArg->getAttribute(ArgumentsNormalizer::ORIGINAL_ARG_ATTRIBUTE); |
983 | | - $arg = clone $originalArg; |
| 980 | + $arg = clone $callArgs[$argIndexByPosition[$position]]; |
984 | 981 | $arg->name = null; |
985 | 982 | $args[] = $arg; |
986 | 983 | } |
|
0 commit comments