Skip to content

Commit fabf273

Browse files
committed
Fix too greedy removal of named arguments
1 parent 772d3ae commit fabf273

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

src/Analyser/ArgumentsNormalizer.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,16 @@ public static function reorderArgs(ParametersAcceptor $parametersAcceptor, array
222222
foreach ($callArgs as $i => $arg) {
223223
if ($arg->name === null) {
224224
// add regular args as is
225-
$reorderedArgs[$i] = $arg;
225+
226+
$attributes = $arg->getAttributes();
227+
$attributes[self::ORIGINAL_ARG_ATTRIBUTE] = $arg;
228+
$reorderedArgs[$i] = new Arg(
229+
$arg->value,
230+
$arg->byRef,
231+
$arg->unpack,
232+
$attributes,
233+
null,
234+
);
226235
} elseif (array_key_exists($arg->name->toString(), $argumentPositions)) {
227236
$argName = $arg->name->toString();
228237
// order named args into the position the signature expects them

tests/PHPStan/Build/NamedArgumentsRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function testRule(): void
4141
'You\'re passing a non-default value 3 to parameter $yetAnother but previous argument is passing default value to its parameter ($another). You can skip it and use named argument for $yetAnother instead.',
4242
41,
4343
],
44+
[
45+
'Named argument $priority can be omitted, type 1 is the same as the default value.',
46+
59,
47+
],
4448
]);
4549
}
4650

tests/PHPStan/Build/data/named-arguments.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,22 @@ public function doBar(): void
4242
}
4343

4444
}
45+
46+
class Baz
47+
{
48+
49+
public const HIGH = 1;
50+
public const MEDIUM = 2;
51+
52+
public static function send(Bar $message, ?string $queueName = null, ?Bar $mode = null, int $priority = self::HIGH)
53+
{
54+
55+
}
56+
57+
public function doFoo(Bar $message): void
58+
{
59+
self::send($message, 'queue', priority: self::HIGH);
60+
self::send($message, 'queue', priority: self::MEDIUM);
61+
}
62+
63+
}

tests/PHPStan/Build/data/named-arguments.php.fixed

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Foo
1313
new Exception('foo', 0, null);
1414
new Exception('foo', previous: new Exception('previous'));
1515
new Exception('foo', previous: new Exception('previous'));
16-
new Exception(previous: new Exception('previous'));
16+
new Exception('foo', previous: new Exception('previous'));
1717
new Exception('foo', code: 1, previous: new Exception('previous'));
1818
new Exception('foo', 1, new Exception('previous'));
1919
new Exception('foo', 1);
@@ -42,3 +42,22 @@ class Bar
4242
}
4343

4444
}
45+
46+
class Baz
47+
{
48+
49+
public const HIGH = 1;
50+
public const MEDIUM = 2;
51+
52+
public static function send(Bar $message, ?string $queueName = null, ?Bar $mode = null, int $priority = self::HIGH)
53+
{
54+
55+
}
56+
57+
public function doFoo(Bar $message): void
58+
{
59+
self::send($message, 'queue');
60+
self::send($message, 'queue', priority: self::MEDIUM);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)