Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use root expression when checking impossible types #1254

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}

}