Repository navigation
Conversation
A method that overrides a generic parent's method without a PHPDoc of its own inherits the parent's tags with the template types replaced by the child's type arguments. The conditional type stayed a conditional type with a concrete subject, so MethodConditionalReturnTypeRule reported `(string is int ? string : int)` on the child as if it had been written there. A static subject is kept, since it depends on the class the method is called on, and so is a conditional type that resolves to another one, including a conditional on a parameter, because MethodSignatureRule compares it with the parent's conditional type branch by branch. Closes phpstan/phpstan#15128
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#15128
A child class that binds the parent's template type and overrides a method without a PHPDoc of its own gets an error about the parent's PHPDoc. The issue's snippet, shortened:
PHPStan reports on
B::foo():B::foo()inherits the tags throughResolvedPhpDocBlock::resolveTemplateTypeInTag(), which replacesTwithstringbut keeps the conditional type, soConditionalReturnTypeRuleHelperchecks(string is int ? string : int)as if it had been written onB::foo(). An inherited@paramtag, an interface method and an abstract trait method get the same error, and a child declared with@template U of intand@extends A<U>getsCondition "U of int is int" in conditional return type is always true.As suggested in the issue, the inherited conditional type is now resolved once its condition no longer depends on a template type, or the bound of that template type decides it. A
staticsubject stays as it is, since it depends on the class the method is called on, and so does a conditional type that would resolve to another one, including a conditional on a parameter, becauseMethodSignatureRulecompares it with the parent's conditional type branch by branch.A conditional on a parameter, such as
($val is int ? string : int)with an inherited@param T $val, is left as it is. Whether it is decided depends on the parameter's final type, which a= nulldefault or the native type can still change, and call sites already resolve it against the argument.