Skip to content

ArrayAccess with nullable type not working #12565

@timri

Description

@timri

Bug report

I have an Object that behaves like an array, so it implements \ArrayAccess, which works ok.
However, when my array-like-object is used as an optional (and thus nullable) property in another class, I get:
Property Wrapper::$myArrayLike (ArrayLike|null) does not accept array<int, EntryType>|ArrayLike.

See linked snippet, where the error does not occur when line 37 is changed from
public ?ArrayLike $myArrayLike;
to
public ArrayLike $myArrayLike;
i.e. making it required / non-nullable

Is this a bug in phpstan, or am I missing something?

Code snippet that reproduces the problem

https://phpstan.org/r/ff897fba-8566-4c13-a6a3-e474c01ec780

Expected output

I expected the code to not report an error, just like when the property is not nullable.

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

Yes, I could already find some subtle does-never-happen-in-reality-bugs in my code.
Thats why I want to have my code passing phpstan without ignoring errors ;-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions