Skip to content

fix(templates): compare the conditional return type of an implementation by its branches, as the guide's - #14502

Merged
orklah merged 3 commits into
vimeo:masterfrom
jirkace:fix/12988-implemented-conditional-return
Nov 24, 2026
Merged

orklah merged 3 commits into
vimeo:masterfrom
jirkace:fix/12988-implemented-conditional-return

Conversation

@jirkace

@jirkace jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

An implemented method with the same conditional return type as the interface method reports ImplementedReturnTypeMismatch: the interface's conditional is evaluated to its branches before the comparison, the implementation's isn't.

Before

<?php
/** @template T = mixed */
interface Box {
    /**
     * @template U = never
     * @param (callable(T): U)|null $f
     * @return Box<($f is null ? T : U)>
     */
    public function map(?callable $f = null): Box;
}

/**
 * @template T = mixed
 * @implements Box<T>
 */
final class Just implements Box {
    /** @param T $value */
    public function __construct(private mixed $value) {}

    /**
     * @template U = never
     * @param (callable(T): U)|null $f
     * @return Box<($f is null ? T : U)>
     */
    public function map(?callable $f = null): Box { // ImplementedReturnTypeMismatch
        return $f === null ? $this : new Just($f($this->value));
    }
}

After

    public function map(?callable $f = null): Box { // no issue

Fixes #12988

…ion by its branches, as the guide's vimeo#12988

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • MethodComparator::compareMethodDocblockReturnTypes() now expands the implementer's return type with its conditional types evaluated, as it already did for the guide's (interface or parent) return type. Both sides are compared by the union of their branches.
  • Effect: no false ImplementedReturnTypeMismatch for an implementation repeating the guide's conditional return type (new valid case implementedMethodWithTheConditionalReturnTypeOfTheInterface, fails on master). A conditional whose branches leave the guide's type is now reported as ImplementedReturnTypeMismatch (($f is null ? array : int) against ($f is null ? string : int)), where master reported nothing (new invalid case implementedMethodWithAConditionalReturnTypeOutsideTheInterfaces, fails on master).
  • Checked: the new cases and inheritConditional on the branch; phpcs clean; full self-analysis shows only the two PoolEventLoopDriverTest MixedArgument errors that master shows locally too. Not run: the full test suite (CI) and the corpus comparison.

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: decreased

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 757 61.9 $\color{gray}{\textsf{-2.2\%}}$ 25 784 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-1}}$ 53 048 $\color{green}{\textsf{-4}}$ 743 0
✅ symfony 3 650 288.1 $\color{gray}{\textsf{+2.4\%}}$ 51 752 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-1}}$ 100 483 $\color{green}{\textsf{-4}}$ 1 585 0
✅ doctrine-orm 384 25.1 $\color{gray}{\textsf{+0.5\%}}$ 3 453 6 194 0 0
✅ psl 622 29.2 $\color{gray}{\textsf{+1.3\%}}$ 1 396 5 647 1 0
✅ phpunit 432 22.5 $\color{gray}{\textsf{+2.6\%}}$ 1 590 2 403 18 0
✅ php-parser 228 14.6 $\color{gray}{\textsf{-0.6\%}}$ 17 113 21 177 0 0
✅ tempest 894 38.0 $\color{gray}{\textsf{-0.9\%}}$ 5 093 10 547 275 0
✅ amphp-amp 48 1.5 $\color{gray}{\textsf{-1.3\%}}$ 31 53 0 0
✅ webmozarts-assert 275 4.3 $\color{gray}{\textsf{-0.5\%}}$ 576 2 720 0 0
✅ guzzle-promises 46 1.6 $\color{gray}{\textsf{-2.5\%}}$ 153 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-2}}$ 425 $\color{green}{\textsf{-8}}$ 2 0
✅ monolog 135 5.7 $\color{gray}{\textsf{+0.5\%}}$ 918 2 491 11 0
✅ drupal 4 797 232.8 $\color{gray}{\textsf{+0.7\%}}$ 97 829 169 274 575 0
✅ wordpress 2 028 199.4 $\color{gray}{\textsf{-0.2\%}}$ 143 502 678 358 505 1
Total 15 296 924.7 $\color{gray}{\textsf{+0.8\%}}$ 349 190 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-4}}$ 1 052 820 $\color{green}{\textsf{-16}}$ 3 715 1
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 +10 % 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. Its wall time warns above +5 % (±5 % is noise); the other thresholds apply per project.
  • Profiling: when both sides support it, the measured runs use --debug-performance=PATH for the hot-files block, which adds the same small CPU and memory cost to both sides.
  • 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.
✅ laravel: issues +0 −1

