From 0fffb55a83f200d42916e030d7139bf83cc0b36f Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Tue, 4 Jan 2022 16:08:06 +0000 Subject: [PATCH] Simplify some logic around negations to prevent unecessary looping --- .../Type/NegatedAssertionReconciler.php | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/src/Psalm/Internal/Type/NegatedAssertionReconciler.php b/src/Psalm/Internal/Type/NegatedAssertionReconciler.php index eb763bba4..df90d2a95 100644 --- a/src/Psalm/Internal/Type/NegatedAssertionReconciler.php +++ b/src/Psalm/Internal/Type/NegatedAssertionReconciler.php @@ -176,39 +176,37 @@ class NegatedAssertionReconciler extends Reconciler // if there wasn't a direct hit, go deeper, eliminating subtypes if (!$existing_var_type->removeType($assertion)) { - foreach ($existing_var_type->getAtomicTypes() as $part_name => $existing_var_type_part) { - if (!$existing_var_type_part->isObjectType() || strpos($assertion, '-')) { - continue; - } - + if (!strpos($assertion, '-')) { $assertion_type = Type::parseString($assertion, null, $template_type_map); - if (!$assertion_type->isSingle()) { - continue; - } + if ($assertion_type->isSingle()) { + $new_type_part = $assertion_type->getSingleAtomic(); - $new_type_part = $assertion_type->getSingleAtomic(); + if ($new_type_part instanceof TNamedObject) { + foreach ($existing_var_type->getAtomicTypes() as $part_name => $existing_var_type_part) { + if (!$existing_var_type_part->isObjectType()) { + continue; + } - if (!$new_type_part instanceof TNamedObject) { - continue; - } - - if (AtomicTypeComparator::isContainedBy( - $codebase, - $existing_var_type_part, - $new_type_part, - false, - false - )) { - $existing_var_type->removeType($part_name); - } elseif (AtomicTypeComparator::isContainedBy( - $codebase, - $new_type_part, - $existing_var_type_part, - false, - false - )) { - $existing_var_type->different = true; + if (AtomicTypeComparator::isContainedBy( + $codebase, + $existing_var_type_part, + $new_type_part, + false, + false + )) { + $existing_var_type->removeType($part_name); + } elseif (AtomicTypeComparator::isContainedBy( + $codebase, + $new_type_part, + $existing_var_type_part, + false, + false + )) { + $existing_var_type->different = true; + } + } + } } } }