Skip to content

fix(docblock): a param docblock of an implementation keeps the inherited return type - #14360

Merged
orklah merged 5 commits into
vimeo:masterfrom
jirkace:fix/14243-inherit-return-with-param-docblock
Nov 22, 2026
Merged

orklah merged 5 commits into
vimeo:masterfrom
jirkace:fix/14243-inherit-return-with-param-docblock

Conversation

@jirkace

@jirkace jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

A @param-only docblock on an implementing method stops the @return of the parent from being inherited.

Before

/** @template T of Node */
interface Handler {
    /**
     * @param T $node
     * @return list<Message>
     */
    public function handle(Node $node): array;
}

/** @implements Handler<Leaf> */
final class LeafHandler implements Handler {
    /** @param Leaf $node */
    #[Override]
    public function handle(Node $node): array { // LessSpecificImplementedReturnType: 'list<Message>' ... 'array<array-key, mixed>'
        return [new Message()];
    }
}

After

    public function handle(Node $node): array { // no issue, the return type is `list<Message>`

Fixes #14243

…ted return type vimeo#14243

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: decreased

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 751 75.7 $\color{gray}{\textsf{-1.6\%}}$ 25 908 53 462 682
✅ symfony 3 600 218.3 $\color{gray}{\textsf{-1.5\%}}$ 50 610 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-1}}$ 98 400 $\color{green}{\textsf{-5}}$ 1 528
✅ doctrine-orm 381 13.0 $\color{gray}{\textsf{-1.4\%}}$ 3 477 $\color{red}{\textsf{+1}}$ $\color{green}{\textsf{-5}}$ 6 144 $\color{red}{\textsf{+1}}$ 0
✅ psl 617 17.1 $\color{gray}{\textsf{+2.4\%}}$ 1 375 5 615 1
✅ phpunit 455 14.4 $\color{gray}{\textsf{+1.3\%}}$ 1 731 3 410 18
✅ php-parser 226 9.3 $\color{gray}{\textsf{-0.1\%}}$ 17 132 21 025 0
✅ tempest 895 26.9 $\color{gray}{\textsf{+1.1\%}}$ 5 047 10 528 252
✅ amphp-amp 68 1.9 $\color{gray}{\textsf{+1.1\%}}$ 29 49 0
✅ webmozarts-assert 273 4.7 $\color{gray}{\textsf{+0.6\%}}$ 577 2 720 0
✅ guzzle-promises 44 1.6 $\color{gray}{\textsf{-0.6\%}}$ 155 434 2
✅ monolog 135 5.7 $\color{gray}{\textsf{+0.9\%}}$ 913 2 479 10
✅ drupal 4 841 143.1 $\color{gray}{\textsf{-3.9\%}}$ 97 591 169 293 583
Total 13 286 531.7 $\color{gray}{\textsf{-1.8\%}}$ 204 545 $\color{red}{\textsf{+1}}$ $\color{green}{\textsf{-6}}$ 373 559 $\color{green}{\textsf{-4}}$ 3 076
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run on the same runner; ±10 % is noise. Above +20 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Thresholds apply per project, not to the total.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.
✅ symfony: issues +0 −1

Memory 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

Issue type added removed
LessSpecificImplementedReturnType 0 1

Removed (1)

  • src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/DoctrineTransportFactory.php:38 LessSpecificImplementedReturnType (error): The inherited return type 'Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineTransport' for Symfony\Component\Messenger\Transport\TransportFactoryInterfac…
✅ doctrine-orm: issues +1 −5

Memory 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

Issue type added removed
InvalidReturnType 1 0
MixedReturnStatement 0 4
MixedReturnTypeCoercion 0 1

Added (1)

  • src/Mapping/LegacyReflectionFields.php:155 InvalidReturnType (error): The declared return type 'Generator<string, ReflectionProperty, mixed, mixed>' for Doctrine\ORM\Mapping\LegacyReflectionFields::getIterator is incorrect, got 'Generator<string, ReflectionProperty|null, mixed, void>'

Removed (5)

  • src/Mapping/ClassMetadata.php:614 MixedReturnStatement (error): Could not infer a return type
  • src/Mapping/ClassMetadata.php:614 MixedReturnStatement (error): Possibly-mixed return value
  • src/Mapping/ClassMetadata.php:633 MixedReturnStatement (error): Could not infer a return type
  • src/Mapping/ClassMetadata.php:633 MixedReturnStatement (error): Possibly-mixed return value
  • src/Mapping/LegacyReflectionFields.php:155 MixedReturnTypeCoercion (error): The declared return type 'Generator<string, ReflectionProperty, mixed, mixed>' for Doctrine\ORM\Mapping\LegacyReflectionFields::getIterator is more specific than the inferred return type 'Generator<string, mixed, mixed, voi…

head 15a45f1 · base 3a3fdf6 (merge-base) · workflow run

@jirkace

jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • A method implementing an interface or overriding a parent method with only @param docblocks now inherits the @return of the parent (LessSpecificImplementedReturnType no longer fires for the inherited return type); the docblock return type of the parent is used for calls too.
  • A method that documents its own @return is still compared with the parent (new invalid test).
  • Not run: the full suite (CI); benchmark corpora.

# Conflicts:
#	tests/ReturnTypeTest.php

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

jirkace and others added 2 commits November 21, 2026 17:06
…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>
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 6f529e3 (master merged in). In Populator::populateOverriddenMethods() a method with its own @param docblock inherits only if the overridden method has a documented @return and its type has no template of that method (returnTypeUsesAMethodTemplate()), as you suggested.

  • returnTypeWithATemplateOfTheOverriddenMethodIsNotInheritedByAMethodDocumentingItsParams is the first snippet with $d asserted (array<array-key, mixed>), ...MeetsTheTemplateOfTheOverridingMethod the one with @template U of int; they fail on the previous head with InvalidReturnStatement ... list<T:fn-p::f as mixed> and array<never, never>. They ignore LessSpecificImplementedReturnType and MoreSpecificImplementedParamType, which the child's own array and int report on any head.
  • nativeReturnTypeOfTheMethodIsKeptWhenTheOverriddenOneDocumentsOnlyItsParams is the tempest interface/trait snippet and ...OverriddenClassMethod... the one with a parent class; both fail before with MoreSpecificReturnType. @orklah

…rictly

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 22, 2026

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jirkace!

@orklah
orklah merged commit 1d02f4a into vimeo:master Nov 22, 2026
71 of 72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A @param-only docblock on an implementing method stops the parent @return from being inherited

2 participants