Skip to content

@return static<...> arguments are applied by position to the called subclass instead of projected through @extends #13988

Open

Description

@alies-dev

https://psalm.dev/r/747a6e8dc8

What Psalm reports / does

@return static<TA, int> is declared on Base<TA, TB>, and the call is made on a subclass whose own template list differs from Base's:

  • Swapped<bool, string>, declared @extends Base<TY, TX>, is Base<string, bool>. withIntB() gives Trace: $r: Swapped<string, int>&static.
  • Prefixed<float, string, bool>, declared @extends Base<TA2, TB2>, is Base<string, bool>. withIntB() gives Trace: $r: Prefixed<string, int>&static. That drops TZ and has 2 type arguments for a 3-template class.

Psalm applies the resolved arguments of static<...> by position to the template list of the class it is called on.

What you expected

The arguments of static<...> belong to the declaring class's templates, so they should be projected onto the called class through its @extends mapping:

  • Swapped<int, string> (TX is TB, so int; TY is TA, so string)
  • Prefixed<float, string, int> (TZ stays float)

An overriding method's inherited return type is projected the same positional way: an override withIntB(): static { return $this; } in Swapped gets the declared return Swapped<TY, int> instead of Swapped<int, TY>.

Psalm version, PHP version

psalm.dev dev-master@7f128f6cd, PHP 8.3; also dev-master@7b7e53e1a locally, PHP 8.5.

Impact

psalm/psalm-plugin-laravel has to build Laravel's factory count() result in a return type provider, mapping the subclass's template arguments by hand (psalm/psalm-plugin-laravel#1625). A stub declaring @return static<TModel, ...> would mis-type every user factory subclass whose template list differs from Factory<TModel, TCount>.

Activity

  1. psalm-github-bot commented on Nov 16, 2026

    @psalm-github-bot

    I found these snippets:

    https://psalm.dev/r/747a6e8dc8
    <?php
    
    /**
     * @template TA
     * @template TB
     */
    abstract class Base
    {
        /** @return static<TA, int> */
        abstract public function withIntB(): static;
    }
    
    /**
     * Same templates, declared in the opposite order.
     *
     * @template TX
     * @template TY
     * @extends Base<TY, TX>
     */
    abstract class Swapped extends Base {}
    
    /**
     * One extra template in front.
     *
     * @template TZ
     * @template TA2
     * @template TB2
     * @extends Base<TA2, TB2>
     */
    abstract class Prefixed extends Base {}
    
    /** @param Swapped<bool, string> $s */
    function swapped(Swapped $s): void
    {
        $r = $s->withIntB();
        /** @psalm-trace $r */
    }
    
    /** @param Prefixed<float, string, bool> $p */
    function prefixed(Prefixed $p): void
    {
        $r = $p->withIntB();
        /** @psalm-trace $r */
    }
    Psalm output (using commit 7f128f6):
    
    INFO: Trace - 36:0 - $r: Swapped<string, int>&static
    
    INFO: Trace - 43:0 - $r: Prefixed<string, int>&static
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions