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:
parent
83e22a10f8
commit
c765d0e969
@ -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
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user