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..a59b37cc1a6 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-15760.php @@ -0,0 +1,87 @@ + '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); + } +} + +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); + } +} 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));