From 3592f19544aa882c70b57ec2072f1930428691a4 Mon Sep 17 00:00:00 2001 From: phpstan-bot <79867460+phpstan-bot@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:46:16 +0000 Subject: [PATCH 1/2] Require ternary arm remainders to be disjoint from the other arm in both `isSuperTypeOf()` directions before recording conditional expressions - AssignHandler: when assigning a ternary, an arm's remainder (the part the other arm cannot produce) is now used as a conditional-expression guard only if both `otherArm->isSuperTypeOf(remainder)` and `remainder->isSuperTypeOf(otherArm)` are `no`. Constant arrays that are not sealed (no bleeding edge) compare asymmetrically: array{'px', 'rem'} is a supertype of array{'px', 'rem', '%'}. The guard "if $units is array{'px', 'rem'} then $blur is true" then supertype-matched values of the other arm (e.g. array{0: 'px', 1: 'rem', 2?: '%'} after in_array()), narrowing $blur to true. - turbo-ext/src/AssignHandler.cpp: same change in the native mirror. - Probed analogous constructs: if/else branch merges (ScopeOps guards already reject guards that are not disjoint from the other branch) and match arm assignments were already correct; they are covered in the new test. --- src/Analyser/ExprHandler/AssignHandler.php | 9 ++- tests/PHPStan/Analyser/Bug15760Test.php | 32 ++++++++++ tests/PHPStan/Analyser/data/bug-15760.php | 71 ++++++++++++++++++++++ turbo-ext/src/AssignHandler.cpp | 13 +++- 4 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 tests/PHPStan/Analyser/Bug15760Test.php create mode 100644 tests/PHPStan/Analyser/data/bug-15760.php diff --git a/src/Analyser/ExprHandler/AssignHandler.php b/src/Analyser/ExprHandler/AssignHandler.php index 426d4aa65f1..827915407b1 100644 --- a/src/Analyser/ExprHandler/AssignHandler.php +++ b/src/Analyser/ExprHandler/AssignHandler.php @@ -1279,15 +1279,18 @@ public function applyWrite( // The variable can prove an arm was taken even when the arm types overlap: // the part of an arm's type not producible by the other arm implies that // arm's condition outcome. With fully disjoint arms both remainders are - // the full arm types. + // the full arm types. Disjointness is checked in both directions because + // isSuperTypeOf() is not symmetric for constant arrays that are not + // sealed - array{'a'} is a supertype of array{'a', 'b'} - and a remainder + // matching values of the other arm would prove the wrong outcome. $truthyRemainder = TypeCombinator::remove($truthyType, $falseyType); - if ($falseyType->isSuperTypeOf($truthyRemainder)->no()) { + if ($falseyType->isSuperTypeOf($truthyRemainder)->no() && $truthyRemainder->isSuperTypeOf($falseyType)->no()) { $conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult); $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult); $conditionalExpressions = $this->processTernaryArmValueImpliedTypesAfterAssign($nodeScopeResolver, $truthyScope, $storage, $var->name, $conditionalExpressions, $if, $truthyRemainder, $falseyType, $impurePoints, $assignedExpr, $storedAssignedExprResult); } $falseyRemainder = TypeCombinator::remove($falseyType, $truthyType); - if ($truthyType->isSuperTypeOf($falseyRemainder)->no()) { + if ($truthyType->isSuperTypeOf($falseyRemainder)->no() && $falseyRemainder->isSuperTypeOf($truthyType)->no()) { $conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult); $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($nodeScopeResolver, $condScope, $storage, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyRemainder, $impurePoints, $assignedExpr, $storedAssignedExprResult); $conditionalExpressions = $this->processTernaryArmValueImpliedTypesAfterAssign($nodeScopeResolver, $falsyScope, $storage, $var->name, $conditionalExpressions, $assignedExpr->else, $falseyRemainder, $truthyType, $impurePoints, $assignedExpr, $storedAssignedExprResult); diff --git a/tests/PHPStan/Analyser/Bug15760Test.php b/tests/PHPStan/Analyser/Bug15760Test.php new file mode 100644 index 00000000000..0739fd3c4c6 --- /dev/null +++ b/tests/PHPStan/Analyser/Bug15760Test.php @@ -0,0 +1,32 @@ +assertFileAsserts($assertType, $file, ...$args); + } + +} diff --git a/tests/PHPStan/Analyser/data/bug-15760.php b/tests/PHPStan/Analyser/data/bug-15760.php new file mode 100644 index 00000000000..54e8be3d447 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-15760.php @@ -0,0 +1,71 @@ + 'px'] : ['a' => 'px', 'b' => 'rem']; + if (!in_array($unit, $units, true)) { + return; + } + assertType('bool', $blur); +} + +function ifElse(string $unit, bool $blur): void +{ + if ($blur) { + $units = ['px', 'rem']; + } else { + $units = ['px', 'rem', '%']; + } + if (!in_array($unit, $units, true)) { + return; + } + assertType('bool', $blur); +} + +function matchArms(string $unit, int $i): void +{ + $units = match ($i) { + 1 => ['px', 'rem'], + default => ['px', 'rem', '%'], + }; + if (!in_array($unit, $units, true)) { + return; + } + assertType('int', $i); +} + +function disjointArmsStillNarrow(bool $blur): void +{ + $units = $blur ? ['px', 'rem'] : ['em']; + if ($units === ['em']) { + assertType('false', $blur); + } else { + assertType('true', $blur); + } +} diff --git a/turbo-ext/src/AssignHandler.cpp b/turbo-ext/src/AssignHandler.cpp index d6ff3f394d1..1f6ef4f5686 100644 --- a/turbo-ext/src/AssignHandler.cpp +++ b/turbo-ext/src/AssignHandler.cpp @@ -2265,10 +2265,17 @@ class AssignHandler // The variable can prove an arm was taken even when the arm types overlap: // the part of an arm's type not producible by the other arm implies that // arm's condition outcome. With fully disjoint arms both remainders are - // the full arm types. + // the full arm types. Disjointness is checked in both directions because + // isSuperTypeOf() is not symmetric for constant arrays that are not + // sealed - array{'a'} is a supertype of array{'a', 'b'} - and a remainder + // matching values of the other arm would prove the wrong outcome. AH_VALB(truthyRemainder, pt_type_combinator_remove(truthyType.raw(), falseyType.raw())); zend_long truthyDisjoint = ahTypeOpTri(falseyType.raw(), PT_OP_IS_SUPER_TYPE_OF, 1, truthyRemainder.raw()); AH_OKB(truthyDisjoint >= 0); + if (truthyDisjoint == PT_TRI_NO) { + truthyDisjoint = ahTypeOpTri(truthyRemainder.raw(), PT_OP_IS_SUPER_TYPE_OF, 1, falseyType.raw()); + AH_OKB(truthyDisjoint >= 0); + } if (truthyDisjoint == PT_TRI_NO) { AH_OKB(processSureTypesForConditionalExpressionsAfterAssign(nsr, condScope.raw(), storage, varName, conditionalExpressions, truthySpecifiedTypes.raw(), truthyRemainder.raw(), impurePoints, assignedExpr, stored)); AH_OKB(processSureNotTypesForConditionalExpressionsAfterAssign(nsr, condScope.raw(), storage, varName, conditionalExpressions, truthySpecifiedTypes.raw(), truthyRemainder.raw(), impurePoints, assignedExpr, stored)); @@ -2277,6 +2284,10 @@ class AssignHandler AH_VALB(falseyRemainder, pt_type_combinator_remove(falseyType.raw(), truthyType.raw())); zend_long falseyDisjoint = ahTypeOpTri(truthyType.raw(), PT_OP_IS_SUPER_TYPE_OF, 1, falseyRemainder.raw()); AH_OKB(falseyDisjoint >= 0); + if (falseyDisjoint == PT_TRI_NO) { + falseyDisjoint = ahTypeOpTri(falseyRemainder.raw(), PT_OP_IS_SUPER_TYPE_OF, 1, truthyType.raw()); + AH_OKB(falseyDisjoint >= 0); + } if (falseyDisjoint == PT_TRI_NO) { AH_OKB(processSureTypesForConditionalExpressionsAfterAssign(nsr, condScope.raw(), storage, varName, conditionalExpressions, falseySpecifiedTypes.raw(), falseyRemainder.raw(), impurePoints, assignedExpr, stored)); AH_OKB(processSureNotTypesForConditionalExpressionsAfterAssign(nsr, condScope.raw(), storage, varName, conditionalExpressions, falseySpecifiedTypes.raw(), falseyRemainder.raw(), impurePoints, assignedExpr, stored)); From 1082277feb390dbe7d8a49bf5818259be0c75058 Mon Sep 17 00:00:00 2001 From: phpstan-bot Date: Tue, 17 Nov 2026 10:45:48 +0000 Subject: [PATCH 2/2] Test ternary arm remainders that are only maybe supertypes of the other arm These cases kill the escaped mutants that turn the reverse disjointness check from ->no() into !->yes(). Co-Authored-By: Claude Opus 5.5 --- tests/PHPStan/Analyser/data/bug-15760.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/PHPStan/Analyser/data/bug-15760.php b/tests/PHPStan/Analyser/data/bug-15760.php index 54e8be3d447..a59b37cc1a6 100644 --- a/tests/PHPStan/Analyser/data/bug-15760.php +++ b/tests/PHPStan/Analyser/data/bug-15760.php @@ -69,3 +69,19 @@ function disjointArmsStillNarrow(bool $blur): void assertType('true', $blur); } } + +function remainderMaybeSupertypeOfOtherArm(bool $blur, string $s): void +{ + $units = $blur ? ['px', 'rem'] : ['px', $s, '%']; + if ($units === ['px', 'rem', '%']) { + assertType('bool', $blur); + } +} + +function remainderMaybeSupertypeOfOtherArmReversed(bool $blur, string $s): void +{ + $units = $blur ? ['px', $s, '%'] : ['px', 'rem']; + if ($units === ['px', 'rem', '%']) { + assertType('bool', $blur); + } +}