Skip to content

Commit 446e353

Browse files
committed
Reorder arguments of the positional arguments fix with ArgumentsNormalizer
Also replaces the match expression the PHP 7.4 downgrade does not handle. Amp-Thread-ID: https://ampcode.com/threads/T-01a19c14-ede5-7153-b7e4-e784ff94be68
1 parent c1f2d68 commit 446e353

3 files changed

Lines changed: 74 additions & 51 deletions

File tree

‎src/Rules/FunctionCallParametersCheck.php‎

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
use Closure;
66
use PhpParser\Node;
77
use PhpParser\Node\Expr;
8+
use PHPStan\Analyser\ArgumentsNormalizer;
89
use PHPStan\Analyser\CollectedDataEmitter;
910
use PHPStan\Analyser\DependencyTracker;
1011
use PHPStan\Analyser\MutatingScope;
1112
use PHPStan\Analyser\NodeCallbackInvoker;
1213
use PHPStan\Analyser\Scope;
1314
use PHPStan\DependencyInjection\AutowiredParameter;
1415
use PHPStan\DependencyInjection\AutowiredService;
16+
use PHPStan\Node\Expr\TypeExpr;
1517
use PHPStan\Reflection\ConstantReflection;
1618
use PHPStan\Reflection\ExtendedParameterReflection;
1719
use PHPStan\Reflection\FunctionReflection;
@@ -916,80 +918,69 @@ private function createPositionalArgumentsFix(
916918
return false;
917919
}
918920

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) {
921+
$args = $funcCall->getArgs();
922+
foreach ($args as $arg) {
929923
if ($arg->unpack) {
930924
return false;
931925
}
932-
if ($arg->name === null) {
933-
$argIndexByPosition[$i] = $i;
934-
$lastPosition = max($lastPosition, $i);
935-
continue;
936-
}
926+
}
937927

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);
928+
$reorderedArgs = ArgumentsNormalizer::reorderArgs($parametersAcceptor, $args);
929+
if ($reorderedArgs === null) {
930+
return false;
949931
}
950932

933+
$parameters = $parametersAcceptor->getParameters();
934+
$passedArgsCount = 0;
951935
/** @var array<int, Expr> $defaultValues */
952936
$defaultValues = [];
953-
for ($position = 0; $position < $lastPosition; $position++) {
954-
if (array_key_exists($position, $argIndexByPosition)) {
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;
955946
continue;
956947
}
957948

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_([]);
949+
$passedArgsCount++;
950+
/** @var Node\Arg $originalArg */
951+
$originalArg = $reorderedArg->getAttribute(ArgumentsNormalizer::ORIGINAL_ARG_ATTRIBUTE);
952+
if ($originalArg->name === null) {
966953
continue;
967954
}
968955

969-
$scalarValues = $defaultValue->getConstantScalarValues();
970-
if (count($scalarValues) !== 1) {
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()) {
971959
return false;
972960
}
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-
};
981961
}
982962

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();
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+
985974
$args = [];
986-
for ($position = 0; $position <= $lastPosition; $position++) {
987-
if (!array_key_exists($position, $argIndexByPosition)) {
975+
foreach ($reorderedArgs as $position => $reorderedArg) {
976+
if (array_key_exists($position, $defaultValues)) {
988977
$args[] = new Node\Arg(clone $defaultValues[$position]);
989978
continue;
990979
}
991980

992-
$arg = clone $callArgs[$argIndexByPosition[$position]];
981+
/** @var Node\Arg $originalArg */
982+
$originalArg = $reorderedArg->getAttribute(ArgumentsNormalizer::ORIGINAL_ARG_ATTRIBUTE);
983+
$arg = clone $originalArg;
993984
$arg->name = null;
994985
$args[] = $arg;
995986
}
@@ -999,6 +990,34 @@ private function createPositionalArgumentsFix(
999990
};
1000991
}
1001992

993+
private function createDefaultValueExpr(Type $type): ?Expr
994+
{
995+
if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
996+
return new Expr\Array_([]);
997+
}
998+
999+
$scalarValues = $type->getConstantScalarValues();
1000+
if (count($scalarValues) !== 1) {
1001+
return null;
1002+
}
1003+
1004+
$scalarValue = $scalarValues[0];
1005+
if ($scalarValue === null) {
1006+
return new Expr\ConstFetch(new Node\Name('null'));
1007+
}
1008+
if (is_bool($scalarValue)) {
1009+
return new Expr\ConstFetch(new Node\Name($scalarValue ? 'true' : 'false'));
1010+
}
1011+
if (is_int($scalarValue)) {
1012+
return new Node\Scalar\Int_($scalarValue);
1013+
}
1014+
if (is_float($scalarValue)) {
1015+
return new Node\Scalar\Float_($scalarValue);
1016+
}
1017+
1018+
return new Node\Scalar\String_($scalarValue);
1019+
}
1020+
10021021
private function callReturnsByReference(Expr $expr, Scope $scope): bool
10031022
{
10041023
if ($expr instanceof Node\Expr\MethodCall) {

‎tests/PHPStan/Rules/Functions/data/fix-no-named-arguments.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ function (): void {
4949
variadic(1, 2, x: 3);
5050
objectDefault(1, c: 4);
5151
objectDefault(1, b: new \stdClass());
52+
foo(1, unknown: 2);
53+
foo(b: 1, b: 2, a: 3);
5254
};

‎tests/PHPStan/Rules/Functions/data/fix-no-named-arguments.php.fixed‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ function (): void {
5353
variadic(1, 2, x: 3);
5454
objectDefault(1, c: 4);
5555
objectDefault(1, new \stdClass());
56+
foo(1, unknown: 2);
57+
foo(b: 1, b: 2, a: 3);
5658
};

0 commit comments

Comments
 (0)