vimeo / psalm

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

Unable to determine the type that $b is being assigned to when using null-coalesce assignment #9615

Open kamil-tekiela opened 1 year ago

kamil-tekiela commented 1 year ago

When using null-coalesce assignment Psalm is unable to determine the type.

https://psalm.dev/r/7cda9cfa9e

But this code works and the type is guaranteed to be DateTime: https://3v4l.org/FMNLM

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

I found these snippets:

https://psalm.dev/r/7cda9cfa9e ```php
othercorey commented 1 year ago

It seems undefined variables are not defaulting to null so mixed is assumed.

robchett commented 1 year ago

In this context $b could be in scope from a require on this file. Do you have an example where $b is definitely undefined?

kamil-tekiela commented 1 year ago

Do you mean something like this? https://psalm.dev/r/b8733551e9

Message ID: @.***>

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

I found these snippets:

https://psalm.dev/r/b8733551e9 ```php