Skip to content

Commit

Permalink
Final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jul 25, 2022
1 parent bcc9d10 commit 3520e85
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 51 deletions.
Expand Up @@ -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();
}
}
}
Expand Down
Expand Up @@ -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(
Expand Down
Expand Up @@ -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".
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Psalm/Type/Reconciler.php
Expand Up @@ -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;
Expand Down

0 comments on commit 3520e85

Please sign in to comment.