Skip to content

Named argument validation ignored on functions using func_get_args #13719

@patrickcarlohickman

Description

@patrickcarlohickman

Bug report

In PHP, if you call a non-variadic function/method using named arguments, but use an argument name that doesn't exist, you'll get a fatal "Unknown named parameter" error. Normally, PHPStan will detect this issue and report an argument.unknown error.

However, if the function is non-variadic but it uses func_get_args() inside, PHPStan will treat it as a variadic function. Because PHPStan thinks its a variadic function, it skips the named argument check since variadic functions can take any named argument without throwing an error.

This leads to a scenario where there is a detectable fatal error, but PHPStan is not catching it.

// THIS WORKS

function non_variadic(string $name, ?string $greeting = null): void {
    var_dump($name);
}

// PHPStan correctly detects the error ('greetings' vs 'greeting').
non_variadic('my name', greetings: 'my greeting');
// THIS ALSO WORKS

// Explicitly defined with variadic arguments.
function explicit_variadic(string $name, ?string $greeting = null, string ...$args): void {
    var_dump($name);
}

// PHPStan correctly doesn't report anything since the function accepts variadic
// arguments and this will not cause an error.
explicit_variadic('my name', greetings: 'my greeting');
// ISSUE HERE

// PHPStan treats as variadic due to `func_get_args()`.
function implicit_variadic(string $name, ?string $greeting = null): void {
    var_dump(func_get_args());
}

// This will cause a PHP fatal error.
// PHPStan should detect argument.unknown error, but it doesn't.
implicit_variadic('my name', greetings: 'my greeting');

In the code above, the implicit_variadic() function is not a real variadic function, so calling it with invalid named arguments will cause a PHP error. Since it is not defined as a variadic function, the argument names should still be checked by PHPStan to catch this error.

Code snippet that reproduces the problem

https://phpstan.org/r/2b0553a1-cbd3-408c-a073-34f8ad6f7e4f

Expected output

PHPStan should detect an issue with the code but doesn't. PHPStan should not skip named argument validation for implicit variadic functions.

Did PHPStan help you today? Did it make you happy in any way?

Absolutely! :)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions