Skip to content

fix(scanner): queue the class of @psalm-scope-this for scanning - #14565

Merged
orklah merged 3 commits into
vimeo:masterfrom
jirkace:fix/11661-scope-this-autoload
Nov 24, 2026
Merged

orklah merged 3 commits into
vimeo:masterfrom
jirkace:fix/11661-scope-this-autoload

Conversation

@jirkace

@jirkace jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

A class named only by @psalm-scope-this (not otherwise referenced in the project) is never loaded, unlike the class of a @var.

Before

<?php
/** @psalm-scope-this Psalm\Tests\LanguageServer\MockProtocolStream */
?>
<h1><?php $this->write(new Psalm\Internal\LanguageServer\Message(new AdvancedJsonRpc\Notification("a"))); ?></h1>
<!-- UndefinedDocblockClass: Scope class Psalm\Tests\LanguageServer\MockProtocolStream does not exist -->

After: no issue; the class is queued for scanning like the class of a @var (ScopeTest::psalmScopeThisInTemplateIsTheOnlyReferenceToTheClass).

Fixes #11661

…#11661

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • The scanner now queues the class of a @psalm-scope-this tag for scanning (and records it as referenced by the file), as it does for the types of @var tags, so a class only reachable through the autoloader is found. Before, UndefinedDocblockClass: Scope class ... does not exist was reported unless something else referenced the class (the existing psalmScopeThisInTemplate cases needed a new Exception() "to trick Psalm's scanner").
  • Also checked from the CLI with a project whose scope class lives outside projectFiles and is loaded by an spl_autoload_register() autoloader: master reports UndefinedDocblockClass, InvalidScope and MixedArgument, the branch nothing.
  • Verified: full self-analysis (only the two local-environment MixedArgument in PoolEventLoopDriverTest, same as master); the other psalm-scope-this cases of ScopeTest pass. PHPUnit suite: not run locally, CI runs it.

@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 2 potential issues.

Fix All in Cursor

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

Reviewed by Cursor Bugbot for commit 63ee866. Configure here.

@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 Dead code
✅ laravel 1 749 95.5 $\color{gray}{\textsf{-2.6\%}}$ 25 907 53 460 682 0
✅ symfony 3 599 260.7 $\color{gray}{\textsf{+0.2\%}}$ 50 610 98 403 1 528 0
✅ doctrine-orm 380 15.2 $\color{gray}{\textsf{+0.9\%}}$ 3 481 6 143 0 0
✅ psl 617 20.0 $\color{gray}{\textsf{+0.2\%}}$ 1 375 5 615 1 0
✅ phpunit 455 14.2 $\color{gray}{\textsf{-0.4\%}}$ 1 731 3 410 18 0
✅ php-parser 226 9.3 $\color{gray}{\textsf{-0.1\%}}$ 17 154 21 245 0 0
✅ tempest 895 26.6 $\color{gray}{\textsf{-0.1\%}}$ 5 047 10 528 252 0
✅ amphp-amp 45 1.2 $\color{gray}{\textsf{+0.9\%}}$ 29 49 0 0
✅ webmozarts-assert 273 3.6 $\color{gray}{\textsf{+2.3\%}}$ 577 2 720 0 0
✅ guzzle-promises 44 1.2 155 434 2 0
✅ monolog 135 4.3 $\color{gray}{\textsf{+0.2\%}}$ 913 2 479 10 0
✅ drupal 4 842 140.7 $\color{gray}{\textsf{+1.3\%}}$ 97 591 169 293 583 0
✅ wordpress 2 039 273.1 $\color{gray}{\textsf{-2.3\%}}$ 142 704 676 684 461 1
Total 15 299 865.5 $\color{gray}{\textsf{-0.7\%}}$ 347 274 1 050 463 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 2a26cb6 · base 680d78d (merge-base) · workflow run

…cblock vimeo#11661

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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! The fix is in the right place, and it also fixes a crash: with an aliased class (use RuntimeException as E; /** @psalm-scope-this E */ ?> <?= $this->getMessage() ?>), master currently crashes with Could not get class storage for runtimeexception.

One regression, though: a tag whose value resolves to an empty name now crashes the scanner. For example:

<?php
/** @psalm-scope-this \ */
?>
<h1><?= 1 ?></h1>

On master this reports UndefinedDocblockClass: Scope class does not exist. On this branch Psalm crashes with Uninitialized string offset 0 in Scanner::queueClassLikeForScanning(), because Type::getFQCLNFromString('\') returns ''. Since the scanner also runs on vendor files, such a docblock in a dependency would now take the whole run down. Could you check the resolved name instead of (or as well as) the trimmed tag, e.g.

$scope_fqcn = $scope_class !== '' ? Type::getFQCLNFromString($scope_class, $this->aliases) : '';
if ($scope_fqcn !== '') {

and add a test for it, like testPsalmScopeThisWithoutAClassInAFileThatIsOnlyScanned but with @psalm-scope-this \?

…othing vimeo#11661

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 24, 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.

Thanks @jirkace!

@orklah
orklah merged commit 33023c6 into vimeo:master Nov 24, 2026
116 of 118 checks passed
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.

@psalm-scope-this does not autoload unlike @var leading to UndefinedClass

2 participants