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

Allow object-like with all optional vars to be compared to empty array

This commit is contained in:
Matthew Brown 2018-03-21 10:46:21 -04:00
parent 452fd2e7c9
commit bc0c4322d5
4 changed files with 11 additions and 4 deletions

View File

@ -347,14 +347,18 @@ class TypeChecker
&& ($container_type_part instanceof TArray || $container_type_part instanceof ObjectLike)
) {
if ($container_type_part instanceof ObjectLike) {
$generic_container_type_part = $container_type_part->getGenericArrayType();
if (!$input_type_part instanceof ObjectLike
&& !$input_type_part->type_params[0]->isMixed()
&& !($input_type_part->type_params[1]->isEmpty()
&& $generic_container_type_part->type_params[1]->possibly_undefined)
) {
$all_types_contain = false;
$type_coerced = true;
}
$container_type_part = $container_type_part->getGenericArrayType();
$container_type_part = $generic_container_type_part;
}
if ($input_type_part instanceof ObjectLike) {

View File

@ -659,7 +659,7 @@ abstract class Type
$combined_type->failed_reconciliation = true;
}
if ($type_1->possibly_undefined || $type_2->possibly_undefined) {
if ($type_1->possibly_undefined && $type_2->possibly_undefined) {
$combined_type->possibly_undefined = true;
}

View File

@ -185,8 +185,6 @@ class ObjectLike extends \Psalm\Type\Atomic
throw new \UnexpectedValueException('$value_type should not be null here');
}
$value_type->possibly_undefined = false;
return new TArray([Type::combineTypes($key_types), $value_type]);
}

View File

@ -731,6 +731,11 @@ class AnnotationTest extends TestCase
'annotations' => [],
'error_levels' => ['LessSpecificReturnStatement', 'MoreSpecificReturnType'],
],
'allowOptionalParamsToBeEmptyArray' => [
'<?php
/** @param array{b?: int, c?: string} $a */
function foo(array $a = []) : void {}',
],
];
}