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

string is incorrectly narrowed down in if with and + or conditions #6329

Closed
herndlm opened this issue Jan 9, 2022 · 5 comments · Fixed by phpstan/phpstan-src#1016
Closed
Labels
Milestone

Comments

@herndlm
Copy link
Contributor

herndlm commented Jan 9, 2022

Bug report

PHPStan seems to be having problems narrowing down types in more complex if statements. In the case I added I use is_string and checks against null and '' to determine that this is a non-empty-string|null type. In a case it is wrongfully detected as string|null though. The example makes more sense than me here I guess :)

Could just be a limitation of PHPStan's capabilities with ifs, feel free to close then.

Code snippet that reproduces the problem

https://phpstan.org/r/ae139617-651e-4a89-8e13-824f9ce1a511

Expected output

No errors

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

Always :)

@ondrejmirtes
Copy link
Member

Don't know why it gets lost but it can definitely be solved.

@herndlm
Copy link
Contributor Author

herndlm commented Jan 10, 2022

Feel free to add the info where you are expecting the problem to be, if you think it's easy enough for others to solve :) I can try to take a look at it myself.

@ondrejmirtes
Copy link
Member

BooleanOr type narrowing is handled here in TypeSpecifier, sho that's where step debugging should begin: https://github.com/phpstan/phpstan-src/blob/b5e71db32dfd256174290b243d72cefd15768503/src/Analyser/TypeSpecifier.php#L709-L725

@herndlm
Copy link
Contributor Author

herndlm commented Jan 12, 2022

To sum up what I found out in a time-boxed debugging session:
SpecifiedTypes::intersectWith (which is called via TypeSpecifier::specifyTypesInCondition because of the BooleanOr) is ignoring the sureNotType '' which would make the string non-empty. But if I adapt that to basically not ignore it, there are still 2 sure types (string|null) which make problems when combining all of that resulting in string|null & ~''. pretty sure I'm missing something obvious, but maybe this helps somebody.
Maybe I'll continue debugging / working on this

@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 Mar 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
2 participants