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

Assumed filter_var return type can be incorrect #10561

Closed
pontus-mp opened this issue Jan 17, 2024 · 2 comments · Fixed by #10621
Closed

Assumed filter_var return type can be incorrect #10561

pontus-mp opened this issue Jan 17, 2024 · 2 comments · Fixed by #10621

Comments

@pontus-mp
Copy link

Psalm gets a bit confused about the return value from filter_var when called with FILTER_VALIDATE_INT, a numeric value and some options.

https://psalm.dev/r/c1579d2079

Copy link

I found these snippets:

https://psalm.dev/r/c1579d2079
<?php
// validate(50) returns 50
// validate(9000) returns NULL
function validate(mixed $c): ?int {
    // This is_numeric check makes psalm confused about $ret
    if (is_numeric($c))
    {
        $ret = filter_var($c, FILTER_VALIDATE_INT, [
            'options' => [
                'default'   => null,
                'min_range' => 1,
                'max_range' => 100,
            ],
        ]);
        if ($ret !== null) {
            return $ret;
        }
    }
    return null;
}
Psalm output (using commit 64868c5):

ERROR: TypeDoesNotContainType - 15:13 - Type null for $ret is always !null

ERROR: NoValue - 16:13 - All possible types for this return were invalidated - This may be dead code

@NicolasCARPi
Copy link
Contributor

This bug is introduced in #10498 and the PR author indicates:

I only handled the most commonly used basic ones. This is just a starting point and can be easily improved with additional types once errors/issues are reported.

So here we are, reporting issues and gently pinging @kkmuffme to ask if they would have the time to look into it :)

Downgrading to 5.18.0 is the current workaround if one doesn't want to add ignore comments.

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