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

False positives for unreachable detection of nested optional elements in an array #4903

Closed
zonuexe opened this issue Apr 26, 2021 · 4 comments
Labels
Milestone

Comments

@zonuexe
Copy link
Contributor

zonuexe commented Apr 26, 2021

Bug report

The following code is detected in the latest PHPStan as “Unreachable statement - code above always terminates.”

Maybe MutatingScope::resolveType() needs to recursively inspect the parent element.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

/**
 * @param array{a?:array{b:bool}} $options
 */
function f(array $options): bool
{
    if (isset($options['a']['b'])) {
        return true;
    }
	
    return false;
}

var_dump(f([])); // returns false

https://phpstan.org/r/c877f393-c5d5-4b81-9655-709ba6b6a85b

Expected output

The last statement is reachable at run time, so no message should be printed.

https://3v4l.org/fjfml

@phpstan-bot
Copy link
Contributor

@zonuexe After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-12: Unreachable statement - code above always terminates.
+ 6: Function f() should return bool but return statement is missing.
+19: Function g() should return bool but return statement is missing.
Full report
Line Error
6 Function f() should return bool but return statement is missing.
19 Function g() should return bool but return statement is missing.

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@3b17acc

@zonuexe
Copy link
Contributor Author

zonuexe commented Jan 31, 2022

Great! Thank you!

@github-actions
Copy link

github-actions bot commented Mar 4, 2022

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 Mar 4, 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

3 participants