Skip to content

Keep the native values the PHPDoc type rules out in disjunction branch unions - #7220

Merged
ondrejmirtes merged 2 commits into
2.3.xfrom
fix-15858-native-type-leak
Nov 27, 2026
Merged

ondrejmirtes merged 2 commits into
2.3.xfrom
fix-15858-native-type-leak

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Closes phpstan/phpstan#15858

DisjunctionBranchUnionAugment and DisjunctionHolderProjectionAugment pin the union of the subject's PHPDoc types in the two branches of || as a sure type, and applySpecifiedTypes() intersects a sure type into the native type too. With treatPhpDocTypesAsCertain: false, a PHPDoc-only array shape became the native type of a parameter without a native type (Dolibarr: nullCoalesce.offset). The leak predates 0e9fdb5 (e.g. the native array of bug-14566 became the PHPDoc shape); the exact shape union only made the augment fire in this case.

Where the PHPDoc type describes more than the native type, the subject's entry is now the alternative of the union and of whatever the PHPDoc type does not describe (built with the existing SpecifiedTypes::intersectWith()):

  • PHPDoc type: narrows to the union, as before.
  • Native type: keeps every value the PHPDoc type rules out; it only loses values the condition itself excludes, e.g. mixed~array{http_code: 200, content: non-falsy-string} for the issue's snippet.
  • Native type equal to the PHPDoc type: both narrow to the union.

ImpossibleInstanceOfRuleTest line 234 now carries the PHPDoc tip: $intersection has no native type, so the always-false verdict comes from PHPDoc only.

turbo-ext: the same in both augments through one shared helper; smoke, signature parity and the full walk-trace corpus are identical, and analysis output is byte-identical with the extension on and off.

…h unions

DisjunctionBranchUnionAugment and DisjunctionHolderProjectionAugment pin
the union of the subject's types in the two branches of `||` (or of a
negated `&&`) as a sure type. Those branch types are PHPDoc types, and
applySpecifiedTypes() intersects a sure type into the native type too, so
with treatPhpDocTypesAsCertain: false a PHPDoc-only array shape became the
native type of a parameter without a native type, and rules reported
errors based on it. The exact union of shapes since 0e9fdb5 exposed it:
before, the two branch shapes merged back into the original shape and the
augment did not fire.

Where the PHPDoc type of the subject describes more than its native type,
the subject's entry is now the alternative of the union and of whatever
the PHPDoc type does not describe. The PHPDoc type still narrows to the
union; the native type keeps every value the PHPDoc type rules out and
only loses the values the condition itself excludes. A subject whose
native type equals its PHPDoc type still narrows both to the union.

turbo-ext: the same in both augments, through one shared helper.
Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4a-d975-75f9-9537-a0c2fc5fdea6
@ondrejmirtes
ondrejmirtes merged commit a0e20ae into 2.3.x Nov 27, 2026
879 of 917 checks passed
@ondrejmirtes
ondrejmirtes deleted the fix-15858-native-type-leak branch November 27, 2026 14:17
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.

PHPDoc array shape leaks into the native type after isset()/empty() with treatPhpDocTypesAsCertain: false (since phpstan-src 0e9fdb53e, #7195)

1 participant