Skip to content

Expand the params of a final class's method with its static as the class itself - #14203

Merged
orklah merged 1 commit into
vimeo:masterfrom
shu-chen-dot-ca:fix-13519-final-static-closure
Nov 23, 2026
Merged

orklah merged 1 commit into
vimeo:masterfrom
shu-chen-dot-ca:fix-13519-final-static-closure

Conversation

@shu-chen-dot-ca

@shu-chen-dot-ca shu-chen-dot-ca commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

A final class or enum forwarding its own Closure(static) (or callable(list<static>)) param to another method of $this, or calling it with $this, gets a false ArgumentTypeCoercion.

Before

<?php
trait HasConditions {
    /** @param Closure(static): mixed $callback */
    public function when(bool $condition, Closure $callback): static {
        if ($condition) {
            $callback($this);
        }
        return $this;
    }

    /** @param Closure(static): mixed $callback */
    public function unless(bool $condition, Closure $callback): static {
        return $this->when(!$condition, $callback); // ArgumentTypeCoercion: Argument 2 of Query::when expects Closure(Query):mixed, but parent type Closure(Query&static):mixed provided
    }
}

final class Query {
    use HasConditions;
}

After

        return $this->when(!$condition, $callback); // no issue

The params of a method are now expanded with the class's finality, so static in them is the class itself in a final class, as $this already is. Static of a non-final class is still checked (nonFinalClassInstancePassedToItsStaticParam).

Fixes #13519

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: decreased

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 749 93.9 $\color{gray}{\textsf{+0.3\%}}$ 25 907 53 460 682
✅ symfony 3 599 237.9 50 610 98 403 1 528
✅ doctrine-orm 380 14.4 $\color{gray}{\textsf{-5.4\%}}$ 3 481 6 143 0
✅ psl 617 17.9 $\color{gray}{\textsf{-0.9\%}}$ 1 375 5 615 1
✅ phpunit 455 14.3 $\color{gray}{\textsf{+0.6\%}}$ 1 731 3 410 18
✅ php-parser 226 9.4 $\color{gray}{\textsf{+0.2\%}}$ 17 154 21 245 0
✅ tempest 895 26.8 $\color{gray}{\textsf{+0.5\%}}$ 5 040 $\color{red}{\textsf{+10}}$ $\color{green}{\textsf{-17}}$ 10 507 $\color{green}{\textsf{-21}}$ 252
✅ amphp-amp 45 1.5 29 49 0
✅ webmozarts-assert 273 4.7 $\color{gray}{\textsf{+0.2\%}}$ 577 2 720 0
✅ guzzle-promises 44 1.6 155 434 2
✅ monolog 135 5.6 $\color{gray}{\textsf{-2.1\%}}$ 913 2 479 10
✅ drupal 4 842 262.3 $\color{gray}{\textsf{+0.4\%}}$ 97 591 169 293 583
Total 13 260 690.2 204 563 $\color{red}{\textsf{+10}}$ $\color{green}{\textsf{-17}}$ 373 758 $\color{green}{\textsf{-21}}$ 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.
✅ tempest: issues +10 −17

Memory 895 → 895 MB (<0.1 %) · Wall 26.7 → 26.8 s (+0.5 %)

Weighted 10 528 → 10 507 (−21, −0.20 %): L3 −7

Issue type added removed
ArgumentTypeCoercion 0 7
PossiblyInvalidArgument 10 10

Added (10)

  • packages/support/src/Arr/ManipulatesArray.php:300 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\diff expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\ImmutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Support\A…
  • packages/support/src/Arr/ManipulatesArray.php:300 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\diff expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\MutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Support\Arr…
  • packages/support/src/Arr/ManipulatesArray.php:312 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\diff_keys expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\ImmutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Supp…
  • packages/support/src/Arr/ManipulatesArray.php:312 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\diff_keys expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\MutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Suppor…
  • packages/support/src/Arr/ManipulatesArray.php:324 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\intersect expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\ImmutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Supp…
  • packages/support/src/Arr/ManipulatesArray.php:324 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\intersect expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\MutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Suppor…
  • packages/support/src/Arr/ManipulatesArray.php:336 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\intersect_keys expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\ImmutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest…
  • packages/support/src/Arr/ManipulatesArray.php:336 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\intersect_keys expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\MutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\S…
  • packages/support/src/Arr/ManipulatesArray.php:348 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\merge expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\ImmutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Support\…
  • packages/support/src/Arr/ManipulatesArray.php:348 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\merge expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\MutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Support\Ar…

