Skip to content

Commit 3487bd9

Browse files
committed
TASK: TernaryBranchScope introduce static factories and dont throw booleans around
> "it must be true though" https://www.youtube.com/watch?v=7EmboKQH8lM&t=4407s
1 parent 57ee20c commit 3487bd9

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

src/TypeSystem/Resolver/TernaryOperation/TernaryOperationTypeResolver.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,15 @@ public function resolveTypeOf(TernaryOperationNode $ternaryOperationNode): TypeI
5454
}
5555

5656
$trueExpressionTypeResolver = new ExpressionTypeResolver(
57-
scope: new TernaryBranchScope(
57+
scope: TernaryBranchScope::forTrueBranch(
5858
$ternaryOperationNode->condition,
59-
true,
6059
$this->scope
6160
)
6261
);
6362

6463
$falseExpressionTypeResolver = new ExpressionTypeResolver(
65-
scope: new TernaryBranchScope(
64+
scope: TernaryBranchScope::forFalseBranch(
6665
$ternaryOperationNode->condition,
67-
false,
6866
$this->scope
6967
)
7068
);

src/TypeSystem/Scope/ShallowScope/TernaryBranchScope.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,23 @@
3535

3636
final class TernaryBranchScope implements ScopeInterface
3737
{
38-
public function __construct(
38+
private function __construct(
3939
private readonly ExpressionNode $conditionNode,
40-
private readonly bool $isBranchLeft,
40+
private readonly bool $isBranchTrue,
4141
private readonly ScopeInterface $parentScope
4242
) {
4343
}
4444

45+
public static function forTrueBranch(ExpressionNode $conditionNode, ScopeInterface $parentScope): self
46+
{
47+
return new self(conditionNode: $conditionNode, isBranchTrue: true, parentScope: $parentScope);
48+
}
49+
50+
public static function forFalseBranch(ExpressionNode $conditionNode, ScopeInterface $parentScope): self
51+
{
52+
return new self(conditionNode: $conditionNode, isBranchTrue: false, parentScope: $parentScope);
53+
}
54+
4555
public function lookupTypeFor(string $name): ?TypeInterface
4656
{
4757
$type = $this->parentScope->lookupTypeFor($name);
@@ -51,7 +61,7 @@ public function lookupTypeFor(string $name): ?TypeInterface
5161
}
5262

5363
if ($this->conditionNode->root instanceof IdentifierNode && $this->conditionNode->root->value === $name) {
54-
return $this->isBranchLeft ? $type->withoutNull() : NullType::get();
64+
return $this->isBranchTrue ? $type->withoutNull() : NullType::get();
5565
}
5666

5767
if (($binaryOperationNode = $this->conditionNode->root) instanceof BinaryOperationNode) {
@@ -64,11 +74,11 @@ public function lookupTypeFor(string $name): ?TypeInterface
6474
}
6575

6676
if ($binaryOperationNode->operator === BinaryOperator::EQUAL) {
67-
return $this->isBranchLeft ? NullType::get() : $type->withoutNull();
77+
return $this->isBranchTrue ? NullType::get() : $type->withoutNull();
6878
}
6979

7080
if ($binaryOperationNode->operator === BinaryOperator::NOT_EQUAL) {
71-
return $this->isBranchLeft ? $type->withoutNull() : NullType::get();
81+
return $this->isBranchTrue ? $type->withoutNull() : NullType::get();
7282
}
7383
}
7484

0 commit comments

Comments
 (0)