Skip to content

False positive: Strict comparison using === between ... will always evaluate to false. #2822

@klausi

Description

@klausi

Bug report

PHPStan does not detect an array index variable being overwritten between 2 nested if statements.

Code snippet that reproduces the problem

$tokens = $_GET;
$lastContent = 'a';

if ($tokens[$lastContent]['code'] === 1
    || $tokens[$lastContent]['code'] === 2
) {
    $lastContent = 'b';
    if ($tokens[$lastContent]['code'] === 3) {
        echo "what?";
    }
}

https://phpstan.org/r/b14bbcfb-cb91-4cec-a4ef-24b452a7aaf3

In this case the index $lastContent is overwritten with 'b', so PHPStan cannot know what is being compared here.

Actual output

+-------------------------------------------------------------------------------------+
| Line | test.php                                                                     |
+-------------------------------------------------------------------------------------+
| 10   | Strict comparison using === between 1|2 and 3 will always evaluate to false. |
+-------------------------------------------------------------------------------------+

 [ERROR] Found 1 error 

Expected output

No output because the example is correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions