Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive InvalidNamedArgument for named arguments to variadic functions #4563

Closed
duskwuff opened this issue Nov 16, 2020 · 3 comments · Fixed by #4575
Closed

False positive InvalidNamedArgument for named arguments to variadic functions #4563

duskwuff opened this issue Nov 16, 2020 · 3 comments · Fixed by #4575

Comments

@duskwuff
Copy link
Contributor

Psalm doesn't recognize that variadic functions can be passed arbitrary named arguments:

https://psalm.dev/r/ad5fcd9926

I think this fixes it, but I'm not familiar enough with the codebase to know if it might have unwanted side effects. It might also be worth considering an annotation to indicate whether a variadic parameter should be allowed to handle named parameters.

diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php
index 1ce336501..b0c6811ff 100644
--- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php
+++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php
@@ -665,6 +665,10 @@ class ArgumentsAnalyzer
                         $arg_function_params = [$candidate_param];
                         break;
                     }
+                    if ($candidate_param->is_variadic) {
+                        $arg_function_params = [$candidate_param];
+                        break;
+                    }
                 }
 
                 if (!$arg_function_params) {
@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/ad5fcd9926
<?php

function named_variadic(int ...$args) : void {
    var_export($args);
}

named_variadic(named_argument: 1);
Psalm output (using commit 4e8fb9c):

ERROR: InvalidNamedArgument - 7:16 - Parameter $named_argument does not exist on function named_variadic

@muglug
Copy link
Collaborator

muglug commented Nov 16, 2020

I think this fixes it, but I'm not familiar enough with the codebase to know if it might have unwanted side effects

If it doesn't break any tests I'm usually happy – PR it!

@duskwuff
Copy link
Contributor Author

OK, requested. :) Added some basic tests too.

muglug pushed a commit that referenced this issue Nov 16, 2020
LukasReschke pushed a commit to LukasReschke/psalm that referenced this issue Nov 17, 2020
danog pushed a commit to danog/psalm that referenced this issue Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants