@@ -209,6 +209,25 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
209209 return $ this ->defaultNarrowingHelper ->specifyDefaultTypes ($ expr , $ context );
210210 }
211211
212+ // An exact context (`=== true`, `!== false`, ...) asks about the taken
213+ // arm's value, not its truthiness - `0 !== false` holds although `0` is
214+ // falsey. The decomposition then holds for the arms compared to the
215+ // constant (`(cond && if !== false) || ...`) being true, the condition
216+ // still read by truthiness.
217+ $ armContext = null ;
218+ if ($ context !== TypeSpecifierContext ::createTruthy () && $ context !== TypeSpecifierContext ::createFalsey ()) {
219+ // `=== false` / `!== true` of an arm are false when the arm is
220+ // true: a disjunction arm would be split as if it were negated
221+ if (
222+ !$ context ->true ()
223+ && (self ::isDisjunction ($ expr ->if ?? $ expr ->cond ) || self ::isDisjunction ($ expr ->else ))
224+ ) {
225+ return $ this ->defaultNarrowingHelper ->specifyDefaultTypes ($ expr , $ context );
226+ }
227+ $ armContext = $ context ;
228+ $ context = TypeSpecifierContext ::createTruthy ();
229+ }
230+
212231 // cond ? if : else narrows like (cond && if) || (!cond && else),
213232 // composed from the walk's results through the boolean helpers -
214233 // the fabricated nodes are only printed into holder keys
@@ -240,8 +259,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
240259
241260 return new BooleanType ();
242261 };
243- $ elseTypes = static fn (MutatingScope $ scope , TypeSpecifierContext $ ctx ): SpecifiedTypes => $ elseResult ->getSpecifiedTypesForScope ($ scope , $ ctx );
244- $ elseType = static fn (bool $ nativeTypesPromoted ): Type => $ elseResult ->getTypeOnScope ($ elseProcessingScope , $ nativeTypesPromoted );
262+ [$ elseTypes , $ elseType , $ elseTruthyScope , $ elseFalseyScope ] = $ this ->createArmOperand ($ expr ->else , $ elseResult , $ elseProcessingScope , $ elseResult ->getScope (), $ armContext );
245263
246264 // the decomposition's branch scopes are the operand walks' own
247265 // memoized branch scopes (the evaluation points), not ask-derived;
@@ -251,7 +269,6 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
251269
252270 // right disjunct: !cond && else
253271 $ bNode = new BooleanAnd ($ notCondNode , $ expr ->else );
254- $ elseFalseyOnCondFalseyScope = static fn (): MutatingScope => $ elseResult ->getFalseyScope ();
255272 $ bTypes = fn (MutatingScope $ scope , TypeSpecifierContext $ ctx ): SpecifiedTypes => $ this ->booleanNarrowingHelper ->specifyConjunction (
256273 $ nodeScopeResolver ,
257274 $ scope ,
@@ -263,17 +280,23 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
263280 $ condTruthyScope ,
264281 $ expr ->else ,
265282 $ elseTypes ,
266- $ elseFalseyOnCondFalseyScope ,
283+ $ elseFalseyScope ,
267284 );
268285 $ bType = $ andVerdict ($ notCondType , $ elseType );
269- $ bTruthyScope = static fn (): MutatingScope => $ elseResult ->getTruthyScope ();
270286
271- if ($ ifResult !== null && $ expr ->if !== null ) {
287+ // the short ternary's truthy value is the condition itself - in an
288+ // exact context it is compared like an arm: cond && (cond === true)
289+ $ shortTernaryArm = $ armContext !== null && ($ ifResult === null || $ expr ->if === null );
290+ if (($ ifResult !== null && $ expr ->if !== null ) || $ shortTernaryArm ) {
272291 // left disjunct: cond && if
273- $ aNode = new BooleanAnd ($ expr ->cond , $ expr ->if );
274- $ ifTypes = static fn (MutatingScope $ scope , TypeSpecifierContext $ ctx ): SpecifiedTypes => $ ifResult ->getSpecifiedTypesForScope ($ scope , $ ctx );
275- $ ifType = static fn (bool $ nativeTypesPromoted ): Type => $ ifResult ->getTypeOnScope ($ ifProcessingScope , $ nativeTypesPromoted );
276- $ ifFalseyOnCondTruthyScope = static fn (): MutatingScope => $ ifResult ->getFalseyScope ();
292+ if ($ ifResult !== null && $ expr ->if !== null ) {
293+ $ ifExpr = $ expr ->if ;
294+ [$ ifTypes , $ ifType , $ ifTruthyScope , $ ifFalseyScope ] = $ this ->createArmOperand ($ expr ->if , $ ifResult , $ ifProcessingScope , $ ifResult ->getScope (), $ armContext );
295+ } else {
296+ $ ifExpr = $ expr ->cond ;
297+ [$ ifTypes , $ ifType , $ ifTruthyScope , $ ifFalseyScope ] = $ this ->createArmOperand ($ expr ->cond , $ ternaryCondResult , $ ifProcessingScope , $ ifProcessingScope , $ armContext );
298+ }
299+ $ aNode = new BooleanAnd ($ expr ->cond , $ ifExpr );
277300 $ aTypes = fn (MutatingScope $ scope , TypeSpecifierContext $ ctx ): SpecifiedTypes => $ this ->booleanNarrowingHelper ->specifyConjunction (
278301 $ nodeScopeResolver ,
279302 $ scope ,
@@ -283,12 +306,11 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
283306 $ condTypes ,
284307 $ condTruthyScope ,
285308 $ condFalseyScope ,
286- $ expr -> if ,
309+ $ ifExpr ,
287310 $ ifTypes ,
288- $ ifFalseyOnCondTruthyScope ,
311+ $ ifFalseyScope ,
289312 );
290313 $ aType = $ andVerdict ($ condType , $ ifType );
291- $ aTruthyScope = static fn (): MutatingScope => $ ifResult ->getTruthyScope ();
292314 // the merged falsey of (cond && if) has no single walk scope -
293315 // derived from the evaluation point on first demand, reused across asks
294316 $ aFalseyScopeThunk = static function () use ($ scope , $ aTypes , &$ aFalseyScope ): MutatingScope {
@@ -303,12 +325,12 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
303325 $ aNode ,
304326 $ aTypes ,
305327 $ aType ,
306- $ aTruthyScope ,
328+ $ ifTruthyScope ,
307329 $ aFalseyScopeThunk ,
308330 $ bNode ,
309331 $ bTypes ,
310332 $ bType ,
311- $ bTruthyScope ,
333+ $ elseTruthyScope ,
312334 )->setRootExpr ($ expr );
313335 }
314336
@@ -326,10 +348,77 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
326348 $ bNode ,
327349 $ bTypes ,
328350 $ bType ,
329- $ bTruthyScope ,
351+ $ elseTruthyScope ,
330352 )->setRootExpr ($ expr );
331353 },
332354 );
333355 }
334356
357+ private static function isDisjunction (Expr $ expr ): bool
358+ {
359+ return $ expr instanceof Expr \BinaryOp \BooleanOr || $ expr instanceof Expr \BinaryOp \LogicalOr;
360+ }
361+
362+ /**
363+ * An arm as an operand of the (cond && if) || (!cond && else) decomposition:
364+ * its narrowing, verdict and branch scopes. With an exact context the operand
365+ * is the arm compared to the bool constant, narrowed like the comparison
366+ * node would be - the constant pinned onto the arm plus the arm's own
367+ * narrowing in the bool context - with branch scopes derived from the arm's
368+ * evaluation point.
369+ *
370+ * @return array{callable(MutatingScope, TypeSpecifierContext): SpecifiedTypes, callable(bool): Type, callable(): MutatingScope, callable(): MutatingScope}
371+ */
372+ private function createArmOperand (Expr $ armExpr , ExpressionResult $ armResult , MutatingScope $ processingScope , MutatingScope $ evaluatedScope , ?TypeSpecifierContext $ armContext ): array
373+ {
374+ if ($ armContext === null ) {
375+ return [
376+ static fn (MutatingScope $ scope , TypeSpecifierContext $ ctx ): SpecifiedTypes => $ armResult ->getSpecifiedTypesForScope ($ scope , $ ctx ),
377+ static fn (bool $ nativeTypesPromoted ): Type => $ armResult ->getTypeOnScope ($ processingScope , $ nativeTypesPromoted ),
378+ static fn (): MutatingScope => $ armResult ->getTruthyScope (),
379+ static fn (): MutatingScope => $ armResult ->getFalseyScope (),
380+ ];
381+ }
382+
383+ // `!== true` / `!== false` are the mixed contexts, `=== true` / `=== false` the pure ones
384+ $ isIdentical = !($ armContext ->truthy () && $ armContext ->falsey ());
385+ $ value = $ isIdentical ? $ armContext ->true () : !$ armContext ->true ();
386+
387+ $ types = function (MutatingScope $ scope , TypeSpecifierContext $ ctx ) use ($ armExpr , $ armResult , $ isIdentical , $ value ): SpecifiedTypes {
388+ $ identicalContext = $ isIdentical ? $ ctx : $ ctx ->negate ();
389+ $ types = $ this ->defaultNarrowingHelper ->createSubjectTypes ($ scope , $ armExpr , $ armResult , new ConstantBooleanType ($ value ), $ identicalContext );
390+
391+ // a nullsafe chain that did not produce the constant may have
392+ // short-circuited instead
393+ if (!$ identicalContext ->true () && ($ armExpr instanceof Expr \NullsafeMethodCall || $ armExpr instanceof Expr \NullsafePropertyFetch)) {
394+ return $ types ;
395+ }
396+
397+ $ boolContext = $ value ? TypeSpecifierContext ::createTrue () : TypeSpecifierContext ::createFalse ();
398+
399+ return $ types ->unionWith ($ armResult ->getSpecifiedTypesForScope ($ scope , $ identicalContext ->true () ? $ boolContext : $ boolContext ->negate ()));
400+ };
401+
402+ return [
403+ $ types ,
404+ static function (bool $ nativeTypesPromoted ) use ($ armResult , $ processingScope , $ isIdentical , $ value ): Type {
405+ $ armType = $ armResult ->getTypeOnScope ($ processingScope , $ nativeTypesPromoted );
406+ $ matches = $ value ? $ armType ->isTrue () : $ armType ->isFalse ();
407+ if (!$ isIdentical ) {
408+ $ matches = $ matches ->negate ();
409+ }
410+ if ($ matches ->yes ()) {
411+ return new ConstantBooleanType (true );
412+ }
413+ if ($ matches ->no ()) {
414+ return new ConstantBooleanType (false );
415+ }
416+
417+ return new BooleanType ();
418+ },
419+ static fn (): MutatingScope => $ evaluatedScope ->applySpecifiedTypes ($ types ($ evaluatedScope , TypeSpecifierContext ::createTruthy ())),
420+ static fn (): MutatingScope => $ evaluatedScope ->applySpecifiedTypes ($ types ($ evaluatedScope , TypeSpecifierContext ::createFalsey ())),
421+ ];
422+ }
423+
335424}
0 commit comments