Skip to content

Commit 6bf335c

Browse files
authored
Use root expression when checking impossible types
1 parent c9ae89c commit 6bf335c

File tree

4 files changed

+200
-155
lines changed

4 files changed

+200
-155
lines changed

src/Analyser/SpecifiedTypes.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function __construct(
2020
private array $sureNotTypes = [],
2121
private bool $overwrite = false,
2222
private array $newConditionalExpressionHolders = [],
23+
private ?Expr $rootExpr = null,
2324
)
2425
{
2526
}
@@ -55,11 +56,17 @@ public function getNewConditionalExpressionHolders(): array
5556
return $this->newConditionalExpressionHolders;
5657
}
5758

59+
public function getRootExpr(): ?Expr
60+
{
61+
return $this->rootExpr;
62+
}
63+
5864
/** @api */
5965
public function intersectWith(SpecifiedTypes $other): self
6066
{
6167
$sureTypeUnion = [];
6268
$sureNotTypeUnion = [];
69+
$rootExpr = $this->mergeRootExpr($this->rootExpr, $other->rootExpr);
6370

6471
foreach ($this->sureTypes as $exprString => [$exprNode, $type]) {
6572
if (!isset($other->sureTypes[$exprString])) {
@@ -83,14 +90,15 @@ public function intersectWith(SpecifiedTypes $other): self
8390
];
8491
}
8592

86-
return new self($sureTypeUnion, $sureNotTypeUnion);
93+
return new self($sureTypeUnion, $sureNotTypeUnion, false, [], $rootExpr);
8794
}
8895

8996
/** @api */
9097
public function unionWith(SpecifiedTypes $other): self
9198
{
9299
$sureTypeUnion = $this->sureTypes + $other->sureTypes;
93100
$sureNotTypeUnion = $this->sureNotTypes + $other->sureNotTypes;
101+
$rootExpr = $this->mergeRootExpr($this->rootExpr, $other->rootExpr);
94102

95103
foreach ($this->sureTypes as $exprString => [$exprNode, $type]) {
96104
if (!isset($other->sureTypes[$exprString])) {
@@ -114,7 +122,7 @@ public function unionWith(SpecifiedTypes $other): self
114122
];
115123
}
116124

117-
return new self($sureTypeUnion, $sureNotTypeUnion);
125+
return new self($sureTypeUnion, $sureNotTypeUnion, false, [], $rootExpr);
118126
}
119127

120128
public function normalize(Scope $scope): self
@@ -130,7 +138,20 @@ public function normalize(Scope $scope): self
130138
$sureTypes[$exprString][1] = TypeCombinator::remove($sureTypes[$exprString][1], $sureNotType);
131139
}
132140

133-
return new self($sureTypes, [], $this->overwrite, $this->newConditionalExpressionHolders);
141+
return new self($sureTypes, [], $this->overwrite, $this->newConditionalExpressionHolders, $this->rootExpr);
142+
}
143+
144+
private function mergeRootExpr(?Expr $rootExprA, ?Expr $rootExprB): ?Expr
145+
{
146+
if ($rootExprA === $rootExprB) {
147+
return $rootExprA;
148+
}
149+
150+
if ($rootExprA === null || $rootExprB === null) {
151+
return $rootExprA ?? $rootExprB;
152+
}
153+
154+
return null;
134155
}
135156

136157
}

0 commit comments

Comments
 (0)