Skip to content

"Access to an undefined static property" with null coalescing default #6809

@lupinitylabs

Description

@lupinitylabs

Bug report

When accessing a class property that does not exist, PHPStan reports a "Access to an undefined (static) property" error even if this is accounted for by using the null coalescing operator for providing a default, i.e. $this->missingProperty ?? false.

Code snippet that reproduces the problem

Issue I originally encountered, using a trait accessing static properties of its class:
https://phpstan.org/r/688928a8-a15c-4f1f-bedb-bb80412d7a94

Minimal example without a trait and non-static:
https://phpstan.org/r/6876d018-2c67-4d7c-8bd0-e519caa7bd87

Workaround to make PHPStan happy:
https://phpstan.org/r/c918d769-56c1-4dda-b0fc-6a906525b439

Expected output

I feel that while the find is not incorrect, it is irrelevant in this case, because as seen in the workaround example, PHPStan does not report the missing property if it is checked by isset(), either.

I have not seen any suggestions that using null-coalescing operators to check if an expression results in an existing, non-null value is considered a bad practice, and if it was, I would not expect it to be a PHPStan level 0 rule.

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

I totally love PHPStan :D Already answered yesterday that over here: https://twitter.com/LupinityLabs/status/1502073054641405956 ;-)
Thanks for your hard work!

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