Skip to content

Fix static kept on the union of $this and another object of its class - #14078

Merged
alies-dev merged 2 commits into
vimeo:masterfrom
shu-chen-dot-ca:fix-13521-static-union
Nov 20, 2026
Merged

alies-dev merged 2 commits into
vimeo:masterfrom
shu-chen-dot-ca:fix-13521-static-union

Conversation

@shu-chen-dot-ca

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

Copy link
Copy Markdown
Contributor

The union of $this (OB&static) and another OB keeps &static when $this comes first, so returning it as static is not reported.

Before

<?php
class A {
    public function pick(A $other, bool $f): void {
        $x = $f ? $this : $other;   // A&static
        $y = $f ? $other : $this;   // A
        $z = $f ? $this : new A();  // A&static
    }

    /** @return static */
    public function pick2(A $other, bool $f) {
        return $f ? $this : $other; // no issue
    }
}

After

        $x = $f ? $this : $other;   // A
        $y = $f ? $other : $this;   // A
        $z = $f ? $this : new A();  // A
    public function pick2(A $other, bool $f) { // MoreSpecificReturnType
        return $f ? $this : $other; // LessSpecificReturnStatement

A @return static method returning such a union now reports both MoreSpecificReturnType and LessSpecificReturnStatement (as for any declared return type narrower than what is returned). Two static objects of the class still combine to static ($f ? $this : $this->me()).

Before After
before after

Fixes #13521

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: increased

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 693 70.2 $\color{gray}{\textsf{-0.6\%}}$ 26 022 53 315 572
✅ symfony 3 613 170.7 $\color{gray}{\textsf{+4.6\%}}$ 50 664 $\color{red}{\textsf{+1}}$ $\color{gray}{\textsf{-0}}$ 99 065 $\color{red}{\textsf{+3}}$ 1 157
✅ doctrine-orm 392 16.1 $\color{gray}{\textsf{-0.7\%}}$ 3 525 $\color{red}{\textsf{+2}}$ $\color{green}{\textsf{-2}}$ 6 259 0
✅ psl 632 20.6 $\color{gray}{\textsf{+4.4\%}}$ 1 331 5 559 2
✅ phpunit 471 14.7 $\color{gray}{\textsf{-0.9\%}}$ 1 604 2 391 17
✅ php-parser 232 9.5 $\color{gray}{\textsf{+1.0\%}}$ 17 129 21 025 0
✅ tempest 889 26.6 $\color{gray}{\textsf{+0.7\%}}$ 4 933 10 539 97
✅ amphp-amp 61 2.2 $\color{gray}{\textsf{-0.9\%}}$ 29 49 0
✅ webmozarts-assert 284 5.3 $\color{gray}{\textsf{+1.3\%}}$ 577 2 722 0
✅ guzzle-promises 55 2.3 $\color{gray}{\textsf{+3.1\%}}$ 161 438 2
✅ monolog 153 6.2 $\color{gray}{\textsf{+1.6\%}}$ 907 2 422 10
✅ drupal 4 751 122.3 $\color{gray}{\textsf{-0.7\%}}$ 98 166 171 256 593
Total 13 226 466.8 $\color{gray}{\textsf{+1.6\%}}$ 205 048 $\color{red}{\textsf{+3}}$ $\color{green}{\textsf{-2}}$ 375 040 $\color{red}{\textsf{+3}}$ 2 450
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 +1 −0

Memory 3 613 → 3 613 MB (<0.1 %) · Wall 163.2 → 170.7 s (+4.6 %)

Weighted 99 062 → 99 065 (+3, <0.01 %): L3 +1

Issue type added removed
MoreSpecificReturnType 1 0

Added (1)

  • src/Symfony/Component/HttpFoundation/Request.php:2187 MoreSpecificReturnType (error): The declared return type 'Symfony\Component\HttpFoundation\Request&static' for Symfony\Component\HttpFoundation\Request::createRequestFromFactory is more specific than the inferred return type 'Symfony\Component\H…
✅ doctrine-orm: issues +2 −2

Memory 392 → 392 MB (+0.0 %) · Wall 16.2 → 16.1 s (−0.7 %)

Issue type added removed
LessSpecificReturnStatement 1 1
MoreSpecificReturnType 1 1

Added (2)

  • src/Mapping/AssociationMapping.php:120 MoreSpecificReturnType (error): The declared return type 'Doctrine\ORM\Mapping\AssociationMapping&static' for Doctrine\ORM\Mapping\AssociationMapping::fromMappingArray is more specific than the inferred return type 'Doctrine\ORM\Mapping\AssociationMapping|Doct…
  • src/Mapping/AssociationMapping.php:158 LessSpecificReturnStatement (error): The type 'Doctrine\ORM\Mapping\AssociationMapping|Doctrine\ORM\Mapping\AssociationMapping&Doctrine\ORM\Mapping\ManyToManyAssociationMapping|Doctrine\ORM\Mapping\ManyToManyAssociationMapping&Doctrine\ORM\Mapping\AssociationM…

Removed (2)

  • src/Mapping/AssociationMapping.php:120 MoreSpecificReturnType (error): The declared return type 'Doctrine\ORM\Mapping\AssociationMapping&static' for Doctrine\ORM\Mapping\AssociationMapping::fromMappingArray is more specific than the inferred return type 'Doctrine\ORM\Mapping\AssociationMapping&Doct…
  • src/Mapping/AssociationMapping.php:158 LessSpecificReturnStatement (error): The type 'Doctrine\ORM\Mapping\AssociationMapping&Doctrine\ORM\Mapping\ManyToManyAssociationMapping|Doctrine\ORM\Mapping\AssociationMapping&static|Doctrine\ORM\Mapping\ManyToManyAssociationMapping&Doctrine\ORM\Mapping\Assoc…

head 412ccdb · base ad06cb6 (merge-base) · workflow run

@alies-dev alies-dev 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.

🤖 The bug is real on current master ($f ? $this : $other is A&static, $f ? $other : $this is A), and the fix gives A for both orders while $f ? $this : $this->me() keeps static. Different-class pairs (A&static|B, A&static|I) stay unions on master and on the PR, so this same-key branch is the only place where static is collapsed wrongly.

No blocking findings.

Recommendations (non-blocking)

  • TypeCombiner already tracks $combination->object_static per class name (set for every TNamedObject, and read back for generic objects via setIsStatic($combination->object_static[...])), but the plain named_object_types entries are emitted as whichever atomic was stored first. Applying object_static when named_object_types are merged into value_types (the same way the generic branch does) would make this order-independent without rewriting the stored atomic in the middle of the scrape, and would keep one rule for generic and non-generic objects.
  • The perf run adds one MoreSpecificReturnType for Request::createRequestFromFactory (symfony). It looks like a true positive (the method can return a plain Request after instanceof self), but note that a @return static function returning $f ? $this : $other now reports both MoreSpecificReturnType and LessSpecificReturnStatement (reproduced), which is worth a line in the description.

@shu-chen-dot-ca

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Moved the fix to where named_object_types are merged into value_types: each one now gets object_static / object_static_resolved of its class, like the generic branch, and the scrape no longer rewrites the stored atomic. Same results on the issue example and the PR's tests, and the existing &static assertion cases (ThisOut, MethodCall, Mixin, the preventDoubleStaticResolution* ones in ClassTest, ...) still pass. Also noted the two return issues in the description.

@alies-dev alies-dev added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 20, 2026
@alies-dev
alies-dev merged commit 0860336 into vimeo:master Nov 20, 2026
70 of 72 checks passed
@shu-chen-dot-ca
shu-chen-dot-ca deleted the fix-13521-static-union branch November 20, 2026 00:15
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.

Union of $this (X&static) and another X keeps &static depending on operand order

2 participants