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 positive when comparing strlen($string) after testing substr($string) #7555

Closed
Zombaya opened this issue Jun 30, 2022 · 4 comments · Fixed by phpstan/phpstan-src#1488
Closed
Labels
Milestone

Comments

@Zombaya
Copy link

Zombaya commented Jun 30, 2022

Bug report

A false positive is reported.

Line 9: Strict comparison using === between int<0, 1> and 2 will always evaluate to false.

Code snippet that reproduces the problem

https://phpstan.org/r/5ae35ca9-5e05-40f8-a47f-892e50895e06

Line 9 is the second if-statement.

$a = 'ab';

if (substr($a, 0, PHP_INT_MAX) === 'x') {
	exit(1);
}

if (strlen($a) === 2) {
	echo('According to phpstan, this should not print because if will always evaluate to false');
}

Expected output of the snippet

https://3v4l.org/aqFoE
According to phpstan, this should not print because if will always evaluate to false

Expected output

No warning should be shown or if a warning should be shown, it should be that it will always be true since the original string is not modified and is known to be of length 2. (Well, in this specific case, not if $a would be a dynamic string returned from a function.)

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

Not today, but the previous weeks totally. In combination with the deprecation-rules, this tool has helped me a lot to prepare our codebase to upgrade some critical libraries before upgrading to the next major release.

It's the first issue we encounter and I just wanted to report it so you can strive to make this already excellent tool even more sublime.

@ondrejmirtes
Copy link
Member

Thank you for your kind words! ❤️

This looks like @staabm's bug because of the substr involvement: https://phpstan.org/r/cfeff81b-48aa-4012-8a07-571745cb85ec

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jun 30, 2022
@staabm
Copy link
Contributor

staabm commented Jun 30, 2022

I will have a look (will be on holiday starting tomorrow... might take 2 weeks until I can start with it)

@Zombaya
Copy link
Author

Zombaya commented Jul 1, 2022

Kudos to everyone involved, this has been fixed blazingly fast 🚀.

👍

@github-actions
Copy link

github-actions bot commented Aug 2, 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 Aug 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
3 participants