diff --git a/src/Analyser/ExpressionTypeHolder.php b/src/Analyser/ExpressionTypeHolder.php index 3367a640c9..7477dbf3dc 100644 --- a/src/Analyser/ExpressionTypeHolder.php +++ b/src/Analyser/ExpressionTypeHolder.php @@ -10,7 +10,11 @@ final class ExpressionTypeHolder { - public function __construct(private Expr $expr, private Type $type, private TrinaryLogic $certainty) + public function __construct( + private readonly Expr $expr, + private readonly Type $type, + private readonly TrinaryLogic $certainty, + ) { } @@ -24,6 +28,15 @@ public static function createMaybe(Expr $expr, Type $type): self return new self($expr, $type, TrinaryLogic::createMaybe()); } + public function equalTypes(self $other): bool + { + if ($this === $other) { + return true; + } + + return $this->type === $other->type || $this->type->equals($other->type); + } + public function equals(self $other): bool { if ($this === $other) { diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index e299d1b5a3..c6ee8f13a5 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -4014,7 +4014,7 @@ private function createConditionalExpressions( continue; } - if (!$mergedExpressionTypes[$exprString]->getType()->equals($holder->getType())) { + if (!$mergedExpressionTypes[$exprString]->equalTypes($holder)) { continue; } @@ -4023,13 +4023,13 @@ private function createConditionalExpressions( $typeGuards = []; foreach ($newVariableTypes as $exprString => $holder) { - if (!$holder->getCertainty()->yes()) { + if (!array_key_exists($exprString, $mergedExpressionTypes)) { continue; } - if (!array_key_exists($exprString, $mergedExpressionTypes)) { + if (!$holder->getCertainty()->yes()) { continue; } - if ($mergedExpressionTypes[$exprString]->getType()->equals($holder->getType())) { + if ($mergedExpressionTypes[$exprString]->equalTypes($holder)) { continue; } @@ -4194,7 +4194,7 @@ private function processFinallyScopeVariableTypeHolders( foreach ($finallyVariableTypeHolders as $exprString => $variableTypeHolder) { if ( isset($originalVariableTypeHolders[$exprString]) - && !$originalVariableTypeHolders[$exprString]->getType()->equals($variableTypeHolder->getType()) + && !$originalVariableTypeHolders[$exprString]->equalTypes($variableTypeHolder) ) { $ourVariableTypeHolders[$exprString] = $variableTypeHolder; continue; @@ -4766,7 +4766,7 @@ private function compareVariableTypeHolders(array $variableTypeHolders, array $o return false; } - if (!$variableTypeHolder->getType()->equals($otherVariableTypeHolders[$variableExprString]->getType())) { + if (!$variableTypeHolder->equalTypes($otherVariableTypeHolders[$variableExprString])) { return false; } }