Skip to content

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

Closed

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

Activity

  1. added this to the Easy fixes milestone on Mar 5, 2026
  2. phpstan-bot commented on Mar 6, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#5137

  3. github-actions commented on Apr 7, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  4. locked as resolved and limited conversation to collaborators on Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions