vimeo / psalm

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

array|list with count, unable to infer type correctly #9045

Open kkmuffme opened 1 year ago

kkmuffme commented 1 year ago

Basic case: https://psalm.dev/r/9e4abcd9ee

Separate counts makes even less sense: https://psalm.dev/r/565da938eb

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

I found these snippets:

https://psalm.dev/r/9e4abcd9ee ```php $arg * @return void */ function foo( $arg ) { $new = $arg; $count = count($arg); if ($count > 1 ) { $arg = array_filter($arg, 'is_int'); } $arg = array_values($arg); /** @psalm-trace $arg */; if ($count > 1 && $new !== $arg) { /** @psalm-trace $new */; } } ``` ``` Psalm output (using commit 7029e76): INFO: Trace - 14:29 - $arg: list INFO: Trace - 17:33 - $new: list ```
https://psalm.dev/r/565da938eb ```php $arg * @return void */ function foo( $arg ) { $new = $arg; if (count($arg) > 1 ) { $arg = array_filter($arg, 'is_int'); } $arg = array_values($arg); /** @psalm-trace $arg */; if (count($new) > 1 && $new !== $arg) { /** @psalm-trace $new */; } } ``` ``` Psalm output (using commit 7029e76): INFO: Trace - 13:29 - $arg: list INFO: Trace - 16:33 - $new: list{0?: int|string, 1?: int|string, ..., int|string>} ```