vimeo / psalm

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

usort by nested array values doesn't work #7601

Open ozon1234 opened 2 years ago

ozon1234 commented 2 years ago

https://psalm.dev/r/dcd5b2f8f8

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

I found these snippets:

https://psalm.dev/r/dcd5b2f8f8 ```php 1], ['foo' => 2], ['foo' => 3]]; usort($arr, static function (array $a, array $b): int { return $a['foo'] <=> $b['foo']; }); ``` ``` Psalm output (using commit faad966): INFO: PossiblyUndefinedStringArrayOffset - 5:9 - Possibly undefined array offset ''foo'' is risky given expected type 'array-key'. Consider using isset beforehand. INFO: PossiblyUndefinedStringArrayOffset - 5:23 - Possibly undefined array offset ''foo'' is risky given expected type 'array-key'. Consider using isset beforehand. ```
orklah commented 2 years ago

This will work fine: https://psalm.dev/r/0086707d3d

As a default, Psalm expects you to provide docblocks to your callbacks.

However, I think this could probably be improved for known functions. Possibly with a ParamsTypeProvider (through the Psalm's plugin system).

Please ping me if you're interested in giving it a try!

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

I found these snippets:

https://psalm.dev/r/0086707d3d ```php 1], ['foo' => 2], ['foo' => 3]]; usort($arr, /** * @param array{foo: int} $a * @param array{foo: int} $b */ static function (array $a, array $b): int { return $a['foo'] <=> $b['foo']; }); ``` ``` Psalm output (using commit faad966): No issues! ```