🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

Teach phpstan about about equivalence of two types of object property access #14847

Description

@mind-bending-forks

Feature request

In php, object properties can be accessed using the syntax $obj->{$name} where $name is a string (or integer or float it seems... Not sure what the exact rules are. Possibly, some numeric strings are automatically converted to integers/floats, etc.) Properties can also be accessed using the syntax $obj->somename but in this case, there are stricter rules about the format of the name. somename must be compatible with php's rules and cannot start with or contain certain special characters such as $ or ..

Given the above, for some property names, there are two equivalent ways of accessing the property. Take the example n. Given an object $ojb, $obj->n is equivalent to $obj->{'n'}.

Phpstan doesn't appear to understand this equivalence in all contexts, which can lead to some false positives. See for example:

https://phpstan.org/r/a4747b5b-1a1d-4cac-9e5d-00eb4ffafaef

In the first test, dead code is not identified, and in the second, type narrowing when accessing a property one way is not applied when accessing it another.

It would be great if phpstan's understanding of this equivalence could be made more robust.

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

Yes!

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