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

Forbid use positional argument after named argument #7104

Closed
klimick opened this issue Dec 8, 2021 · 3 comments · Fixed by #7136
Closed

Forbid use positional argument after named argument #7104

klimick opened this issue Dec 8, 2021 · 3 comments · Fixed by #7136

Comments

@klimick
Copy link
Contributor

klimick commented Dec 8, 2021

This code will fail with fatal error: https://3v4l.org/14hEF#v8.0.13

<?php

final class Person
{
    public function __construct(
        public string $name,
        public int $age,
    ) { }
}

new Person(name: '', 0);

It would be nice if Psalm report about it:
https://psalm.dev/r/b0ed47490d

@psalm-github-bot
Copy link

I found these snippets:

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

final class Person
{
	public function __construct(
    	public string $name,
        public int $age,
    ) { }
}

new Person(name: '', 0);
Psalm output (using commit 8bd525a):

No issues!

@weirdan
Copy link
Collaborator

weirdan commented Dec 8, 2021

Yeah, that would be nice. It's not a pressing issue though, as it can be detected with php -l.

@orklah
Copy link
Collaborator

orklah commented Dec 8, 2021

It should be quite easy though. Arguments are handled here:

foreach ($args as $argument_offset => $arg) {

This is a loop that checks each argument for matching purposes (types must match, numbers must match, named argument must match, etc...)

And here:

} elseif ($arg->name && (!$function_storage || $function_storage->allow_named_arg_calls)) {

Psalm spots a named argument while looping. I guess it should only be a matter of creating a local variable flag set to true when seeing the first named argument, and then emitting an issue on the next loop if the arg is not named.

InvalidArgument should do nicely I think

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants