vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.56k stars 660 forks source link

Optional before required function parameters not recognized #5253

Open ohader opened 3 years ago

ohader commented 3 years ago

https://psalm.dev/r/82728ccc0b

function whatever(string $optional = 'optional', bool $required): void
{
    if ($required) {
        echo $optional;
    }
}

It would be great if Psalm could report this issue (similar to https://www.jetbrains.com/help/phpstorm/php-optional-before-required-parameter.html).

psalm-github-bot[bot] commented 3 years ago

I found these snippets:

https://psalm.dev/r/82728ccc0b ```php
jawira commented 2 years ago

This would be very useful, mainly because it was deprecated in PHP 8: Deprecate required parameters after optional parameters in function/method signatures

orklah commented 2 years ago

To be discussed: I'm not actually sure it should be the role of Psalm to report this.

It seems to me that code style rules are perfectly equipped to enforce this kind of things

jawira commented 2 years ago

To people working on legacy projects, Rector has a rule to fix this: OptionalParametersAfterRequiredRector

ctrl-f5 commented 4 months ago

I'm not actually sure it should be the role of Psalm to report this.

It seems to me that code style rules are perfectly equipped to enforce this kind of things

This is not code styling, this is actual code that due to the deprecation will behave differently than stated.

function test($foo = null, $bar) {
    return $bar;
}

test(bar: 6); // error: $foo is required

This reads like it should work, but it acutally does not.