Repository navigation
Compare branches of two conditional types in isSuperTypeOf() only when they share the same condition, and include negation in equals() - #7097
Conversation
…hen they share the same condition, and include negation in `equals()` - ConditionalType::isSuperTypeOf() and ConditionalTypeForParameter::isSuperTypeOf() compared the if/else branches of any two conditionals, ignoring subject / parameter name, target and negation. `($p is lowercase-string ? lowercase-string : string)` was therefore considered a supertype of `($p is '' ? decimal-int-string : string)` (decimal-int-string is lowercase), and TypeCombinator::intersect() dropped it. The branch comparison now only happens when the negation, the subject (or parameter name) and the target are equal; otherwise the late-resolvable default is used. - The same mistake made TypeCombinator::union() collapse conditionals with different conditions (wrong union results, and `is` vs `is not` conditionals absorbing each other). - ConditionalType::equals() and ConditionalTypeForParameter::equals() ignored `negated`, so `($p is int ? A : B)` equalled `($p is not int ? A : B)`. - Ported both changes to the turbo-ext C++ mirrors (ConditionalType.cpp, ConditionalTypeForParameter.cpp) via a shared hasSameCondition() helper, and extended the type-family differential coverage.
VincentLanglet
left a comment
There was a problem hiding this comment.
Explain why the bug was specific to the exemple with decimal-int-string and numeric-string was already working fine
|
The bug wasn't really about The comparison before the fix. Take the two conditionals from the issue:
When the PHPDoc is resolved, The else branches always give "yes". The result therefore depends only on whether
Why the "yes" was wrong. A takes its if-branch when Why What the fix changes. Branches are now compared only when both conditionals have the same parameter or subject, target and negation. In every other case the code falls back to the late-resolvable default. The union and |
| public function equals(Type $type): bool | ||
| { | ||
| return $type instanceof self | ||
| && $this->negated === $type->negated |
There was a problem hiding this comment.
this change is not covered by a test
There was a problem hiding this comment.
I added the missing test coverage in one commit, 518de62ab, and changed no source code. make tests, make phpstan and make cs all pass. Nothing is pushed.
"Not covered by a test" (the negated check in equals() of both classes, and the negated and parameterName checks in ConditionalTypeForParameter::isSuperTypeOf()):
I added two unit tests, tests/PHPStan/Type/ConditionalTypeTest.php and tests/PHPStan/Type/ConditionalTypeForParameterTest.php. Each one checks:
isSuperTypeOf(): returns "yes" for the same condition, including when the other type's branches are narrower. Returns "maybe" when only the negation, the subject (or parameter name, e.g.$xvs$y), or the target differs.equals(): returns true for identical conditionals, and false when exactly one of these differs: negation, subject or parameter name, target, if, else. Both directions are checked.
With the old versions of the two PHP classes swapped back in, 8 of these cases fail: each new condition in isSuperTypeOf() and the negated check in both equals() methods. All 22 pass with the fix.
"Do we really support intersections of conditional types in phpdoc?"
Yes, they already worked before this PR; the only problem was the wrong collapse it fixes. phpdoc-parser reads (A) & (B) with parenthesised conditionals as an intersection node. TypeNodeResolver::resolveIntersectionTypeNode() (src/PhpDoc/TypeNodeResolver.php:701) combines the parts with TypeCombinator::intersect(), and each conditional is resolved against the argument at the call site.
I checked this by analysing a standalone file with bin/phpstan:
| Argument | Result |
|---|---|
'foo-' |
lowercase-string |
'' |
decimal-int-string |
'FOO' |
string |
an unknown string |
string |
The WordPress wp_unique_id() return type in the issue uses exactly this form. I found no existing test with an intersection of conditional types, so bug-15759.php is the first one.
| * @return ( | ||
| * ( $p is '' ? decimal-int-string : string ) | ||
| * & ( $p is lowercase-string ? lowercase-string : string ) | ||
| * ) |
There was a problem hiding this comment.
do we really support intersections of conditional types in phpdoc?
| public function equals(Type $type): bool | ||
| { | ||
| return $type instanceof self | ||
| && $this->negated === $type->negated |
There was a problem hiding this comment.
this change is not covered by a test
| if ( | ||
| $type instanceof self | ||
| && $this->negated === $type->negated | ||
| && $this->parameterName === $type->parameterName |
There was a problem hiding this comment.
this line is not covered by a test
… conditions Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
A return type that intersects two conditional types, such as
($p is '' ? decimal-int-string : string) & ($p is lowercase-string ? lowercase-string : string),lost
lowercase-string. The intersection was collapsed into the first conditional when the PHPDoc was resolved. This happened becauseConditionalType*::isSuperTypeOf()compared only the if/else branches of two conditionals and ignored what each one was conditioned on.Changes
src/Type/ConditionalType.php:isSuperTypeOf()compares branches only whennegated,subjectandtargetare equal. Otherwise it falls back toisSuperTypeOfDefault().equals()now also comparesnegated.src/Type/ConditionalTypeForParameter.php: the same change, usingparameterNamein place ofsubject.turbo-ext/src/ConditionalType.cpp,turbo-ext/src/ConditionalTypeForParameter.cpp: the C++ mirrors use a newhasSameCondition()helper, shared byisSuperTypeOf()andequals(). Reads happen in the same order as the PHP twin.turbo-ext/tests/type-family.php: added differential cases for conditionals whose subject, target or parameter name differ.Analogous cases found and fixed by the same change:
TypeCombinator::union()also relies onisSuperTypeOf(). As a result,($p is '' ? decimal-int-string : string) | ($p is lowercase-string ? lowercase-string : string)gavelowercase-stringfor'foo-'(the correct result isstring).isvsis not:($p is int ? int : string) | ($p is not int ? int : string)collapsed to a single conditional, givingstring/intinstead ofint|string.ConditionalType(T is '' ? ... : ...), which is theConditionalTypesibling ofConditionalTypeForParameter, showed the same symptom as the issue.equals()ignored negation in both classes.Root cause
isSuperTypeOf()on two conditionals checkedif ⊇ if'andelse ⊇ else'. That is only sound when both conditionals choose a branch from the same condition. With different conditions, the branches that end up selected are unrelated.TypeCombinatorthen treated one conditional as absorbing the other in intersections and unions. Requiring the same negation, subject or parameter name, and target keeps the precise answer for identical conditions and uses the sound late-resolvable default otherwise.Test
tests/PHPStan/Analyser/nsrt/bug-15759.php:is/is notunion;ConditionalType) intersection.Without the fix, the test fails on 7 assertions.
turbo-ext/tests/type-family.php: new "cond pairs" observations. Without the PHP change these differ from the native results.make testspasses, both without the extension (apart from a missing// lint >= 8.0header on the new test, now added) and with the turbo extension loaded.make phpstanpasses. The turbo smoke, side-by-side and signature-parity checks pass.Fixes phpstan/phpstan#15759