1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 09:19:40 +01:00

bugfix: overriding types based on assertions have to pass more checks

Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com>
This commit is contained in:
Maximilian Bösing 2022-06-12 00:32:36 +02:00
parent 7c85e0c0d9
commit 2d9133b35a
No known key found for this signature in database
GPG Key ID: 9A8988C93CEC81A3

View File

@ -763,7 +763,9 @@ class CallAnalyzer
$codebase $codebase
); );
if ($union->isSingle() || $union->allLiterals()) { $arg_var_type = $context->vars_in_scope[$assertion_var_id];
if (self::isNewTypeNarrowingDownOldType($arg_var_type, $union)) {
foreach ($union->getAtomicTypes() as $atomic_type) { foreach ($union->getAtomicTypes() as $atomic_type) {
if ($assertion_type instanceof TTemplateParam if ($assertion_type instanceof TTemplateParam
&& $assertion_type->as->getId() === $atomic_type->getId() && $assertion_type->as->getId() === $atomic_type->getId()
@ -1117,4 +1119,24 @@ class CallAnalyzer
} }
} }
} }
/**
* This method should detect if the new type narrows down the old type.
*/
private static function isNewTypeNarrowingDownOldType(Union $old_type, Union $new_type): bool
{
if ($new_type->isSingle()) {
return true;
}
if ($old_type->hasMixed() && !$new_type->hasMixed()) {
return true;
}
if ($old_type->isSingleAndMaybeNullable() && !$new_type->isNullable()) {
return true;
}
return false;
}
} }