Skip to content

False error on a boolean && condition depending on operand order #14787

@uri2x

Description

@uri2x

Bug report

PHPStan reports the following error on the second if condition in the linked snippet:

Right side of && is always false.

I believe this is a false positive.

After the first condition, PHPStan can infer that !($a && $b && $c) is true, meaning at least one of $a, $b, or $c is false. However, that does not imply that $b must be false when $c is true.

For example, this input reaches the second branch:

$a = false;
$b = true;
$c = true;

In that case, the first condition is false, but the second condition is true.

Also, the issue appears to depend on operand order. The warning is reported for:

$c && $b

but not for:

$b && $c

These expressions are logically equivalent in this case, so I would not expect PHPStan to report an error for only one of them.

Code snippet that reproduces the problem

https://phpstan.org/r/bbf2c2c1-fb4b-4c5d-857d-47d4eb1e3873

Expected output

No error should be reported

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
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions