Skip to content

Unexpected conversion of list<T> to array<T> #12131

@sivlev

Description

@sivlev

Bug report

If I have a class with an array property declared as list<T> or non-empty-list<T> and if I try to assign a value to an existing element with a non-zero index in that list, e.g. $array[1] = 1, then I get an assign.propertyType error:
non-empty-array<int<0, max>, int> might not be a list.

If the assignment is done to the zero-index element, e.g. $array[0] = 1, then it does not trigger an error. Adding a limit-checking condition, e.g.
if ($index < 0 || $index >= count($array)) return;
does not help.

Code snippet that reproduces the problem

https://phpstan.org/r/a86e92a1-bb03-472f-bce4-f103ddbaf457

Expected output

No conversion from list to array is expected.

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