diff --git a/src/Psalm/Internal/Type/SimpleAssertionReconciler.php b/src/Psalm/Internal/Type/SimpleAssertionReconciler.php index fa3e3de6f7f..382cd907233 100644 --- a/src/Psalm/Internal/Type/SimpleAssertionReconciler.php +++ b/src/Psalm/Internal/Type/SimpleAssertionReconciler.php @@ -2238,10 +2238,9 @@ private static function reconcileFalsyOrEmpty( //if any atomic in the union is either always truthy, we remove it. If not always falsy, we mark the check //as not redundant. $union = new Union([$existing_var_type_part]); - if (!$existing_var_type->possibly_undefined - && !$existing_var_type->possibly_undefined_from_try - && $union->isAlwaysTruthy() - ) { + $union->possibly_undefined = $existing_var_type->possibly_undefined; + $union->possibly_undefined_from_try = $existing_var_type->possibly_undefined_from_try; + if ($union->isAlwaysTruthy()) { $did_remove_type = true; $existing_var_type->removeType($existing_var_type_key); } elseif (!$union->isAlwaysFalsy()) { diff --git a/src/Psalm/Internal/Type/SimpleNegatedAssertionReconciler.php b/src/Psalm/Internal/Type/SimpleNegatedAssertionReconciler.php index 4a5d05a7bd1..0aec7c76fbc 100644 --- a/src/Psalm/Internal/Type/SimpleNegatedAssertionReconciler.php +++ b/src/Psalm/Internal/Type/SimpleNegatedAssertionReconciler.php @@ -591,13 +591,12 @@ private static function reconcileFalsyOrEmpty( //if any atomic in the union is either always falsy, we remove it. If not always truthy, we mark the check //as not redundant. $union = new Union([$existing_var_type_part]); + $union->possibly_undefined = $existing_var_type->possibly_undefined; + $union->possibly_undefined_from_try = $existing_var_type->possibly_undefined_from_try; if ($union->isAlwaysFalsy()) { $did_remove_type = true; $existing_var_type->removeType($existing_var_type_key); - } elseif ($existing_var_type->possibly_undefined - || $existing_var_type->possibly_undefined_from_try - || !$union->isAlwaysTruthy() - ) { + } elseif (!$union->isAlwaysTruthy()) { $did_remove_type = true; } } diff --git a/src/Psalm/Type/Union.php b/src/Psalm/Type/Union.php index cfb719a7cf7..1ec86d4b06f 100644 --- a/src/Psalm/Type/Union.php +++ b/src/Psalm/Type/Union.php @@ -1083,6 +1083,10 @@ public function isTrue(): bool public function isAlwaysTruthy(): bool { + if ($this->possibly_undefined || $this->possibly_undefined_from_try) { + return false; + } + foreach ($this->getAtomicTypes() as $atomic_type) { if ($atomic_type instanceof TTrue) { continue;