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

array_pop(): Unreachable statement - code above always terminates. #3875

Closed
mahagr opened this issue Sep 19, 2020 · 4 comments · Fixed by phpstan/phpstan-src#333
Closed

array_pop(): Unreachable statement - code above always terminates. #3875

mahagr opened this issue Sep 19, 2020 · 4 comments · Fixed by phpstan/phpstan-src#333
Labels
Milestone

Comments

@mahagr
Copy link

mahagr commented Sep 19, 2020

Bug report (version 0.12.42)

If you have a queue with single item in it, phpstan seems to think that array_pop() returns null.

Code snippet that reproduces the problem

https://phpstan.org/r/57010e6f-71f7-4b56-9e14-e6b55b87cf47

function foo(): void
{
    $queue = ['asd'];
    $list = [];
    do {
        $current = array_pop($queue);
        if ($current === null) {
            break;
        }

        $list[] = $current;
    } while ($queue);
}

Output

Line 10: Unreachable statement - code above always terminates.

Expected output

No error

@mergeable
Copy link

mergeable bot commented Sep 19, 2020

This bug report is missing a link to reproduction on phpstan.org.
It will most likely be closed after manual review.

@jlherren
Copy link

jlherren commented Oct 7, 2020

phpstan/phpstan-src#333 should fix this. However PHPStan will still (correctly) report that the if-condition is always false. The do-while-loop will only ever execute once, thus the comparison will always be 'asd' === null.

@mahagr
Copy link
Author

mahagr commented Oct 8, 2020

Yup, I just quickly stripped out some code to make a working example. Items were being pushed to the queue from within the loop.

ondrejmirtes added a commit that referenced this issue Oct 9, 2020
phpstan/phpstan-src@ae26b54 Fix type inference in assignment with side-effects
phpstan/phpstan-src@81b2385 Add test for fixed bug #3875
phpstan/phpstan-src@f32a30f Add test for fixed bug #2611
phpstan/phpstan-src@5ef1f3a Add test for fixed bug #3548
@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 May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants