Skip to content

Incorrect type-narrowing when using strict rules #14446

@syranide

Description

@syranide

Bug report

PHPStan reports Strict comparison using === between false and true will always evaluate to false. for $initial even though it can actually be both true or false. The variable is very clearly never changed inside the function, and the function always returns. This is trivially proven by running the script in CLI and seeing that it shows both true and false in the console.

It seems that when $current is narrowed in the if-statement then $initial also gets narrowed for some reason, despite being unrelated apart from being the initial source of the value. This only happens if $initial is used to set the value of $current, so I assume there's some underlying dependency that is incorrectly retained.

This error only occurs when "Strict rules" is enabled. I'm not 100% sure, but I believe this bug was introduced "quite recently" (maybe a few months ago?) as this code didn't previously have this problem.

Code snippet that reproduces the problem

https://phpstan.org/r/8b0e6c11-886f-43f9-9cad-0876d033f330

Expected output

PHPStan should not report an error, the code is valid.

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

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions