Skip to content

Need an sequential integer type such that array<sequential-int, string> can safely resolve to list<string> #12576

Closed

Description

@calebdw

Feature request

Hello!

We heavily use Laravel Collections in our code, but this has started causing conflicts in places where list<...>s are expected. Despite calling ->values()->all() (which ensures the data is a list), phpstan still thinks it's an array<int, string>:

https://phpstan.org/r/f872629e-145a-42de-a408-7abce701c58a

I've tried updating the values method to return static<int<0, max>, TValue> but this didn't work either. Would it be a good idea to create a new sequential-int type that represents this and can be safely converted to list<TValue>?

Or is there a better way to solve this with PHPDocs? I know it would be possible to create a Larastan extension that checks if ->all()/->toArray() is called on ->values() and could update the return type accordingly, but that feels rather overblown...

Thanks!

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

No response

Activity

  1. stof commented on Feb 11, 2025

    @stof
    Contributor

    sequential-int does not make sense as the type of a single key. Being a list is a feature impacting the array itself, not something that impacts the type of a single key.

  2. calebdw commented on Feb 11, 2025

    @calebdw
    ContributorAuthor

    Do you have any thoughts on how to solve this? I'd rather not have to wrap the collection in array_values...

  3. ondrejmirtes commented on Feb 12, 2025

    @ondrejmirtes
    Member

    Some prior art in other languages would be useful here, although PHP arrays are so weird and unique I doubt any exists.

    Two ideas:

    1. Marker interface that would live in an intersection: https://phpstan.org/r/29f8c8dc-c31e-491c-9c14-133654783be1
    2. Another generic type variable with a default: https://phpstan.org/r/15569a9d-ff64-4a8f-9cb1-34746e774a1a
  4. github-actions commented on Oct 9, 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.

  5. locked as resolved and limited conversation to collaborators on Oct 9, 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions