vimeo / psalm

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

`InvalidDocblock` while using conditional return with union type #8305

Open fluffycondor opened 2 years ago

fluffycondor commented 2 years ago

https://psalm.dev/r/3faa0adee6

Enclosing union types in parenthesis solves the problem, but is this the way it should work? https://psalm.dev/r/9c49548588

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

I found these snippets:

https://psalm.dev/r/3faa0adee6 ```php &Iterator : Traversable&Iterator) */ function foo($bar) {} ``` ``` Psalm output (using commit 85fe7e8): ERROR: InvalidDocblock - 8:1 - Saw : outside of object-like array in docblock for foo INFO: UnusedParam - 8:14 - Param $bar is never referenced in this method INFO: MissingReturnType - 8:10 - Method foo does not have a return type, expecting void ```
https://psalm.dev/r/9c49548588 ```php &Iterator) : (Traversable&Iterator)) */ function foo($bar) {} ``` ``` Psalm output (using commit 85fe7e8): INFO: UnusedParam - 8:14 - Param $bar is never referenced in this method ERROR: InvalidReturnType - 6:18 - No return statements were found for method foo but return type 'Traversable&Iterator' was expected ```
AndrolGenhald commented 2 years ago

Actually union types work fine, it's only intersections that cause problems.

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

I found these snippets:

https://psalm.dev/r/6187b08188 ```php
https://psalm.dev/r/d8347c3bd6 ```php
weirdan commented 1 year ago

Unrelated note: Traversable<int, stdClass>&Iterator can be simplified to Iterator<int, stdClass>