Memory 1 757 → 1 757 MB (<0.1 %) · Wall 63.3 → 61.9 s (−2.2 %)

Weighted 53 052 → 53 048 (−4, −0.01 %): L4 −1

Issue type added removed
ImplementedReturnTypeMismatch 0 1

Removed (1)

  • src/Illuminate/Container/Container.php:833 ImplementedReturnTypeMismatch (error): The inherited return type 'Closure[impure]():TClass:fn-illuminate\contracts\container\container::factory as object|Closure[impure]():mixed' for Illuminate\Contracts\Container\Container::factory is different to the imp…
✅ symfony: issues +0 −1

Memory 3 650 → 3 650 MB (<0.1 %) · Wall 281.4 → 288.1 s (+2.4 %)

Weighted 100 487 → 100 483 (−4, <0.01 %): L4 −1

Issue type added removed
ImplementedReturnTypeMismatch 0 1

Removed (1)

  • src/Symfony/Component/HttpFoundation/InputBag.php:115 ImplementedReturnTypeMismatch (error): The inherited return type '(T:fn-parameterbag::getenum as BackedEnum)|null' for ParameterBag::getEnum is different to the implemented return type for InputBag::getEnum '(TGeneratedFromParam2 is null ? (T:fn…
✅ guzzle-promises: issues +0 −2

Memory 46 → 46 MB (<0.1 %) · Wall 1.6 → 1.6 s (−2.5 %)

Weighted 433 → 425 (−8, −1.85 %): L4 −2

Issue type added removed
ImplementedReturnTypeMismatch 0 2

Removed (2)

  • src/Coroutine.php:104 ImplementedReturnTypeMismatch (error): The inherited return type 'PromiseInterface<(TFulfilledValue:fn-promiseinterface::then as mixed)|(TRejectedValue:fn-promiseinterface::then as mixed)|(TValue:Coroutine as mixed), (TFulfilledReason:fn-promiseinterface::then as mixed)|(TReas…
  • src/Promise.php:62 ImplementedReturnTypeMismatch (error): The inherited return type 'PromiseInterface<(TFulfilledValue:fn-promiseinterface::then as mixed)|(TRejectedValue:fn-promiseinterface::then as mixed)|(TValue:Promise as mixed), (TFulfilledReason:fn-promiseinterface::then as mixed)|(TReason:Pr…

head b5b1e6e · base 66b5f4a (merge-base) · workflow run

…the values the guide's param takes vimeo#12988

An implementation widening `class-string<T>` to `string|class-string<T>`
with `@return ($className is class-string<T> ? T : object)` was compared
by both branches, so its `object` branch, which only strings the guide
never takes reach, made it an `ImplementedReturnTypeMismatch` (doctrine-orm
`EntityManagerInterface::getClassMetadata()`). Only the branch the guide's
values reach is compared when that is decidable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Review findings of the triage report (head 632db32), checked:

  • Confirmed and fixed in 09cb0a1: the perf report's new ImplementedReturnTypeMismatch at doctrine-orm src/EntityManagerInterface.php:240. The implementation widens the param to string|class-string<T> and returns ($className is class-string<T> ? ClassMetadata<T> : ClassMetadata<object>). Compared by both branches, its ClassMetadata<object> branch did not fit the guide's ClassMetadata<T>, although only strings the guide never takes reach that branch. Now a conditional on a param is read for the values of the guide's param at the same position, with templates read as their bounds. Only the if branch is compared when those values all fit the condition, only the else branch when none does, and both otherwise.
  • New tests:
    • ConditionalReturnTypeTest::testValidCode@implementationWideningAParamReturnsTheGuidesTypeForTheGuidesValues fails on the previous head. In a probe, master reports this case too, with the conditional in the message.
    • testInvalidCode@conditionalOfAnImplementationOnPartOfTheGuidesValuesIsComparedByBothBranches guards the case where the guide takes string and the condition is "x": still reported. It passes on the previous head too.
    • The two cases of this PR and inheritConditional still pass.
  • Self-analysis shows only the two local PoolEventLoopDriverTest MixedArgument errors. phpcs is clean. Not run: the full suite (CI runs it) and doctrine-orm itself, only a reduced copy of its interfaces.

@orklah orklah closed this Nov 24, 2026
@orklah orklah reopened this Nov 24, 2026
@orklah orklah closed this Nov 24, 2026
@orklah orklah reopened this Nov 24, 2026
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 24, 2026
@orklah

orklah commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Thanks @jirkace!

@orklah
orklah merged commit d923102 into vimeo:master Nov 24, 2026
72 of 73 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.

Implemented method with the same conditional return type as the interface reports ImplementedReturnTypeMismatch

2 participants