Repository navigation
fix(docblock): a param docblock of an implementation keeps the inherited return type - #14360
Conversation
…ted return type vimeo#14243 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
✅ Performance: unchanged · Types: decreased
How to read this table
✅ symfony: issues +0 −1Memory 3 600 → 3 600 MB (<0.1 %) · Wall 221.7 → 218.3 s (−1.5 %) Weighted 98 405 → 98 400 (−5, −0.01 %): L5 −1
Removed (1)
✅ doctrine-orm: issues +1 −5Memory 381 → 381 MB (<0.1 %) · Wall 13.2 → 13.0 s (−1.4 %) Weighted 6 143 → 6 144 (+1, +0.02 %): L1 −5 · L6 +1
Added (1)
Removed (5)
head 15a45f1 · base 3a3fdf6 (merge-base) · workflow run |
|
Behaviour change
|
# Conflicts: # tests/ReturnTypeTest.php
orklah
left a comment
There was a problem hiding this comment.
Thanks! The direction is right (master already inherits @param tags one by one, this does the same for @return), and the issue case is fixed. Now that documenting_method_ids is set for methods with their own @param docblock, two cases go wrong, both checked against the base:
1. The inherited @return uses a template of the parent method. The child's params no longer bind it (CallAnalyzer::checkMethodArgs only binds the templates of the documenting method when the method has no @param docblock), so the template leaks:
class P {
/**
* @template T
* @param T $x
* @return list<T>
*/
public function f($x): array { return [$x]; }
}
final class C extends P {
/** @param int $x */
#[Override]
public function f($x): array { return [$x]; } // InvalidReturnType, InvalidReturnStatement: 'list<T:fn-p::f as mixed>'
}
$d = (new C)->f(3); // list<T:fn-p::f as mixed>, was array<array-key, mixed>When the child declares its own @template U of int + @param U $x, (new C)->f(3) becomes array<never, never>, and (new C)->f(...) called with 3 reports InvalidArgument: Argument 1 expects U:fn-c::f as int, but 3 provided.
2. The parent documents only its params. There is no @return to inherit, but the parent's native return type now replaces the child's. A new false positive on tempest (HasConditions::unless):
interface Console {
/** @param positive-int $n */
public function times(int $n): self;
/** @param positive-int $n */
public function twice(int $n): self;
}
trait HasTimes {
/** @param positive-int $n */
public function times(int $n): static { return $this; }
/** @param positive-int $n */
public function twice(int $n): static {
return $this->times($n); // MoreSpecificReturnType: 'Console&GenericConsole&static' vs inferred 'Console&GenericConsole'
}
}
final class GenericConsole implements Console { use HasTimes; }Suggested fix, in the same condition of Populator::populateOverriddenMethods: when the method has its own @param docblock, only inherit if the overridden method has a docblock @return and that type contains no template of the overridden method. Please add both snippets as test cases (the first with an assertion on $d), plus one with a parent class instead of an interface.
…eturn-with-param-docblock
…return type without a template of the overridden method Populator::populateOverriddenMethods() lets a method with its own @PARAM docblock take the return type of the overridden method only when that documents it and it uses no template of that method: the params of the overriding method do not bind such a template. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Pushed 6f529e3 (master merged in). In
|
…rictly Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A
@param-only docblock on an implementing method stops the@returnof the parent from being inherited.Before
After
Fixes #14243