Resolve $this on both sides of a final class's method signature check - #6641
Open
SanderMuller wants to merge 1 commit into
Open
SanderMuller wants to merge 1 commit into
SanderMuller wants to merge 1 commit into
Conversation
MethodSignatureRule changed the parent's $this to the plain class in a final class, but kept the child's $this. Inside an invariant template argument the two types then did not match, and the rule reported an override as incompatible with a type that prints the same. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.
Closes phpstan/phpstan#13418
In a final class,
MethodSignatureRulereported a method as incompatible with a parent type that prints the same:transformStaticType()changesstaticand$thisin the parent's type to the plain class when the declaring class is final. The child's own$thisstayed aThisType. Inside an invariant template argument, the class and$this(Direct)then do not match.staticin the child does not have the problem, because PHPStan already resolves it to the class in a final class. Non-final classes are not affected, because theretransformStaticType()keeps the$this.This PR applies
transformStaticType()to the child's type too, at the two comparison points. The types in the error messages do not change.The test data has the issue's snippet. It also has the
@implements Billable<$this>shape, through a trait and as a direct method, for return and parameter types.WrongClass(Rel<Other>) is still reported.In a final class, a child
Rel<$this>against a parentRel<NamedParent>is now accepted. It was reported before. The parent side already worked this way: a parentRel<$this>against a childRel<ConcreteClass>was already accepted.NamedParentandConcreteClassin the test data cover both directions.make tests,make phpstanand phpcs on the changed files pass. The new test fails without the change. I did not measure performance. The change adds one type traversal for each compared return or parameter type.🤖 Generated with Claude Code