@@ -20,6 +20,7 @@ public function __construct(
20
20
private array $ sureNotTypes = [],
21
21
private bool $ overwrite = false ,
22
22
private array $ newConditionalExpressionHolders = [],
23
+ private ?Expr $ rootExpr = null ,
23
24
)
24
25
{
25
26
}
@@ -55,11 +56,17 @@ public function getNewConditionalExpressionHolders(): array
55
56
return $ this ->newConditionalExpressionHolders ;
56
57
}
57
58
59
+ public function getRootExpr (): ?Expr
60
+ {
61
+ return $ this ->rootExpr ;
62
+ }
63
+
58
64
/** @api */
59
65
public function intersectWith (SpecifiedTypes $ other ): self
60
66
{
61
67
$ sureTypeUnion = [];
62
68
$ sureNotTypeUnion = [];
69
+ $ rootExpr = $ this ->mergeRootExpr ($ this ->rootExpr , $ other ->rootExpr );
63
70
64
71
foreach ($ this ->sureTypes as $ exprString => [$ exprNode , $ type ]) {
65
72
if (!isset ($ other ->sureTypes [$ exprString ])) {
@@ -83,14 +90,15 @@ public function intersectWith(SpecifiedTypes $other): self
83
90
];
84
91
}
85
92
86
- return new self ($ sureTypeUnion , $ sureNotTypeUnion );
93
+ return new self ($ sureTypeUnion , $ sureNotTypeUnion, false , [], $ rootExpr );
87
94
}
88
95
89
96
/** @api */
90
97
public function unionWith (SpecifiedTypes $ other ): self
91
98
{
92
99
$ sureTypeUnion = $ this ->sureTypes + $ other ->sureTypes ;
93
100
$ sureNotTypeUnion = $ this ->sureNotTypes + $ other ->sureNotTypes ;
101
+ $ rootExpr = $ this ->mergeRootExpr ($ this ->rootExpr , $ other ->rootExpr );
94
102
95
103
foreach ($ this ->sureTypes as $ exprString => [$ exprNode , $ type ]) {
96
104
if (!isset ($ other ->sureTypes [$ exprString ])) {
@@ -114,7 +122,7 @@ public function unionWith(SpecifiedTypes $other): self
114
122
];
115
123
}
116
124
117
- return new self ($ sureTypeUnion , $ sureNotTypeUnion );
125
+ return new self ($ sureTypeUnion , $ sureNotTypeUnion, false , [], $ rootExpr );
118
126
}
119
127
120
128
public function normalize (Scope $ scope ): self
@@ -130,7 +138,20 @@ public function normalize(Scope $scope): self
130
138
$ sureTypes [$ exprString ][1 ] = TypeCombinator::remove ($ sureTypes [$ exprString ][1 ], $ sureNotType );
131
139
}
132
140
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 ;
134
155
}
135
156
136
157
}
0 commit comments