Skip to content

False positive when comparing strlen($string) after testing substr($string) #7555

@Zombaya

Description

@Zombaya

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions