From 3520e85b6d564c673b53a6c43c5bc6fa8a113db7 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 25 Jul 2022 17:26:47 +0200 Subject: [PATCH] Final fixes --- .../Expression/AssignmentAnalyzer.php | 5 +- .../ExistingAtomicMethodCallAnalyzer.php | 6 ++- .../Statements/Expression/CallAnalyzer.php | 49 +------------------ src/Psalm/Type/Reconciler.php | 5 +- 4 files changed, 14 insertions(+), 51 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php index b0f9d457dd3..d1d4fda55a4 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php @@ -1523,7 +1523,10 @@ private static function analyzeDestructuringAssignment( if (($context->error_suppressing && ($offset || $can_be_empty)) || $has_null ) { - $context->vars_in_scope[$list_var_id] = $context->vars_in_scope[$list_var_id]->getBuilder()->addType(new TNull)->freeze(); + $context->vars_in_scope[$list_var_id] = $context->vars_in_scope[$list_var_id] + ->getBuilder() + ->addType(new TNull) + ->freeze(); } } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/ExistingAtomicMethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/ExistingAtomicMethodCallAnalyzer.php index 279b3a8d963..72c0c7cbda5 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/ExistingAtomicMethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/ExistingAtomicMethodCallAnalyzer.php @@ -300,7 +300,11 @@ public static function analyze( if ($method_storage) { if ($method_storage->if_this_is_type) { $class_type = new Union([$lhs_type_part]); - $if_this_is_type = TemplateInferredTypeReplacer::replace(clone $method_storage->if_this_is_type, $template_result, $codebase); + $if_this_is_type = TemplateInferredTypeReplacer::replace( + clone $method_storage->if_this_is_type, + $template_result, + $codebase + ); if (!UnionTypeComparator::isContainedBy($codebase, $class_type, $if_this_is_type)) { IssueBuffer::maybeAdd( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php index 2aaeb234a56..7f9eb8ffbbb 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php @@ -1136,49 +1136,6 @@ public static function checkTemplateResult( } } - /** - * This method should detect if the new type narrows down the old type. - */ - private static function isNewTypeNarrowingDownOldType(Union $new_type, Union $old_type): bool - { - if (count($new_type->getAtomicTypes()) === 1) { - return true; - } - - // non-mixed is always better than mixed - if ($old_type->isMixed() && !$new_type->hasMixed()) { - return true; - } - - // non-nullable is always better than nullable - if ($old_type->isNullable() && !$new_type->isNullable()) { - return true; - } - - // Do not hassle around with non-single old types if they are not nullable - if (count($old_type->getAtomicTypes()) !== 1) { - return false; - } - - // Do not hassle around with single literals as they supposed to be more accurate than any new type assertion - if ($old_type->isSingleFloatLiteral() - || $old_type->isSingleIntLiteral() - || $old_type->isSingleStringLiteral() - ) { - return false; - } - - // Literals should always replace non-literals - if (($old_type->isString() && $new_type->allStringLiterals()) - || ($old_type->isInt() && $new_type->allIntLiterals()) - || ($old_type->isFloat() && $new_type->allFloatLiterals()) - ) { - return true; - } - - return false; - } - /** * This method should kick all literals within `new_type` which are not part of the already known `old_type`. * So lets say we already know that the old type is one of "a", "b" or "c". @@ -1187,10 +1144,6 @@ private static function isNewTypeNarrowingDownOldType(Union $new_type, Union $ol */ private static function createUnionIntersectionFromOldType(Union $new_type, Union $old_type): ?Union { - if (!self::isNewTypeNarrowingDownOldType($new_type, $old_type)) { - return null; - } - if (!$new_type->allLiterals() || !$old_type->allLiterals()) { return $new_type; } @@ -1207,7 +1160,7 @@ private static function createUnionIntersectionFromOldType(Union $new_type, Unio } } - if ($equal_atomic_types === []) { + if ($equal_atomic_types === [] || count($equal_atomic_types) === count($old_type->getAtomicTypes())) { return null; } diff --git a/src/Psalm/Type/Reconciler.php b/src/Psalm/Type/Reconciler.php index 5481d080f06..7fd5fb055d2 100644 --- a/src/Psalm/Type/Reconciler.php +++ b/src/Psalm/Type/Reconciler.php @@ -731,7 +731,10 @@ private static function getValueForKey( if (($has_isset || $has_inverted_isset) && isset($new_assertions[$new_base_key])) { if ($has_inverted_isset && $new_base_key === $key) { - $new_base_type_candidate = $new_base_type_candidate->getBuilder()->addType(new TNull)->freeze(); + $new_base_type_candidate = $new_base_type_candidate + ->getBuilder() + ->addType(new TNull) + ->freeze(); } $new_base_type_candidate->possibly_undefined = true;