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

Fix a couple of false-positive redundant conditions

This commit is contained in:
Brown 2020-09-19 18:12:14 -04:00 committed by Daniil Gentili
parent 1b10f11217
commit ec64ae930a
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
4 changed files with 35 additions and 0 deletions

View File

@ -2151,6 +2151,14 @@ class AssertionFinder
$literal_assertions[] = '=' . $array_literal_type->getId();
}
if ($atomic_type->type_params[1]->isFalsable()) {
$literal_assertions[] = 'false';
}
if ($atomic_type->type_params[1]->isNullable()) {
$literal_assertions[] = 'null';
}
if ($negate) {
$if_types = \Psalm\Type\Algebra::negateTypes([
$first_var_name => [$literal_assertions]

View File

@ -945,6 +945,8 @@ class TypeCombination
}
}
$has_defined_keys = false;
foreach ($type->properties as $candidate_property_name => $candidate_property_type) {
$value_type = isset($combination->objectlike_entries[$candidate_property_name])
? $combination->objectlike_entries[$candidate_property_name]
@ -973,6 +975,14 @@ class TypeCombination
if (!$type->previous_value_type) {
unset($possibly_undefined_entries[$candidate_property_name]);
}
if (!$candidate_property_type->possibly_undefined) {
$has_defined_keys = true;
}
}
if (!$has_defined_keys) {
$combination->array_always_filled = false;
}
if ($combination->array_counts !== null) {

View File

@ -629,6 +629,13 @@ class TypeCombinationTest extends TestCase
'positive-int',
],
],
'combinNonEmptyArrayAndKeyedArray' => [
'array<int, int>',
[
'non-empty-array<int, int>',
'array{0?:int}',
]
],
];
}

View File

@ -766,6 +766,16 @@ class ValueTest extends \Psalm\Tests\TestCase
return $ret;
}'
],
'inArrayPreserveNull' => [
'<?php
function x(?string $foo): void {
if (!in_array($foo, ["foo", "bar", null], true)) {
throw new Exception();
}
if ($foo) {}
}',
],
];
}