Removed (17)

  • packages/support/src/Arr/ManipulatesArray.php:300 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\diff expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\ImmutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Support\A…
  • packages/support/src/Arr/ManipulatesArray.php:300 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\diff expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\MutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Support\Arr…
  • packages/support/src/Arr/ManipulatesArray.php:312 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\diff_keys expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\ImmutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Supp…
  • packages/support/src/Arr/ManipulatesArray.php:312 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\diff_keys expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\MutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Suppor…
  • packages/support/src/Arr/ManipulatesArray.php:324 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\intersect expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\ImmutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Supp…
  • packages/support/src/Arr/ManipulatesArray.php:324 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\intersect expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\MutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Suppor…
  • packages/support/src/Arr/ManipulatesArray.php:336 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\intersect_keys expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\ImmutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest…
  • packages/support/src/Arr/ManipulatesArray.php:336 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\intersect_keys expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\MutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\S…
  • packages/support/src/Arr/ManipulatesArray.php:348 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\merge expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\ImmutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Support\…
  • packages/support/src/Arr/ManipulatesArray.php:348 PossiblyInvalidArgument (error): Argument 2 of Tempest\Support\Arr\merge expects array<array-key, mixed>, but possibly different type (Tempest\Support\Arr\MutableArray<TKey:Tempest\Support\Arr\ManipulatesArray as array-key, TValue:Tempest\Support\Ar…
  • … and 7 more

head 05b980e · base 2da2040 (merge-base) · workflow run

@shu-chen-dot-ca

Copy link
Copy Markdown
Contributor Author

Rebased onto master (78d192b). The only conflict was in tests/CallableTest.php, where master's new invalid cases and this PR's nonFinalClassInstancePassedToItsStaticParam were both added at the top of providerInvalidCodeParse; both are kept.

@shu-chen-dot-ca
shu-chen-dot-ca force-pushed the fix-13519-final-static-closure branch from 991c3dd to 95a179e Compare November 21, 2026 01:30

@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 for this. The tests are good and the false positive is real, but the fix is at the wrong level: the
Query&static comes from FunctionLikeAnalyzer, which expands every param type with final: false
(TypeExpander::expandUnion(..., $param_type->hasConditional(), false, true, false, true)), while $this of a
final class is already a plain Query. Teaching the comparators that X&static equals X then leaks into the
PHP signature checks, which is why MethodComparator needs replacesStaticWithItsClass() to undo it before 8.5.

Passing the class's finality there instead:

$param_type = TypeExpander::expandUnion(
    $codebase,
    $param_type,
    $context->self,
    $context->self,
    $this->getParentFQCLN(),
    true,
    $param_type->hasConditional(),
    $context->self !== null
        && $codebase->classlike_storage_provider->has($context->self)
        && $codebase->classlike_storage_provider->get($context->self)->final,
    true,
    false,
    true,
);

makes finalClassForwardsItsClosureOfStaticParam, the generic and enum variants and a callable($this) param
(the Tempest Timelock case) pass locally, without touching the comparators. Could you move the fix there, drop
the comparator changes, isSealedClass() and the MethodComparator guard, and keep your tests? The Config
baseline entry is a static property, handled separately in #14351.

@shu-chen-dot-ca shu-chen-dot-ca changed the title Treat static of a final class or enum as the class itself in object comparisons Expand the params of a final class's method with its static as the class itself Nov 21, 2026
@shu-chen-dot-ca

Copy link
Copy Markdown
Contributor Author

Rebased onto master (2da2040) and squashed into one commit. The conflict was with 80bc820, which now expands static from $this and already covers a final class's own methods; I kept its condition and added the class-finality check as the final argument, so methods coming from a trait (where $this stays X&static) are covered too. The tests are now the trait cases (final, generic final, @final, enum), plus the final/non-final instance cases.

@shu-chen-dot-ca
shu-chen-dot-ca force-pushed the fix-13519-final-static-closure branch from 795261b to 05b980e Compare November 21, 2026 18:59
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 23, 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.

@orklah
orklah merged commit 810b501 into vimeo:master Nov 23, 2026
72 of 73 checks passed
@shu-chen-dot-ca
shu-chen-dot-ca deleted the fix-13519-final-static-closure branch November 23, 2026 16:29
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.

False ArgumentTypeCoercion "expects Closure(X), but parent type Closure(X&static)" for a final class forwarding its Closure(static) param

2 participants