Skip to content

Commit

Permalink
Fix a couple of false-positive redundant conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Sep 19, 2020
1 parent 941643c commit 0ae436d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2151,6 +2151,14 @@ public static function processFunctionCall(
$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]
Expand Down
10 changes: 10 additions & 0 deletions src/Psalm/Internal/Type/TypeCombination.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,8 @@ private static function scrapeTypeProperties(
}
}

$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]
Expand Down Expand Up @@ -973,6 +975,14 @@ private static function scrapeTypeProperties(
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) {
Expand Down
7 changes: 7 additions & 0 deletions tests/TypeCombinationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,13 @@ public function providerTestValidTypeCombination(): array
'positive-int',
],
],
'combinNonEmptyArrayAndKeyedArray' => [
'array<int, int>',
[
'non-empty-array<int, int>',
'array{0?:int}',
]
],
];
}

Expand Down
10 changes: 10 additions & 0 deletions tests/TypeReconciliation/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,16 @@ function f(): int {
return $ret;
}'
],
'inArrayPreserveNull' => [
'<?php
function x(?string $foo): void {
if (!in_array($foo, ["foo", "bar", null], true)) {
throw new Exception();
}
if ($foo) {}
}',
],
];
}

Expand Down

0 comments on commit 0ae436d

Please sign in to comment.