Fix #12244: $this(Type~Subtracted) does not work on conditional return type as Type~Subtracted#5104
Open
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
Open
Conversation
- When a method has a conditional return type like `@return ($this is self::A ? int : null)`, and `$this` has a subtracted type (e.g. `$this(X~X::A)` from narrowing), the conditional was not properly resolved because the subtracted type was not propagated - The root cause was in `StaticType::transformStaticType` which transforms `StaticType` instances in return types but did not propagate the calling type's subtracted type - Added subtracted type propagation in `StaticType::transformStaticType` to match the behavior of `CalledOnTypeUnresolvedMethodPrototypeReflection::transformStaticType` - New regression test in tests/PHPStan/Analyser/nsrt/bug-12244.php Closes phpstan/phpstan#12244
fa23f75 to
f871587
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a method has a conditional return type referencing
$this(e.g.,@return ($this is self::A ? int : null)) and$thishas been narrowed via type subtraction (e.g., insideif ($this !== self::A)), the conditional return type was not properly resolved. It returnedint|nullinstead ofnull, even though the same narrowing worked correctly for non-$thisvariables.Changes
StaticType::transformStaticType()insrc/Type/StaticType.phpto propagate the calling type's subtracted type when transformingStaticTypeinstances in return typestests/PHPStan/Analyser/nsrt/bug-12244.phpRoot cause
When PHPStan resolves method return types, it transforms
StaticType/ThisTypeinstances in the return type to reflect the actual calling type. For regular object types ($x: X~X::A),CalledOnTypeUnresolvedMethodPrototypeReflection::transformStaticTypereplacesStaticTypewith the full caller type including its subtracted type. However, for$thistypes ($this(X~X::A)),StaticType::transformStaticTypeonly changed the base class ofStaticTypeinstances without propagating the subtracted type from the calling$this. This meant the conditional($this is self::A ? int : null)could not determine that$thiswas definitely notself::A.The fix adds subtracted type propagation in
StaticType::transformStaticType, ensuring consistency with howCalledOnTypehandles the same scenario.Test
Added
tests/PHPStan/Analyser/nsrt/bug-12244.phpwhich tests:$this->get()inside an instance method after narrowing$thisviaif ($this !== self::A)correctly returnsnull$x->get()inside a static method after narrowing$xviaif ($x !== self::A)correctly returnsnull(was already working)Fixes phpstan/phpstan#12244