vimeo / psalm

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

Converting iterable to array with iterator_to_array causes error #11007

Open lgatting opened 1 month ago

lgatting commented 1 month ago

When converting iterable to array, psalm returns an error: https://psalm.dev/r/496d9d1084

To me it sounds that this shouldn't be a problem because it is equivalent to a foreach, which doesn't throw an error: https://psalm.dev/r/cc83c4dc5d

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

I found these snippets:

https://psalm.dev/r/496d9d1084 ```php $items */ public function a(array $items): void; } class A implements AInterface { public function a(array $items): void { // ... } } class B { public function __construct( /** @var iterable */ private readonly iterable $items, ) { } public function b(): void { $a = new A(); $a->a(iterator_to_array($this->items)); } } ``` ``` Psalm output (using commit 16b24bd): ERROR: InvalidArgument - 24:33 - Argument 1 of iterator_to_array expects Traversable|array, but iterable provided ERROR: ArgumentTypeCoercion - 24:15 - Argument 1 of A::a expects list, but parent type array provided ```
https://psalm.dev/r/cc83c4dc5d ```php $items */ public function a(array $items): void; } class A implements AInterface { public function a(array $items): void { // ... } } class B { public function __construct( /** @var iterable */ private readonly iterable $items, ) { } public function b(): void { $a = new A(); $array = []; foreach ($this->items as $item) { $array[] = $item; } $a->a($array); } } ``` ``` Psalm output (using commit 16b24bd): No issues! ```