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

iterator_to_array must always return array-key (#5400)

This commit is contained in:
orklah 2021-03-16 18:45:34 +01:00 committed by GitHub
parent 1a3ff5676a
commit ff5a6cb297
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -2,8 +2,9 @@
namespace Psalm\Internal\Provider\ReturnTypeProvider;
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Type\Atomic\TTemplateParam;
use function array_shift;
use function assert;
use PhpParser;
use Psalm\Internal\Analyzer\Statements\Block\ForeachAnalyzer;
use Psalm\Internal\Type\Comparator\AtomicTypeComparator;
use Psalm\Internal\Codebase\InternalCallMapHandler;
@ -86,6 +87,15 @@ class IteratorToArrayReturnTypeProvider implements \Psalm\Plugin\EventHandler\Fu
$key_type = Type::getArrayKey();
}
if ($key_type->isSingle() && $key_type->hasTemplate()) {
$template_types = $key_type->getTemplateTypes();
$template_type = array_shift($template_types);
if ($template_type->as->hasMixed()) {
$template_type->as = Type::getArrayKey();
$key_type = new Type\Union([$template_type]);
}
}
return new Type\Union([
new Type\Atomic\TArray([
$key_type,

View File

@ -235,6 +235,19 @@ class Php71Test extends TestCase
return $i;
}',
],
'iterator_to_arrayMixedKey' => [
'<?php
/**
* @template TKey
* @template TValue
* @param Traversable<TKey, TValue> $traversable
* @return array<TValue>
*/
function toArray(Traversable $traversable): array
{
return iterator_to_array($traversable);
}',
],
'noReservedWordInDocblock' => [
'<?php
/**