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

Variable marked as always defined, even though it might be unset #6997

Closed
icatalina opened this issue Apr 7, 2022 · 5 comments
Closed

Variable marked as always defined, even though it might be unset #6997

icatalina opened this issue Apr 7, 2022 · 5 comments
Labels

Comments

@icatalina
Copy link

icatalina commented Apr 7, 2022

Bug report

This seems like a regression from 1.5.3, we didn't have this issue before.

$whereParts = [];

if (!$foo['a']) {
    $whereParts[] = 'something';
}

if (!$foo['b']) {
    $count = 3;
    $whereParts[] = 'example';
}

if (empty($whereParts)) {
    return [];
}

return isset($count) ? $count : 0;

$count is marked as always defined, it can clearly be unset.

Code snippet that reproduces the problem

https://phpstan.org/r/81e9327a-136e-4a7f-908e-b571c173d32c

Expected output

No error

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

80% of the time, it does all the time!

@icatalina
Copy link
Author

icatalina commented Apr 7, 2022

Ok, after a bit more digging, in 1.5.3 this code doesn't report:

public function sayHello(array $foo): void
{
    $whereParts = [];

    if (!$foo['a']) {
        $whereParts[] = 'something';
    }

    if (!$foo['b']) {
        $count = 3;
        // notice the 2 pushes to the array
        $whereParts[] = 'example';
        $whereParts[] = 'bar';
    }

    if (empty($whereParts)) {
        return [];
    }

    return isset($count) ? $count : 0;
}

but the original does

@ondrejmirtes ondrejmirtes added this to the Dependent types milestone Apr 7, 2022
@phpstan-bot
Copy link
Contributor

@icatalina After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-22: Variable $count in isset() always exists and is not nullable.
+No errors

@icatalina
Copy link
Author

👏

@herndlm
Copy link
Contributor

herndlm commented Jul 26, 2022

Ondrej most likely wants a regression test for it before closing it :)

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants