Skip to content

Commit 65e4cec

Browse files
committed
Named arguments - type mismatch
1 parent dcdfcfa commit 65e4cec

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/Rules/FunctionCallParametersCheck.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,14 @@ static function (Type $type): bool {
255255
&& !$this->ruleLevelHelper->accepts($parameterType, $argumentValueType, $scope->isDeclareStrictTypes())
256256
) {
257257
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType);
258+
$parameterDescription = sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName());
258259
$errors[] = RuleErrorBuilder::message(sprintf(
259260
$messages[6],
260-
sprintf('#%d %s', $i + 1, sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName())),
261+
$argumentName === null ? sprintf(
262+
'#%d %s',
263+
$i + 1,
264+
$parameterDescription
265+
) : $parameterDescription,
261266
$parameterType->describe($verbosityLevel),
262267
$argumentValueType->describe($verbosityLevel)
263268
))->build();
@@ -271,9 +276,10 @@ static function (Type $type): bool {
271276
}
272277

273278
if ($this->nullsafeCheck->containsNullSafe($argumentValue)) {
279+
$parameterDescription = sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName());
274280
$errors[] = RuleErrorBuilder::message(sprintf(
275281
$messages[8],
276-
sprintf('#%d %s', $i + 1, sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName()))
282+
$argumentName === null ? sprintf('#%d %s', $i + 1, $parameterDescription) : $parameterDescription
277283
))->build();
278284
continue;
279285
}
@@ -285,9 +291,10 @@ static function (Type $type): bool {
285291
continue;
286292
}
287293

294+
$parameterDescription = sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName());
288295
$errors[] = RuleErrorBuilder::message(sprintf(
289296
$messages[8],
290-
sprintf('#%d %s', $i + 1, sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName()))
297+
$argumentName === null ? sprintf('#%d %s', $i + 1, $parameterDescription) : $parameterDescription
291298
))->build();
292299
}
293300

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,22 @@ public function testNamedArguments(): void
16401640
'Unknown parameter $z in call to method NamedArgumentsMethod\Foo::doFoo().',
16411641
42,
16421642
],
1643+
[
1644+
'Parameter #1 $i of method NamedArgumentsMethod\Foo::doFoo() expects int, string given.',
1645+
49,
1646+
],
1647+
[
1648+
'Parameter $j of method NamedArgumentsMethod\Foo::doFoo() expects int, string given.',
1649+
55,
1650+
],
1651+
[
1652+
'Parameter $i of method NamedArgumentsMethod\Foo::doBaz() is passed by reference, so it expects variables only.',
1653+
70,
1654+
],
1655+
[
1656+
'Parameter $i of method NamedArgumentsMethod\Foo::doBaz() is passed by reference, so it expects variables only.',
1657+
71,
1658+
],
16431659
]);
16441660
}
16451661

tests/PHPStan/Rules/Methods/data/named-arguments.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ public function doBar(): void
4545
3,
4646
z: 4
4747
);
48+
49+
$this->doFoo(
50+
'foo',
51+
j: 2,
52+
k: 3
53+
);
54+
55+
$this->doFoo(
56+
1,
57+
j: 'foo',
58+
k: 3
59+
);
60+
61+
}
62+
63+
public function doBaz(&$i): void
64+
{
65+
66+
}
67+
68+
public function doLorem(?\stdClass $foo): void
69+
{
70+
$this->doBaz(i: 1);
71+
$this->doBaz(i: $foo?->bar);
4872
}
4973

5074
}

0 commit comments

Comments
 (0)