Skip to content

Commit c1f2d68

Browse files
committed
Make named arguments passed to @no-named-arguments callables fixable
The fix passes the arguments positionally in parameter order and fills skipped optional parameters with their default values. Amp-Thread-ID: https://ampcode.com/threads/T-01a19c14-ede5-7153-b7e4-e784ff94be68
1 parent 2426bae commit c1f2d68

4 files changed

Lines changed: 224 additions & 3 deletions

File tree

‎src/Rules/FunctionCallParametersCheck.php‎

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules;
44

5+
use Closure;
56
use PhpParser\Node;
67
use PhpParser\Node\Expr;
78
use PHPStan\Analyser\CollectedDataEmitter;
@@ -43,6 +44,8 @@
4344
use function count;
4445
use function implode;
4546
use function in_array;
47+
use function is_bool;
48+
use function is_float;
4649
use function is_int;
4750
use function is_string;
4851
use function lcfirst;
@@ -336,6 +339,7 @@ public function check(
336339
return $errors;
337340
}
338341

342+
$positionalArgumentsFix = null;
339343
foreach ($argumentsWithParameters as $i => [$argumentValue, $argumentValueType, $unpack, $argumentName, $argumentLine, $parameter, $originalParameter]) {
340344
if ($this->checkArgumentTypes && $unpack) {
341345
$iterableTypeResult = $this->ruleLevelHelper->findTypeToCheck(
@@ -375,10 +379,15 @@ public function check(
375379

376380
if (!$acceptsNamedArguments->yes()) {
377381
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)))
379383
->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();
382391
} elseif ($unpack) {
383392
$unpackedArrayType = $scope->getType($argumentValue);
384393
$hasStringKey = $unpackedArrayType->getIterableKeyType()->isString();
@@ -892,6 +901,104 @@ private function resolveConstantReflections(Expr $expr, Scope $scope): ?array
892901
return null;
893902
}
894903

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+
8951002
private function callReturnsByReference(Expr $expr, Scope $scope): bool
8961003
{
8971004
if ($expr instanceof Node\Expr\MethodCall) {

‎tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,12 @@ public function testNoNamedArguments(): void
22442244
]);
22452245
}
22462246

2247+
#[RequiresPhp('>= 8.0.0')]
2248+
public function testFixNoNamedArguments(): void
2249+
{
2250+
$this->fix(__DIR__ . '/data/fix-no-named-arguments.php', __DIR__ . '/data/fix-no-named-arguments.php.fixed');
2251+
}
2252+
22472253
public function testBug11056(): void
22482254
{
22492255
$this->checkExplicitMixed = true;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php // lint >= 8.0
2+
3+
namespace FixNoNamedArgumentsFunction;
4+
5+
/**
6+
* @no-named-arguments
7+
*/
8+
function foo(int $a, int $b = 2, ?string $c = null, bool $d = false, array $e = [], float $f = -1.5, string $g = 'x'): void
9+
{
10+
11+
}
12+
13+
/**
14+
* @no-named-arguments
15+
*/
16+
function byRef(int $a, &$b = null, int $c = 3): void
17+
{
18+
19+
}
20+
21+
/**
22+
* @no-named-arguments
23+
*/
24+
function variadic(int $a, int ...$rest): void
25+
{
26+
27+
}
28+
29+
/**
30+
* @no-named-arguments
31+
*/
32+
function objectDefault(int $a, \stdClass $b = new \stdClass(), int $c = 3): void
33+
{
34+
35+
}
36+
37+
function (): void {
38+
foo(a: 1);
39+
foo(b: 5, a: 1);
40+
foo(1, c: 'c');
41+
foo(
42+
1,
43+
g: 'y',
44+
d: true,
45+
);
46+
foo(a: foo(b: 3, a: 4), e: [1]);
47+
byRef(1, c: 4);
48+
variadic(a: 1);
49+
variadic(1, 2, x: 3);
50+
objectDefault(1, c: 4);
51+
objectDefault(1, b: new \stdClass());
52+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php // lint >= 8.0
2+
3+
namespace FixNoNamedArgumentsFunction;
4+
5+
/**
6+
* @no-named-arguments
7+
*/
8+
function foo(int $a, int $b = 2, ?string $c = null, bool $d = false, array $e = [], float $f = -1.5, string $g = 'x'): void
9+
{
10+
11+
}
12+
13+
/**
14+
* @no-named-arguments
15+
*/
16+
function byRef(int $a, &$b = null, int $c = 3): void
17+
{
18+
19+
}
20+
21+
/**
22+
* @no-named-arguments
23+
*/
24+
function variadic(int $a, int ...$rest): void
25+
{
26+
27+
}
28+
29+
/**
30+
* @no-named-arguments
31+
*/
32+
function objectDefault(int $a, \stdClass $b = new \stdClass(), int $c = 3): void
33+
{
34+
35+
}
36+
37+
function (): void {
38+
foo(1);
39+
foo(1, 5);
40+
foo(1, 2, 'c');
41+
foo(
42+
1,
43+
2,
44+
null,
45+
true,
46+
[],
47+
-1.5,
48+
'y',
49+
);
50+
foo(foo(4, 3), 2, null, false, [1]);
51+
byRef(1, c: 4);
52+
variadic(1);
53+
variadic(1, 2, x: 3);
54+
objectDefault(1, c: 4);
55+
objectDefault(1, new \stdClass());
56+
};

0 commit comments

Comments
 (0)