vimeo / psalm

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

mergeArrays function InvalidReturnStatement #5823

Open vudaltsov opened 3 years ago

vudaltsov commented 3 years ago

https://psalm.dev/r/8381fec80a

I could rewrite it with generic keys and values, but then it does not take lists into account: https://psalm.dev/r/90f1dd39e0

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

I found these snippets:

https://psalm.dev/r/8381fec80a ```php $arrays * @return T */ function mergeArrays(array $arrays): array { return array_merge(...array_values($arrays)); } /** * @return list */ function getList(): array { return []; } /** * @psalm-trace $_list */ $_list = mergeArrays([getList(), getList()]); ``` ``` Psalm output (using commit 38c452a): ERROR: InvalidReturnStatement - 11:12 - The inferred type 'array' does not match the declared return type 'T:fn-mergearrays as array' for mergeArrays ERROR: InvalidReturnType - 7:12 - The declared return type 'T:fn-mergearrays as array' for mergeArrays is incorrect, got 'array' INFO: Trace - 25:1 - $_list: list ```
https://psalm.dev/r/90f1dd39e0 ```php > $arrays * @return array */ function mergeArrays(array $arrays): array { return array_merge(...array_values($arrays)); } /** * @return list */ function getList(): array { return []; } /** * @psalm-trace $_list */ $_list = mergeArrays([getList(), getList()]); ``` ``` Psalm output (using commit 38c452a): INFO: Trace - 26:1 - $_list: array ```