1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Fix #5506 – make array_values and iterator_to_array respect nested templates

This commit is contained in:
Matt Brown 2021-03-29 09:37:42 -04:00
parent d57dde0d15
commit 17e147935a
3 changed files with 68 additions and 31 deletions

View File

@ -23,42 +23,53 @@ class ArrayValuesReturnTypeProvider implements \Psalm\Plugin\EventHandler\Functi
return Type::getMixed(); return Type::getMixed();
} }
$first_arg = $call_args[0]->value ?? null; $first_arg = isset($call_args[0]->value) ? $call_args[0]->value : null;
$first_arg_array = $first_arg if (!$first_arg) {
&& ($first_arg_type = $statements_source->node_data->getType($first_arg))
&& $first_arg_type->hasType('array')
&& ($array_atomic_type = $first_arg_type->getAtomicTypes()['array'])
&& ($array_atomic_type instanceof Type\Atomic\TArray
|| $array_atomic_type instanceof Type\Atomic\TKeyedArray
|| $array_atomic_type instanceof Type\Atomic\TList)
? $array_atomic_type
: null;
if (!$first_arg_array) {
return Type::getArray(); return Type::getArray();
} }
if ($first_arg_array instanceof Type\Atomic\TKeyedArray) { $first_arg_type = $statements_source->node_data->getType($first_arg);
$first_arg_array = $first_arg_array->getGenericArrayType();
if (!$first_arg_type) {
return Type::getArray();
} }
if ($first_arg_array instanceof Type\Atomic\TArray) { $atomic_types = $first_arg_type->getAtomicTypes();
if ($first_arg_array instanceof Type\Atomic\TNonEmptyArray) {
return new Type\Union([ $return_atomic_type = null;
new Type\Atomic\TNonEmptyList(
clone $first_arg_array->type_params[1] while ($atomic_type = \array_shift($atomic_types)) {
) if ($atomic_type instanceof Type\Atomic\TTemplateParam) {
]); $atomic_types = \array_merge($atomic_types, $atomic_type->as->getAtomicTypes());
continue;
} }
return new Type\Union([ if ($atomic_type instanceof Type\Atomic\TKeyedArray) {
new Type\Atomic\TList( $atomic_type = $atomic_type->getGenericArrayType();
clone $first_arg_array->type_params[1] }
)
]); if ($atomic_type instanceof Type\Atomic\TArray) {
if ($atomic_type instanceof Type\Atomic\TNonEmptyArray) {
$return_atomic_type = new Type\Atomic\TNonEmptyList(
clone $atomic_type->type_params[1]
);
} else {
$return_atomic_type = new Type\Atomic\TList(
clone $atomic_type->type_params[1]
);
}
} elseif ($atomic_type instanceof Type\Atomic\TList) {
$return_atomic_type = $atomic_type;
} else {
return Type::getArray();
}
} }
return new Type\Union([clone $first_arg_array]); if (!$return_atomic_type) {
throw new \UnexpectedValueException('This should never happen');
}
return new Type\Union([$return_atomic_type]);
} }
} }

View File

@ -34,15 +34,20 @@ class IteratorToArrayReturnTypeProvider implements \Psalm\Plugin\EventHandler\Fu
return Type::getMixed(); return Type::getMixed();
} }
if (($first_arg_type = $statements_source->node_data->getType($call_args[0]->value)) if (($first_arg_type = $statements_source->node_data->getType($call_args[0]->value))) {
&& $first_arg_type->hasObjectType()
) {
$key_type = null; $key_type = null;
$value_type = null; $value_type = null;
$codebase = $statements_source->getCodebase(); $codebase = $statements_source->getCodebase();
foreach ($first_arg_type->getAtomicTypes() as $call_arg_atomic_type) { $atomic_types = $first_arg_type->getAtomicTypes();
while ($call_arg_atomic_type = array_shift($atomic_types)) {
if ($call_arg_atomic_type instanceof Type\Atomic\TTemplateParam) {
$atomic_types = \array_merge($atomic_types, $call_arg_atomic_type->as->getAtomicTypes());
continue;
}
if ($call_arg_atomic_type instanceof Type\Atomic\TNamedObject if ($call_arg_atomic_type instanceof Type\Atomic\TNamedObject
&& AtomicTypeComparator::isContainedBy( && AtomicTypeComparator::isContainedBy(
$codebase, $codebase,

View File

@ -790,6 +790,27 @@ class ConditionalReturnTypeTest extends TestCase
/** @psalm-suppress MixedArgument */ /** @psalm-suppress MixedArgument */
echo (new Request)->getParams(Request::SOURCE_GET)["a"];' echo (new Request)->getParams(Request::SOURCE_GET)["a"];'
], ],
'conditionalArrayValues' => [
'<?php
/**
* @psalm-pure
* @template TValue
* @template TIterable of ?iterable<TValue>
* @param TIterable $iterable
* @return (TIterable is null ? null : list<TValue>)
*/
function toList(?iterable $iterable): ?array {
if (null === $iterable) {
return null;
}
if (is_array($iterable)) {
return array_values($iterable);
}
return iterator_to_array($iterable, false);
}'
],
]; ];
} }
} }