Skip to content

Fix (templates): bind a composed @extends argument on inherited members - #14558

Merged
alies-dev merged 7 commits into
vimeo:masterfrom
alies-dev:alies-dev/composed-extends-argument
Nov 21, 2026
Merged

alies-dev merged 7 commits into
vimeo:masterfrom
alies-dev:alies-dev/composed-extends-argument

Conversation

@alies-dev

@alies-dev alies-dev commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Inherited invokable

When a parent is extended with an argument built from the child's templates (@extends Base<Box<TModel>>), the callable of its __invoke keeps the parent's template unbound.

Before

<?php
/** @template T */
final class Box {}

/** @template T */
abstract class Base {
    /** @param T $value */
    public function __invoke(mixed $value): void {}
}

/**
 * @template TModel of object
 * @extends Base<Box<TModel>>
 */
final class Composed extends Base {}

/** @param callable(Box<stdClass>): void $callback */
function takesCallback(callable $callback): void {}

/** @param Composed<stdClass> $composed */
function f(Composed $composed): void {
    takesCallback($composed); // InvalidArgument: expects callable(Box<stdClass>):void, but callable(T:Base as mixed):void provided
    takesCallback($composed(...)); // InvalidArgument: expects callable(Box<stdClass>):void, but Closure(T:Base as mixed):void provided
    takesCallback(Closure::fromCallable($composed)); // InvalidArgument: expects callable(Box<stdClass>):void, but Closure(T:Base as mixed):void provided
}

After

    takesCallback($composed); // no issue
    takesCallback($composed(...)); // no issue
    takesCallback(Closure::fromCallable($composed)); // no issue

Two @extends levels

When a composed argument goes through a class in between, methods and the invokable keep that class's template.

Before

<?php
/** @template T */
final class Box {}

/** @template T */
abstract class Base {
    /** @param T $value */
    public function __invoke(mixed $value): void {}

    /** @return T */
    public function get(): mixed {}

    /** @return T */
    public static function make(): mixed {}
}

/**
 * @template TItem of object
 * @extends Base<list<Box<TItem>>>
 */
abstract class Middle extends Base {}

/**
 * @template TModel of object
 * @extends Middle<TModel>
 */
final class Leaf extends Middle {}

/** @param callable(list<Box<stdClass>>): void $callback */
function takesCallback(callable $callback): void {}

/** @param Leaf<stdClass> $leaf */
function f(Leaf $leaf): void {
    takesCallback($leaf); // InvalidArgument: expects callable(list<Box<stdClass>>):void, but callable(T:Base as mixed):void provided
}

/** @return Leaf<stdClass> */
function make(): Leaf {}

$leaf = make();
$get = $leaf->get(); // list<Box<TItem:Middle as object>>
$static = $leaf::make(); // list<Box<TItem:Middle as object>>

After

    takesCallback($leaf); // no issue
$get = $leaf->get(); // list<Box<stdClass>>
$static = $leaf::make(); // list<Box<stdClass>>

A child that isn't generic (@extends Base<string>) passed as a callable still keeps the template unbound: #14567.

Part of #13143

A member inherited from a parent extended with an argument that mentions
templates of the child (`@extends Base<Box<TModel>>`, `@extends Base<list<U>>`)
kept the template of the parent or of the class in between unbound: reading
`Composed<User>->value` gave `Box<TModel:Composed as object>`, an invokable
`Composed<User>` was `callable(T:Base as mixed)`, and with two `@extends`
levels even methods returned `list<Box<TItem:Middle as object>>`.

The property and the callable now bind such an argument like a method call
does, and the templates of a class in between nested in an argument are
replaced by what the child passes to it.

Fixes vimeo#13143
@alies-dev alies-dev added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 21, 2026
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 749 71.5 $\color{gray}{\textsf{-1.1\%}}$ 25 907 53 460 682
✅ symfony 3 599 270.9 $\color{gray}{\textsf{+0.5\%}}$ 50 610 98 403 1 528
✅ doctrine-orm 380 20.4 $\color{gray}{\textsf{-0.5\%}}$ 3 481 6 143 0
✅ psl 617 26.7 $\color{gray}{\textsf{-0.6\%}}$ 1 375 5 615 1
✅ phpunit 455 19.9 $\color{gray}{\textsf{+0.4\%}}$ 1 731 3 410 18
✅ php-parser 226 13.4 $\color{gray}{\textsf{+0.4\%}}$ 17 154 21 245 0
✅ tempest 895 36.8 $\color{gray}{\textsf{+1.3\%}}$ 5 047 10 528 252
✅ amphp-amp 45 1.4 $\color{gray}{\textsf{-1.4\%}}$ 29 49 0
✅ webmozarts-assert 273 4.3 $\color{gray}{\textsf{+0.5\%}}$ 577 2 720 0
✅ guzzle-promises 44 1.5 $\color{gray}{\textsf{+0.7\%}}$ 155 434 2
✅ monolog 135 5.5 $\color{gray}{\textsf{-0.5\%}}$ 913 2 479 10
✅ drupal 4 842 237.8 $\color{gray}{\textsf{+1.5\%}}$ 97 591 169 293 583
Total 13 260 710.2 $\color{gray}{\textsf{+0.6\%}}$ 204 570 373 779 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.

head 68f2283 · base 3130738 (merge-base) · workflow run

@alies-dev alies-dev self-assigned this Nov 21, 2026
…d-extends-argument

# Conflicts:
#	src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/AtomicPropertyFetchAnalyzer.php
#	tests/Template/ClassTemplateExtendsTest.php
…ertyType()

Populator::extendType() now substitutes the templates of a class in
between wherever they are nested in an @extends argument, so the class
storage's template_extended_params already resolves `Grand` ->
`@extends Two<string, W>` -> `@extends Base<array<K, V>>` to
`array<string, W>`. The repeated replace() over the extended params is
a no-op on that storage.
…Type()

This reverts commit 609f6c6. Populator::extendType() resolves the
templates of a class in between only where they are nested in a
composed argument. A template passed on as another template
(`HasMany<TRelated>` -> `@extends HasOneOrMany<TRelated, ...>` ->
`@extends Relation<TRelated, ...>`) stays the in-between class's
template in the storage, so an inherited property read from two levels
up gave `TRelated:HasOneOrMany` instead of the object's argument.
@alies-dev
alies-dev merged commit 341b0c2 into vimeo:master Nov 21, 2026
70 checks passed
@alies-dev alies-dev changed the title Fix: bind a composed @extends argument on inherited members Fix (templates): bind a composed @extends argument on inherited members Nov 21, 2026
@alies-dev
alies-dev deleted the alies-dev/composed-extends-argument branch November 21, 2026 23:43
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.

1 participant