vimeo / psalm

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

Psalm drops `null` from variable values in chain method calls after `?->` #6057

Open fluffycondor opened 3 years ago

fluffycondor commented 3 years ago

https://psalm.dev/r/4ca48f577e

Expected: $bazString: string|null Got: $bazString: string

psalm-github-bot[bot] commented 3 years ago

I found these snippets:

https://psalm.dev/r/4ca48f577e ```php getBar()?->getBaz(); $bazString = $foo->getBar()?->getBaz()->toString(); /** @psalm-trace $baz */; /** @psalm-trace $bazString */; ``` ``` Psalm output (using commit d505a69): INFO: Trace - 28:25 - $baz: Baz|null INFO: Trace - 29:31 - $bazString: string ```
ndench commented 1 year ago

A workaround for this is to use the optional chaining operator on every method call in the chain:

https://psalm.dev/r/203fa897fc

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

I found these snippets:

https://psalm.dev/r/203fa897fc ```php getBar()?->getBaz(); $bazString = $foo->getBar()?->getBaz()?->toString(); /** @psalm-trace $baz */; /** @psalm-trace $bazString */; ``` ``` Psalm output (using commit 902a019): INFO: Trace - 28:25 - $baz: Baz|null INFO: Trace - 29:31 - $bazString: null|string ```