Skip to content

Cannot use array destructuring on class that implements ArrayAccess #6068

@NLthijs48

Description

@NLthijs48

Bug report

Just upgraded to PHPStan 1.2.0 and ran into a possible inconsistency when using a class that implements ArrayAccess.
Accessing an array property like $row['a'] is allowed, but using ['a' => $a] = $row is not allowed. I think they both basically do the same thing right? Or is there a reason I'm missing that the destructure construct is not supported this way?

Maybe good to know that it starts to fail on level 3 and up.

Code snippet that reproduces the problem

https://phpstan.org/r/696b99c9-11c8-44a0-86d4-a12e7c677ba3

Expected output

I think it should allow destructuring of ArrayAccess instances, unless I'm missing something that makes these access operations different.

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

It helped to refactor code to support an additional case in a string union type, it quickly pointed out all the code that needed to change after updating the type definition in phpstan.neon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions