Skip to content

Commit 0ce2ee3

Browse files
committed
NamedArgumentsRule - do not skip arguments with objects as default values
1 parent 1c33d21 commit 0ce2ee3

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

build/PHPStan/Build/NamedArgumentsRule.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ private function processArgs(ExtendedParametersAcceptor $acceptor, Scope $scope,
142142
}
143143
}
144144

145+
if (count($parameter->getDefaultValue()->getFiniteTypes()) === 0) {
146+
continue;
147+
}
148+
145149
if (!$argValue->equals($parameter->getDefaultValue())) {
146150
if (count($defaultValueWasPassed) > 0) {
147151
$errorBuilders[] = RuleErrorBuilder::message(sprintf(
@@ -199,6 +203,10 @@ private function processArgs(ExtendedParametersAcceptor $acceptor, Scope $scope,
199203
$newArgs[] = $originalArg;
200204
continue;
201205
}
206+
if (count($parameter->getDefaultValue()->getFiniteTypes()) === 0) {
207+
$newArgs[] = $originalArg;
208+
continue;
209+
}
202210
$argValue = $scope->getType($normalizedArg->value);
203211
if ($argValue->equals($parameter->getDefaultValue())) {
204212
$skippedOptional = true;

tests/PHPStan/Build/NamedArgumentsRuleTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,27 @@ public function testFixFileWithMatch(): void
8484
);
8585
}
8686

87+
public function testNewInInitializer(): void
88+
{
89+
if (PHP_VERSION_ID < 80100) {
90+
$this->markTestSkipped('Test requires PHP 8.1.');
91+
}
92+
93+
$this->analyse([__DIR__ . '/data/named-arguments-new.php'], [
94+
[
95+
'You\'re passing a non-default value \'bar\' to parameter $d but previous argument is passing default value to its parameter ($c). You can skip it and use named argument for $d instead.',
96+
24,
97+
],
98+
]);
99+
}
100+
101+
public function testFixNewInInitializer(): void
102+
{
103+
if (PHP_VERSION_ID < 80100) {
104+
$this->markTestSkipped('Test requires PHP 8.1.');
105+
}
106+
107+
$this->fix(__DIR__ . '/data/named-arguments-new.php', __DIR__ . '/data/named-arguments-new.php.fixed');
108+
}
109+
87110
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php // lint >= 8.1
2+
3+
namespace NamedArgumentsRuleNew;
4+
5+
class Bar
6+
{
7+
8+
}
9+
10+
class Foo
11+
{
12+
13+
public static function doFoo(int $a, Bar $bar = new Bar(), string $c = 'bar', string $d = 'baz'): void
14+
{
15+
16+
}
17+
18+
}
19+
20+
function (): void {
21+
Foo::doFoo(1, new Bar(), 'bar');
22+
Foo::doFoo(1, new Bar(), 'baz');
23+
24+
Foo::doFoo(1, new Bar(), 'bar', 'bar');
25+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php // lint >= 8.1
2+
3+
namespace NamedArgumentsRuleNew;
4+
5+
class Bar
6+
{
7+
8+
}
9+
10+
class Foo
11+
{
12+
13+
public static function doFoo(int $a, Bar $bar = new Bar(), string $c = 'bar', string $d = 'baz'): void
14+
{
15+
16+
}
17+
18+
}
19+
20+
function (): void {
21+
Foo::doFoo(1, new Bar(), 'bar');
22+
Foo::doFoo(1, new Bar(), 'baz');
23+
24+
Foo::doFoo(1, new Bar(), d: 'bar');
25+
};

0 commit comments

Comments
 (0)