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

Fix inference for empty arrays

This commit is contained in:
Matt Brown 2021-05-03 12:40:14 -04:00
parent eb71506a59
commit 881dbf86e1
2 changed files with 24 additions and 4 deletions

View File

@ -33,11 +33,20 @@ class ArrayTypeComparator
) : bool {
$all_types_contain = true;
$is_empty_array = $input_type_part->getId() === 'array<empty, empty>';
$is_empty_array = $input_type_part->equals(
new Type\Atomic\TArray([
new Type\Union([new Type\Atomic\TEmpty()]),
new Type\Union([new Type\Atomic\TEmpty()])
]),
false
);
if ($is_empty_array
&& ($container_type_part instanceof Type\Atomic\TArray
|| $container_type_part instanceof Type\Atomic\TKeyedArray)
&& !$container_type_part instanceof Type\Atomic\TNonEmptyArray
&& (($container_type_part instanceof Type\Atomic\TArray
&& !$container_type_part instanceof Type\Atomic\TNonEmptyArray)
|| ($container_type_part instanceof Type\Atomic\TKeyedArray
&& !$container_type_part->isNonEmpty())
)
) {
return true;
}

View File

@ -294,6 +294,17 @@ class TKeyedArray extends \Psalm\Type\Atomic
return $array_type;
}
public function isNonEmpty(): bool
{
foreach ($this->properties as $key => $property) {
if (!$property->possibly_undefined) {
return true;
}
}
return false;
}
public function __clone()
{
foreach ($this->properties as &$property) {