Skip to content

Commit

Permalink
Use root expression when checking impossible types
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed Apr 27, 2022
1 parent 3bf2958 commit 2aba5cc
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 92 deletions.
27 changes: 24 additions & 3 deletions src/Analyser/SpecifiedTypes.php
Expand Up @@ -20,6 +20,7 @@ public function __construct(
private array $sureNotTypes = [],
private bool $overwrite = false,
private array $newConditionalExpressionHolders = [],
private ?Expr $rootExpr = null,
)
{
}
Expand Down Expand Up @@ -55,11 +56,17 @@ public function getNewConditionalExpressionHolders(): array
return $this->newConditionalExpressionHolders;
}

public function getRootExpr(): ?Expr
{
return $this->rootExpr;
}

/** @api */
public function intersectWith(SpecifiedTypes $other): self
{
$sureTypeUnion = [];
$sureNotTypeUnion = [];
$rootExpr = $this->mergeRootExpr($this->rootExpr, $other->rootExpr);

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

return new self($sureTypeUnion, $sureNotTypeUnion);
return new self($sureTypeUnion, $sureNotTypeUnion, false, [], $rootExpr);
}

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

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

return new self($sureTypeUnion, $sureNotTypeUnion);
return new self($sureTypeUnion, $sureNotTypeUnion, false, [], $rootExpr);
}

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

return new self($sureTypes, [], $this->overwrite, $this->newConditionalExpressionHolders);
return new self($sureTypes, [], $this->overwrite, $this->newConditionalExpressionHolders, $this->rootExpr);
}

private function mergeRootExpr(?Expr $rootExprA, ?Expr $rootExprB): ?Expr
{
if ($rootExprA === $rootExprB) {
return $rootExprA;
}

if ($rootExprA === null || $rootExprB === null) {
return $rootExprA ?? $rootExprB;
}

return null;
}

}

0 comments on commit 2aba5cc

Please sign in to comment.