Skip to content

[bug] comparison does not work correctly with sorted arrays #6467

Closed
@adaniloff

Description

Bug report

Version: 1.4.2
Issue: Comparison does not work correctly with sorted arrays

Given the following code block I got the following error message.

Code snippet that reproduces the problem

(See at: https://phpstan.org/r/5fadc22b-916a-4cdd-9752-45d0fbbeb576)

        $values = $sortedValues = [0.5, 1, 2, 0.8, 0.4];
        sort($sortedValues);
        $expected = $sortedValues[2] + 0.05; // note here that we get the value from the sorted array
        foreach (array_fill(0, 5, null) as $index => $null) {
             $success = $values[$index] < $expected;
        }

Actual output

The following error (which is wrong):

Comparison operation "<" between 0.4|0.5|0.8|1|2 and 2.05 is always true.

Expected output

The output should be fine and not throw an error since the sorted array 3rd element is 0.8, which is not 2.05 but 1.3.

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

Yes of course :)

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions