vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.54k stars 659 forks source link

"Cannot call method on possibly null value" inside if that confirms value is set #11052

Open jnvsor opened 1 month ago

jnvsor commented 1 month ago

https://psalm.dev/r/6b7253ee73

When in an if that checks a nullable typed property, a following if attempting to call a method on it will fail even though we know the type is not null.

Removing the instanceof check makes it work, but that shouldn't have any effect in this case since they're all public properties.

psalm-github-bot[bot] commented 1 month ago

I found these snippets:

https://psalm.dev/r/6b7253ee73 ```php value */ if ($v->value) { /** @psalm-trace $v->value */ if ($v instanceof B && 'yay' === $v->value->yay()) { echo 'yay'; } } } ``` ``` Psalm output (using commit 16b24bd): ERROR: PossiblyNullReference - 25:53 - Cannot call method yay on possibly null value INFO: Trace - 25:9 - $v->value: C INFO: Trace - 23:5 - $v->value: C|null ERROR: MissingConstructor - 4:12 - A has an uninitialized property A::$value, but no constructor ERROR: MissingConstructor - 4:12 - B has an uninitialized property A::$value, but no constructor ```