3535
3636final 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