1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #1307 - improve generator array type

This commit is contained in:
Matthew Brown 2019-02-09 15:48:15 -05:00
parent 83e22a10f8
commit c765d0e969
3 changed files with 24 additions and 6 deletions

View File

@ -225,6 +225,10 @@ class ReturnTypeCollector
$value_type = null;
foreach ($yield_types as $type) {
if ($type instanceof Type\Atomic\ObjectLike) {
$type = $type->getGenericArrayType();
}
if ($type instanceof Type\Atomic\TArray
|| $type instanceof Type\Atomic\TIterable
|| $type instanceof Type\Atomic\TGenericObject

View File

@ -1283,12 +1283,17 @@ class ExpressionAnalyzer
$yield_from_type = null;
foreach ($stmt->expr->inferredType->getTypes() as $atomic_type) {
if ($yield_from_type === null
&& $atomic_type instanceof Type\Atomic\TGenericObject
&& strtolower($atomic_type->value) === 'generator'
&& isset($atomic_type->type_params[3])
) {
$yield_from_type = clone $atomic_type->type_params[3];
if ($yield_from_type === null) {
if ($atomic_type instanceof Type\Atomic\TGenericObject
&& strtolower($atomic_type->value) === 'generator'
&& isset($atomic_type->type_params[3])
) {
$yield_from_type = clone $atomic_type->type_params[3];
} elseif ($atomic_type instanceof Type\Atomic\TArray) {
$yield_from_type = clone $atomic_type->type_params[1];
} elseif ($atomic_type instanceof Type\Atomic\ObjectLike) {
$yield_from_type = $atomic_type->getGenericValueType();
}
} else {
$yield_from_type = Type::getMixed();
}

View File

@ -247,6 +247,15 @@ class Php70Test extends TestCase
],
'error_levels' => ['MixedAssignment'],
],
'yieldFromArray' => [
'<?php
/**
* @return Generator<int, int, mixed, void>
*/
function Bar() : Generator {
yield from [1];
}'
],
'generatorWithNestedYield' => [
'<?php
function other_generator(): Generator {