Skip to content

Iterating over all except the last element of a list<T> reports that the offset might not exist #14234

@TimWolla

Description

@TimWolla

Bug report

When iterating over a list<T> using a for loop from 0 until count($list) - 1:

for ($i = 0; $i < count($parts) - 1; $i++)

to handle all but the last element, PHPStan reports:

Offset int<0, max> might not exist on non-empty-list<string>.

It works correctly when iterating over all elements. Apparently PHPStan does not understand that the range stays within the bounds when performing the subtraction.

Code snippet that reproduces the problem

https://phpstan.org/r/0fc0f7ee-7d69-4c45-8ae9-e7204ad89da3

Expected output

No error.

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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions