1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Fix #2225 - iterator_to_array can return a list

This commit is contained in:
Matthew Brown 2019-10-10 23:47:11 -04:00
parent 0556f28a9f
commit 1a1b3a10a9
2 changed files with 23 additions and 0 deletions

View File

@ -63,6 +63,14 @@ class IteratorToArrayReturnTypeProvider implements \Psalm\Plugin\Hook\FunctionRe
}
if ($value_type) {
if (isset($call_args[1]->value->inferredType)
&& ((string) $call_args[1]->value->inferredType === 'false')
) {
return new Type\Union([
new Type\Atomic\TList($value_type),
]);
}
return new Type\Union([
new Type\Atomic\TArray([
$key_type

View File

@ -991,6 +991,21 @@ class FunctionCallTest extends TestCase
'$a' => 'array<int, string>',
],
],
'iteratorToArrayWithGetIteratorReturningList' => [
'<?php
class C implements IteratorAggregate {
/**
* @return Traversable<int,string>
*/
public function getIterator() {
yield 1 => "1";
}
}
$a = iterator_to_array(new C, false);',
'assertions' => [
'$a' => 'list<string>',
],
],
'arrayColumnInference' => [
'<?php
function makeMixedArray(): array { return []; }