Skip to content

Commit 7b001c8

Browse files
Report unknown parameter for implicit variadic methods (#4481)
1 parent 020adb5 commit 7b001c8

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/Rules/FunctionCallParametersCheck.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,13 @@ private function processArguments(
562562
$originalParametersByName = [];
563563
$unusedParametersByName = [];
564564
$errors = [];
565+
$isNativelyVariadic = false;
565566
foreach ($parameters as $i => $parameter) {
566567
$parametersByName[$parameter->getName()] = $parameter;
567568
$originalParametersByName[$parameter->getName()] = $originalParameters[$i];
568569

569570
if ($parameter->isVariadic()) {
571+
$isNativelyVariadic = true;
570572
continue;
571573
}
572574

@@ -603,7 +605,7 @@ private function processArguments(
603605

604606
$parametersCount = count($parameters);
605607
if (
606-
!$parametersAcceptor->isVariadic()
608+
!$isNativelyVariadic
607609
|| $parametersCount <= 0
608610
|| $isBuiltin
609611
) {

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,21 @@ public function testBug4514(): void
534534
$this->analyse([__DIR__ . '/data/bug-4514.php'], []);
535535
}
536536

537+
#[RequiresPhp('>= 8.0')]
538+
public function testBug13719(): void
539+
{
540+
$this->analyse([__DIR__ . '/data/bug-13719.php'], [
541+
[
542+
'Unknown parameter $greetings in call to function Bug13719\non_variadic.',
543+
20,
544+
],
545+
[
546+
'Unknown parameter $greetings in call to function Bug13719\implicit_variadic.',
547+
27,
548+
],
549+
]);
550+
}
551+
537552
public function testBug4530(): void
538553
{
539554
$this->analyse([__DIR__ . '/data/bug-4530.php'], []);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13719;
4+
5+
function non_variadic(string $name, ?string $greeting = null): void {
6+
var_dump($name);
7+
}
8+
9+
// Explicitly defined with variadic arguments.
10+
function explicit_variadic(string $name, ?string $greeting = null, string ...$args): void {
11+
var_dump($name);
12+
}
13+
14+
// Treated as variadic due to usage of `func_get_args()`.
15+
function implicit_variadic(string $name, ?string $greeting = null): void {
16+
var_dump(func_get_args());
17+
}
18+
19+
// PHPStan correctly detects the error ('greetings' vs 'greeting').
20+
non_variadic('my name', greetings: 'my greeting');
21+
22+
// PHPStan correctly doesn't report anything since the function
23+
// accepts variadic arguments and this is not an error.
24+
explicit_variadic('my name', greetings: 'my greeting');
25+
26+
// ISSUE: PHPStan should detect argument.unknown error here, but it doesn't.
27+
implicit_variadic('my name', greetings: 'my greeting');

0 commit comments

Comments
 (0)