Fix phpstan/phpstan#4860: Error „should return static(Test) but returns Test“#5190
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#4860: Error „should return static(Test) but returns Test“#5190phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- StaticType::getClassStringType() was delegating to getStaticObjectType() which returned the base ObjectType, losing the static/this type information - Changed it to return GenericClassStringType($this) directly, preserving the static type so get_class($this) correctly returns class-string<static(T)> - This fixes false positives when passing get_class($this) to template methods expecting class-string<T> and returning T, where the return should be static - New regression test in tests/PHPStan/Analyser/nsrt/bug-4860.php - Updated existing test expectations to reflect the more precise type
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
get_class($this)was returningclass-string<ClassName>instead ofclass-string<static(ClassName)>, causing false positive errors when the result was passed to a template method that should preserve thestatictype. For example, acopy(): staticmethod calling$this->copyTo(get_class($this))wherecopyTohas@template Tand@param class-string<T>/@return Twould incorrectly report "should return static(Test) but returns Test".Changes
StaticType::getClassStringType()insrc/Type/StaticType.phpto returnnew GenericClassStringType($this)instead of delegating togetStaticObjectType()->getClassStringType(), which was losing the static type informationtests/PHPStan/Analyser/data/bug-7391b.php,tests/PHPStan/Analyser/nsrt/get-parent-class.php, andtests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.phpto reflect the more precise type descriptionsRoot cause
StaticType::getClassStringType()was delegating togetStaticObjectType()which resolves theStaticTypeto a plainObjectType, losing thestatic/$thisinformation. This meantget_class($this)returnedclass-string<Foo>instead ofclass-string<$this(Foo)>. When this was passed to a template method with@param class-string<T> @return T, the templateTresolved toFooinstead ofstatic(Foo), causing the false positive.Test
Added
tests/PHPStan/Analyser/nsrt/bug-4860.phpwhich verifies thatget_class($this)returnsclass-string<$this(ClassName)>and that passing it to a template method correctly preserves the static type.Fixes phpstan/phpstan#4860