Skip to content

Require ternary arm remainders to be disjoint from the other arm in both isSuperTypeOf() directions before recording conditional expressions - #7094

Merged
staabm merged 5 commits into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-c5s4k58
Nov 18, 2026
Merged

staabm merged 5 commits into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-c5s4k58

Conversation

@phpstan-bot

Copy link
Copy Markdown
Collaborator

Summary

Without bleeding edge, $units = $blur ? ['px', 'rem'] : ['px', 'rem', '%'] followed by a passing in_array($unit, $units, true) narrowed $blur to true. That caused a false ternary.alwaysTrue on the next ternary on $blur. This regressed when ternary assignments started deriving conditional expressions from overlapping arm types (fba3878). The fix restores the two-directional disjointness check for the arm remainders.

Changes

  • src/Analyser/ExprHandler/AssignHandler.php: a ternary arm's remainder is recorded as a guard only if it is disjoint from the other arm in both isSuperTypeOf() directions. Previously only otherArm->isSuperTypeOf(remainder) was checked.
  • turbo-ext/src/AssignHandler.cpp: the same change in the native mirror. I built the extension and confirmed it fixes the test on its own.
  • Probed analogous constructs, all already correct:
    • if/else branch merges in both arm orders, including a variable defined in only one branch. ScopeOps::createConditionalExpressions() already checks guard->isSuperTypeOf(theirType).
    • match arm assignments.

Root cause

Without bleeding edge, constant arrays are not sealed, so isSuperTypeOf() is not symmetric for them. array{'px', 'rem', '%'} is not a supertype of array{'px', 'rem'}, but array{'px', 'rem'} is a supertype of array{'px', 'rem', '%'}, because extra keys are ignored.

For the true arm, remove(array{'px','rem'}, array{'px','rem','%'}) stays array{'px','rem'}. The one-directional check passed, so the guard "if $units is array{'px','rem'} then $blur is true" was recorded. The false arm's remainder was never, so it got no guard.

After in_array(), $units is array{0: 'px', 1: 'rem', 2?: '%'}. The supertype pass of ScopeOps::matchConditionalExpressions() matched it against the true-arm guard and narrowed $blur to true. Before fba3878, both arms had to be mutually disjoint in both directions. The fix reapplies that requirement to the remainders.

Test

  • tests/PHPStan/Analyser/Bug15760Test.php with tests/PHPStan/Analyser/data/bug-15760.php runs without bleeding edge, so constant arrays are not sealed. The regular nsrt suite runs with bleeding edge, where sealed arrays hide the bug. It covers:
    • the reported case, plus the reversed arm order and string-keyed arrays (all three failed before the fix);
    • if/else and match variants;
    • a check that disjoint arms still narrow.

Fixes phpstan/phpstan#15760

@staabm staabm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread tests/PHPStan/Analyser/Bug15760Test.php Outdated
Comment on lines +8 to +11
/**
* Runs without bleeding edge, so that constant arrays are not sealed.
*/
class Bug15760Test extends TypeInferenceTestCase

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in https://github.com/phpstan/phpstan-src/pull/7095/files#diff-5330012288c961f747397e9ce43ad8efc3b0cd3461a646a6f38178c5431dba9fR11 we added a similar test.

can we consolidate all tests which extend extends TypeInferenceTestCase and do not have any specific use-case but just don't enable bleeding edge?
something like NoBleedingEdgeTypeInferenceTestCase

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've consolidated the test classes into one, committed it as 0bf009d38 (not pushed), and make phpstan and make tests both pass.

What changed

  • Bug10980Test, Bug15736Test and Bug15760Test were identical single-file tests that only existed to run without bleeding edge. I deleted all three.
  • Their data files moved to tests/PHPStan/Analyser/data/no-bleeding-edge/.
  • The new tests/PHPStan/Analyser/NoBleedingEdgeTypeInferenceTest.php runs every file in that directory, without bleeding edge. Adding a new case is now just dropping a file there; no new test class is needed. Because the directory sits under data/, it's already excluded from lint, phpcs and phpstan, so no config changes were needed.
  • I left ScopePhpVersionTest and TypeInferenceTestCaseTest as they are. They also extend TypeInferenceTestCase without bleeding edge, but they test specific things rather than just running a data file.

Checks

  • The new test runs the same 10 assertions as the three old classes did.
  • With the AssignHandler.php fix temporarily reverted, 3 of them fail (the bug-15760 cases), so the test still catches the bug after the move.
  • make tests: 23866 tests pass, 116 skipped.

The PR #7095 test the reviewer linked isn't on this branch (I couldn't fetch that PR here), so it can move into the new directory once one of the two PRs is merged.

I didn't do anything about staabm's earlier question on whether the escaped mutations can be killed. I couldn't open the CI mutation report from here, and this review was only about consolidating the tests, so that question is still open.

@phpstan-bot

Copy link
Copy Markdown
Collaborator Author

I added tests that should kill the mutants, but I couldn't open the infection job log (gh had no token here). So I'm inferring which mutants escaped rather than reading them from the log.

