vimeo / psalm

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

Incorrect RedundantCondition with null safe property access and null coalesce #10316

Open tux-rampage opened 9 months ago

tux-rampage commented 9 months ago

When accessing properties/methods with the null safe operator, the result seems to be evaluated incorrectly. The resulting type should be <Type>|null, but psalm evaluates only <Type>.

https://psalm.dev/r/14b17f9fa4

psalm-github-bot[bot] commented 9 months ago

I found these snippets:

https://psalm.dev/r/14b17f9fa4 ```php a?->copy()->a() ?? ''; return $value; } public function trace(): string { /** @psalm-trace $value */ $value = $this->a?->copy()->a(); return $value; } } ``` ``` Psalm output (using commit 147505c): ERROR: RedundantCondition - 13:18 - Type string for $229 is never null ERROR: TypeDoesNotContainNull - 13:44 - Cannot resolve types for $229 - string does not contain null INFO: Trace - 20:9 - $value: string ```
aleho commented 7 months ago

Maybe this is related (seen in Symfony):

https://psalm.dev/r/46409b9f5a

psalm-github-bot[bot] commented 7 months ago

I found these snippets:

https://psalm.dev/r/46409b9f5a ```php getPrevious()) && $authException?->getToken() ) { return new Response($prev); } return new Response(null); } } ``` ``` Psalm output (using commit 0e43c44): ERROR: TypeDoesNotContainNull - 35:16 - AuthenticationException does not contain null ERROR: RedundantCondition - 35:16 - Type AuthenticationException for $authException is never null ERROR: RedundantCondition - 34:13 - Type AuthenticationException for $authException is never null ```