Skip to content

Fix assertions on $this of calls with no object, and make call-site InvalidDocblock suppressible - #14678

Merged
orklah merged 2 commits into
vimeo:masterfrom
orklah:assertion-invalid-docblock-callsite
Nov 23, 2026
Merged

orklah merged 2 commits into
vimeo:masterfrom
orklah:assertion-invalid-docblock-callsite

Conversation

@orklah

@orklah orklah commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Assertions on $this of a call with no object

An assertion on $this or $this->prop of a constructor, of a static method or of a call on an object with no id is reported as InvalidDocblock, or applied to the caller's own $this.

Before

<?php
class Holder {
    public ?int $v = null;

    /** @psalm-assert int $this->v */
    public function __construct(int $v) {
        $this->v = $v;
    }
}

$holder = new Holder(1); // InvalidDocblock: Variable $this is not an argument so cannot be asserted

class Caller {
    public ?int $v = null;

    public function test(): ?int {
        $this->v = null;
        new Holder(1); // TypeDoesNotContainType: Type null for $this->v is never int
        $v = $this->v;
        return $v;
    }
}

After

$holder = new Holder(1); // no issue
        new Holder(1); // no issue

Assertions on $this of parent:: and self:: calls

Before

<?php
interface Marker {}

class Holder {
    /** @psalm-assert-if-true Marker $this */
    public function isMarker(): bool {
        return $this instanceof Marker;
    }
}

class Child extends Holder {
    public function test(): void {
        if (self::isMarker()) {
            /** @psalm-check-type $this = Child&Marker */; // CheckType: does not match $this = Child&static
        }
        if (!parent::isMarker()) { // InvalidDocblock: Assertion of $this can be done only on method of a class
            return;
        }
    }
}

After

            /** @psalm-check-type $this = Child&Marker */; // no issue
        if (!parent::isMarker()) { // no issue

Static call on one of several classes

A static call on a union of classes takes the conditional assertions of one of them, even when another makes none.

Before

<?php
class Other {
    /** @psalm-assert-if-true int $x */
    public static function isInt(mixed $x): bool {
        return is_int($x);
    }
}

class Other2 {
    public static function isInt(mixed $_x): bool {
        return true;
    }
}

function test(bool $flag, mixed $x): void {
    $static = $flag ? Other2::class : Other::class;
    if ($static::isInt($x)) {
        /** @psalm-check-type-exact $x = mixed */; // CheckType: does not match $x = int
    }
}

After

        /** @psalm-check-type-exact $x = mixed */; // no issue

@psalm-suppress InvalidDocblock ignored

An InvalidDocblock reported at a call for the assertions of the called function, or for a malformed inline @var, can't be suppressed.

Before

<?php
/** @psalm-assert-if-true int $nope */
function isTrue(mixed $_m): bool {
    return true;
}

function test(mixed $m): void {
    /** @psalm-suppress InvalidDocblock */
    if (isTrue($m)) {} // InvalidDocblock: Assertion of variable "$nope" cannot be recognized
}

/** @psalm-suppress InvalidDocblock */
function assign(): int {
    /** @var array< */
    $a = 1; // InvalidDocblock: array< is not a valid type
    return $a;
}

After

    if (isTrue($m)) {} // no issue
    $a = 1; // no issue

Fixes #11662

🤖 Generated with Claude Code

…nvalidDocblock suppressible

An assertion on `$this` or `$this->prop` is only translated to the object
for `$obj->method()`. For `new`, functions, static calls and calls on an
object with no id it fell back to the caller's `$this`, or was reported as
InvalidDocblock. `parent::`/`self::` calls now assert on `$this`, and the
conditional assertions of a `self::` call forwarded to `$this` are kept.

InvalidDocblock reported at a call for the called function's assertions,
and for malformed inline docblocks, now honours `@psalm-suppress`.

Fixes vimeo#11662

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 23, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T08:14:27.930387Z dfcc361 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit dfcc361. Configure here.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dfcc36104c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 768 58.8 $\color{gray}{\textsf{+0.2\%}}$ 25 691 52 994 682 0
✅ symfony 3 596 281.8 $\color{gray}{\textsf{+3.6\%}}$ 50 604 98 386 1 528 0
✅ doctrine-orm 380 12.5 $\color{gray}{\textsf{-0.6\%}}$ 3 476 6 143 0 0
✅ psl 617 17.1 $\color{gray}{\textsf{+4.7\%}}$ 1 375 5 597 1 0
✅ phpunit 455 20.2 $\color{gray}{\textsf{+0.6\%}}$ 1 731 3 410 18 0
✅ php-parser 226 13.6 $\color{gray}{\textsf{-0.5\%}}$ 17 118 21 193 0 0
✅ tempest 895 37.3 $\color{gray}{\textsf{+1.4\%}}$ 5 047 10 528 252 0
✅ amphp-amp 45 1.2 $\color{gray}{\textsf{+2.5\%}}$ 29 49 0 0
✅ webmozarts-assert 272 3.8 $\color{gray}{\textsf{-0.5\%}}$ 575 2 714 0 0
✅ guzzle-promises 44 1.3 $\color{gray}{\textsf{+0.8\%}}$ 155 434 2 0
✅ monolog 135 4.8 $\color{gray}{\textsf{+4.6\%}}$ 913 2 479 10 0
✅ drupal 4 839 240.9 $\color{gray}{\textsf{+0.8\%}}$ 97 575 169 202 583 0
✅ wordpress 2 038 112.9 $\color{gray}{\textsf{+0.6\%}}$ 142 681 676 628 461 1
Total 15 312 806.1 $\color{gray}{\textsf{+1.8\%}}$ 346 970 1 049 757 3 537 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.
  • 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 74106cf · base 40f73cb (merge-base) · workflow run

… static targets

An instance method called statically asserts on `$this` only when it is a
method of the caller's class or of an ancestor. A static call on several
possible classes keeps only the conditional assertions they all make.
MissingDocblockType in malformed inline docblocks honours @psalm-suppress
like InvalidDocblock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@orklah
orklah merged commit dd67b34 into vimeo:master Nov 23, 2026
71 of 72 checks passed
@orklah
orklah deleted the assertion-invalid-docblock-callsite branch November 23, 2026 09:31
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.

Impossible to suppress InvalidDocblock from library code

1 participant