vimeo / psalm

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

False positive for ArrayAccess and Traversable (UndefinedInterfaceMethod PossibleRawObjectIteration ) #7928

Open jordisala1991 opened 2 years ago

jordisala1991 commented 2 years ago

We are trying to detect if a variable can be passed through a foreach and can be acces directly at a position (to do an unset), here is the code:

https://github.com/sonata-project/form-extensions/blob/2.x/src/EventListener/ResizeFormListener.php#L167-L178

When moving to Psalm level 2 we get this error:

ERROR: PossibleRawObjectIteration - src/EventListener/ResizeFormListener.php:178:18 - Possibly undesired iteration over regular object ArrayAccess (see https://psalm.dev/208)
        foreach ($data as $name => $child) {

Related PR: https://github.com/sonata-project/form-extensions/pull/319 We tried to reproduce it: https://psalm.dev/r/c95d52a172

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

I found these snippets:

https://psalm.dev/r/c95d52a172 ```php $value) { unset($data[$name]); } } ``` ``` Psalm output (using commit f960d71): INFO: MixedAssignment - 13:32 - Unable to determine the type that $value is being assigned to ERROR: UndefinedInterfaceMethod - 14:15 - Method Traversable::offsetGet does not exist INFO: Trace - 13:5 - $data: Traversable|array INFO: UnusedForeachValue - 13:32 - $value is never referenced or the value is not used ```
AndrolGenhald commented 2 years ago

Some of these conditionals end up being a bit difficult, it looks like it works if you separate it out like this.

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

I found these snippets:

https://psalm.dev/r/73779a3244 ```php $value) { unset($data[$name]); } } ``` ``` Psalm output (using commit f960d71): INFO: MixedAssignment - 11:32 - Unable to determine the type that $value is being assigned to INFO: Trace - 11:5 - $data: Traversable&ArrayAccess|array INFO: UnusedForeachValue - 11:32 - $value is never referenced or the value is not used ```
orklah commented 2 years ago

Note: the original psalm.dev link now reproduce the issue