Skip to content

Commit 772d3ae

Browse files
committed
NamedArgumentsRule - do not touch by-ref arguments
1 parent efcc982 commit 772d3ae

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

build/PHPStan/Build/NamedArgumentsRule.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ private function processArgs(ExtendedParametersAcceptor $acceptor, Scope $scope,
125125
if ($parameter->getDefaultValue() === null) {
126126
continue;
127127
}
128+
if (!$parameter->passedByReference()->no()) {
129+
continue;
130+
}
128131
$argValue = $scope->getType($normalizedArg->value);
129132
if ($normalizedArg->name !== null) {
130133
continue;
@@ -192,6 +195,10 @@ private function processArgs(ExtendedParametersAcceptor $acceptor, Scope $scope,
192195
$newArgs[] = $originalArg;
193196
continue;
194197
}
198+
if (!$parameter->passedByReference()->no()) {
199+
$newArgs[] = $originalArg;
200+
continue;
201+
}
195202
$argValue = $scope->getType($normalizedArg->value);
196203
if ($argValue->equals($parameter->getDefaultValue())) {
197204
$skippedOptional = true;

tests/PHPStan/Build/NamedArgumentsRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function testRule(): void
3737
'You\'re passing a non-default value Exception to parameter $previous but previous arguments are passing default values to their parameters ($message, $code). You can skip them and use named argument for $previous instead.',
3838
20,
3939
],
40+
[
41+
'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.',
42+
41,
43+
],
4044
]);
4145
}
4246

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,24 @@ public function doFoo(): void
2121
}
2222

2323
}
24+
25+
function (): void {
26+
$output = null;
27+
exec('exec', $output, $exitCode);
28+
};
29+
30+
class Bar
31+
{
32+
33+
public static function doFoo($a, &$byRef = null, int $another = 1, int $yetAnother = 2): void
34+
{
35+
36+
}
37+
38+
public function doBar(): void
39+
{
40+
$byRef = null;
41+
self::doFoo('a', $byRef, 1, 3);
42+
}
43+
44+
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,24 @@ class Foo
2121
}
2222

2323
}
24+
25+
function (): void {
26+
$output = null;
27+
exec('exec', $output, $exitCode);
28+
};
29+
30+
class Bar
31+
{
32+
33+
public static function doFoo($a, &$byRef = null, int $another = 1, int $yetAnother = 2): void
34+
{
35+
36+
}
37+
38+
public function doBar(): void
39+
{
40+
$byRef = null;
41+
self::doFoo('a', $byRef, yetAnother: 3);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)