Likely escaped mutants: the existing tests only covered the case where the reverse check $remainder->isSuperTypeOf($otherArm) returns yes. Nothing covered maybe. That means mutating the new ->no() calls to !->yes() changed no test result.

New tests (committed as 1082277fe, in tests/PHPStan/Analyser/data/bug-15760.php), in both arm orders:

$units = $blur ? ['px', 'rem'] : ['px', $s, '%'];
if ($units === ['px', 'rem', '%']) {
	assertType('bool', $blur);
}

Here the remainder array{'px', 'rem'} is only a maybe supertype of array{'px', string, '%'}. With either mutant applied, the bad guard is recorded and $blur is wrongly narrowed (true or false depending on arm order), so the assertion fails.

Mutants I applied by hand to AssignHandler.php: each of these now makes the test fail:

  • no() → !yes() on all four calls in the two changed conditions;
  • deleting either side of each &&;
  • && → ||.

make tests passed (23868 tests, 116 skipped) and make phpstan reported no errors. The re-run of the infection job after the push will show whether any mutant still escapes.

phpstan-bot and others added 3 commits November 18, 2026 15:25
…oth `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.
…er arm

These cases kill the escaped mutants that turn the reverse disjointness
check from ->no() into !->yes().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@clxmstaab
clxmstaab force-pushed the create-pull-request/patch-c5s4k58 branch from 1082277 to bf5fe33 Compare November 18, 2026 14:26

@staabm staabm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Escaped mutants:
================


1) /home/runner/work/phpstan-src/phpstan-src/src/Analyser/ExprHandler/AssignHandler.php:1287    [M] PHPStan\Infection\TrinaryLogicMutator [ID] 21d9d31d840feb227952f843a87717aa

@@ @@
 					// 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() && $truthyRemainder->isSuperTypeOf($falseyType)->no()) {
+					if (!$falseyType->isSuperTypeOf($truthyRemainder)->yes() && $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);


2) /home/runner/work/phpstan-src/phpstan-src/src/Analyser/ExprHandler/AssignHandler.php:1287    [M] PHPStan\Infection\TrinaryLogicMutator [ID] 221c8a86c2909094bd89b55ef06bff52

@@ @@
 					// 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() && $truthyRemainder->isSuperTypeOf($falseyType)->no()) {
+					if ($falseyType->isSuperTypeOf($truthyRemainder)->no() && !$truthyRemainder->isSuperTypeOf($falseyType)->yes()) {
 						$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);


3) /home/runner/work/phpstan-src/phpstan-src/src/Analyser/ExprHandler/AssignHandler.php:1293    [M] PHPStan\Infection\TrinaryLogicMutator [ID] 2df87dbefd5ce9e526f5a4f48129a783

@@ @@
 						$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() && $falseyRemainder->isSuperTypeOf($truthyType)->no()) {
+					if (!$truthyType->isSuperTypeOf($falseyRemainder)->yes() && $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);


4) /home/runner/work/phpstan-src/phpstan-src/src/Analyser/ExprHandler/AssignHandler.php:1293    [M] PHPStan\Infection\TrinaryLogicMutator [ID] d1dda8d469b0b1e9f908c171b4fe2558

@@ @@
 						$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() && $falseyRemainder->isSuperTypeOf($truthyType)->no()) {
+					if ($truthyType->isSuperTypeOf($falseyRemainder)->no() && !$falseyRemainder->isSuperTypeOf($truthyType)->yes()) {
 						$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);

add tests where possible

…enceTest so PHPUnit runs it

PHPUnit only collects files ending in Test.php, so the renamed class and
the bug-15736, bug-15749 and bug-15760 assertions were silently skipped,
which let the TrinaryLogic mutants in AssignHandler escape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@phpstan-bot

Copy link
Copy Markdown
Collaborator Author

The four escaped mutants were not a gap in the tests. The tests weren't running at all.

Cause: the refactor renamed the test class to NoBleedingEdgeTypeInferenceTestCase.php. phpunit.xml only picks up files ending in Test.php, so PHPUnit stopped running that class. That silently dropped every assertion from bug-15736.php, bug-15749.php and bug-15760.php. The remainderMaybeSupertypeOfOtherArm* cases in bug-15760.php are the ones meant to kill exactly these ->no() → !->yes() mutants. vendor/bin/phpunit --list-tests listed none of these tests before the fix.

Fix: I renamed the file and class to tests/PHPStan/Analyser/NoBleedingEdgeTypeInferenceTest.php. Nothing else referenced the old name. No new tests were needed.

Checks:

  • The class now runs 20 assertions, and all pass.
  • I applied each of the 4 reported mutants to AssignHandler.php by hand, one at a time. Each one makes the test fail, so all four are now killed.
  • make tests passes (23909 tests, 116 skipped) and make phpstan reports no errors.

I committed this as eda31530b and have not pushed it. The infection job will confirm the result once the branch is pushed.

@staabm
staabm merged commit 7ddcce8 into phpstan:2.3.x Nov 18, 2026
1431 of 1442 checks passed
@staabm
staabm deleted the create-pull-request/patch-c5s4k58 branch November 18, 2026 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

in_array() with a haystack chosen by a ternary narrows the ternary's condition to true (regression since 2.2.7)

3 participants