|
2 | 2 |
|
3 | 3 | namespace PHPStan\Rules; |
4 | 4 |
|
| 5 | +use Closure; |
5 | 6 | use PhpParser\Node; |
6 | 7 | use PhpParser\Node\Expr; |
7 | 8 | use PHPStan\Analyser\CollectedDataEmitter; |
|
43 | 44 | use function count; |
44 | 45 | use function implode; |
45 | 46 | use function in_array; |
| 47 | +use function is_bool; |
| 48 | +use function is_float; |
46 | 49 | use function is_int; |
47 | 50 | use function is_string; |
48 | 51 | use function lcfirst; |
@@ -336,6 +339,7 @@ public function check( |
336 | 339 | return $errors; |
337 | 340 | } |
338 | 341 |
|
| 342 | + $positionalArgumentsFix = null; |
339 | 343 | foreach ($argumentsWithParameters as $i => [$argumentValue, $argumentValueType, $unpack, $argumentName, $argumentLine, $parameter, $originalParameter]) { |
340 | 344 | if ($this->checkArgumentTypes && $unpack) { |
341 | 345 | $iterableTypeResult = $this->ruleLevelHelper->findTypeToCheck( |
@@ -375,10 +379,15 @@ public function check( |
375 | 379 |
|
376 | 380 | if (!$acceptsNamedArguments->yes()) { |
377 | 381 | if ($argumentName !== null) { |
378 | | - $errors[] = RuleErrorBuilder::message(sprintf($namedArgumentMessage, sprintf('named argument $%s', $argumentName))) |
| 382 | + $errorBuilder = RuleErrorBuilder::message(sprintf($namedArgumentMessage, sprintf('named argument $%s', $argumentName))) |
379 | 383 | ->identifier('argument.named') |
380 | | - ->line($argumentLine) |
381 | | - ->build(); |
| 384 | + ->line($argumentLine); |
| 385 | + $positionalArgumentsFix ??= $this->createPositionalArgumentsFix($parametersAcceptor, $funcCall); |
| 386 | + if ($positionalArgumentsFix !== false) { |
| 387 | + // every named argument of the call reports the same fix, the identical changes are applied once |
| 388 | + $errorBuilder->fixNode($funcCall, $positionalArgumentsFix); |
| 389 | + } |
| 390 | + $errors[] = $errorBuilder->build(); |
382 | 391 | } elseif ($unpack) { |
383 | 392 | $unpackedArrayType = $scope->getType($argumentValue); |
384 | 393 | $hasStringKey = $unpackedArrayType->getIterableKeyType()->isString(); |
@@ -892,6 +901,104 @@ private function resolveConstantReflections(Expr $expr, Scope $scope): ?array |
892 | 901 | return null; |
893 | 902 | } |
894 | 903 |
|
| 904 | + /** |
| 905 | + * Rewrites the call to pass every argument positionally, skipped optional parameters get their default values. |
| 906 | + * |
| 907 | + * @return (Closure(Node\Expr\FuncCall|Node\Expr\MethodCall|Node\Expr\StaticCall|Node\Expr\New_): (Node\Expr\FuncCall|Node\Expr\MethodCall|Node\Expr\StaticCall|Node\Expr\New_))|false |
| 908 | + */ |
| 909 | + private function createPositionalArgumentsFix( |
| 910 | + ParametersAcceptor $parametersAcceptor, |
| 911 | + Node\Expr\FuncCall|Node\Expr\MethodCall|Node\Expr\StaticCall|Node\Expr\New_ $funcCall, |
| 912 | + ): Closure|false |
| 913 | + { |
| 914 | + if ((bool) $funcCall->getAttribute('isAttribute', false)) { |
| 915 | + // the call was created from an attribute, it is not in the file |
| 916 | + return false; |
| 917 | + } |
| 918 | + |
| 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) { |
| 929 | + if ($arg->unpack) { |
| 930 | + return false; |
| 931 | + } |
| 932 | + if ($arg->name === null) { |
| 933 | + $argIndexByPosition[$i] = $i; |
| 934 | + $lastPosition = max($lastPosition, $i); |
| 935 | + continue; |
| 936 | + } |
| 937 | + |
| 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); |
| 949 | + } |
| 950 | + |
| 951 | + /** @var array<int, Expr> $defaultValues */ |
| 952 | + $defaultValues = []; |
| 953 | + for ($position = 0; $position < $lastPosition; $position++) { |
| 954 | + if (array_key_exists($position, $argIndexByPosition)) { |
| 955 | + continue; |
| 956 | + } |
| 957 | + |
| 958 | + $parameter = $parameters[$position]; |
| 959 | + $defaultValue = $parameter->getDefaultValue(); |
| 960 | + if (!$parameter->isOptional() || !$parameter->passedByReference()->no() || $defaultValue === null) { |
| 961 | + return false; |
| 962 | + } |
| 963 | + |
| 964 | + if ($defaultValue->isConstantArray()->yes() && $defaultValue->isIterableAtLeastOnce()->no()) { |
| 965 | + $defaultValues[$position] = new Expr\Array_([]); |
| 966 | + continue; |
| 967 | + } |
| 968 | + |
| 969 | + $scalarValues = $defaultValue->getConstantScalarValues(); |
| 970 | + if (count($scalarValues) !== 1) { |
| 971 | + return false; |
| 972 | + } |
| 973 | + $scalarValue = $scalarValues[0]; |
| 974 | + $defaultValues[$position] = match (true) { |
| 975 | + $scalarValue === null => new Expr\ConstFetch(new Node\Name('null')), |
| 976 | + is_bool($scalarValue) => new Expr\ConstFetch(new Node\Name($scalarValue ? 'true' : 'false')), |
| 977 | + is_int($scalarValue) => new Node\Scalar\Int_($scalarValue), |
| 978 | + is_float($scalarValue) => new Node\Scalar\Float_($scalarValue), |
| 979 | + default => new Node\Scalar\String_($scalarValue), |
| 980 | + }; |
| 981 | + } |
| 982 | + |
| 983 | + 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_ { |
| 984 | + $callArgs = $call->getArgs(); |
| 985 | + $args = []; |
| 986 | + for ($position = 0; $position <= $lastPosition; $position++) { |
| 987 | + if (!array_key_exists($position, $argIndexByPosition)) { |
| 988 | + $args[] = new Node\Arg(clone $defaultValues[$position]); |
| 989 | + continue; |
| 990 | + } |
| 991 | + |
| 992 | + $arg = clone $callArgs[$argIndexByPosition[$position]]; |
| 993 | + $arg->name = null; |
| 994 | + $args[] = $arg; |
| 995 | + } |
| 996 | + $call->args = $args; |
| 997 | + |
| 998 | + return $call; |
| 999 | + }; |
| 1000 | + } |
| 1001 | + |
895 | 1002 | private function callReturnsByReference(Expr $expr, Scope $scope): bool |
896 | 1003 | { |
897 | 1004 | if ($expr instanceof Node\Expr\MethodCall) { |
|
0 commit comments