Skip to content

fix(references): --find-references-to finds the calls of a function - #14648

Merged
alies-dev merged 2 commits into
vimeo:masterfrom
jirkace:fix/14197-find-references-to-function
Nov 22, 2026
Merged

alies-dev merged 2 commits into
vimeo:masterfrom
jirkace:fix/14197-find-references-to-function

Conversation

@jirkace

@jirkace jirkace commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

--find-references-to (Codebase::findReferencesToSymbol()) finds no reference to a function, while it finds those to a method.

Before

<?php
namespace App;

function helper(): int {
    return 1;
}

final class H {
    public function a(): int {
        return helper();
    }
}

helper();
$f = \App\helper(...);
$codebase->findReferencesToSymbol('App\helper'); // []

After

$codebase->findReferencesToSymbol('App\helper'); // 10:36:helper, 14:21:helper, 15:26:\App\helper
$codebase->findReferencesToSymbol('\App\helper'); // the same

Fixes #14197

…imeo#14197

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

jirkace commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • When locations are collected (--find-references-to, Codebase::collectLocations()), a call of a function the code declares (one with a storage location, so not a builtin) is recorded as a reference to it in the code use graph, at the function name, with the call's context and whether its return value is used, as Methods::methodExists() does for methods.
  • Codebase::findReferencesToSymbol() with a name without :: now returns the references to the function of that name as well as those to the class-like (a leading \ is ignored for the function).
  • Nothing is recorded when locations are not collected, so a normal analysis and its unused-code detection are unchanged.
  • Not covered (Resolve callable strings in one place #14198): calls through callable strings (array_map('App\helper', ...), call_user_func).

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 768 95.2 $\color{gray}{\textsf{+0.7\%}}$ 25 691 52 996 682 0
✅ symfony 3 596 221.7 $\color{gray}{\textsf{+2.9\%}}$ 50 610 98 403 1 528 0
✅ doctrine-orm 380 15.2 $\color{gray}{\textsf{-0.3\%}}$ 3 481 6 143 0 0
✅ psl 617 20.0 $\color{gray}{\textsf{-0.5\%}}$ 1 375 5 615 1 0
✅ phpunit 455 18.4 $\color{gray}{\textsf{-0.4\%}}$ 1 731 3 410 18 0
✅ php-parser 226 12.3 $\color{gray}{\textsf{+0.1\%}}$ 17 154 21 245 0 0
✅ tempest 895 32.5 $\color{gray}{\textsf{+0.1\%}}$ 5 047 10 528 252 0
✅ amphp-amp 45 1.2 $\color{gray}{\textsf{+3.4\%}}$ 29 49 0 0
✅ webmozarts-assert 272 3.6 $\color{gray}{\textsf{+1.1\%}}$ 577 2 720 0 0
✅ guzzle-promises 44 1.2 $\color{gray}{\textsf{-0.8\%}}$ 155 434 2 0
✅ monolog 135 4.3 $\color{gray}{\textsf{-1.1\%}}$ 913 2 479 10 0
✅ drupal 4 839 194.8 $\color{gray}{\textsf{-5.8\%}}$ 97 591 169 293 583 0
✅ wordpress 2 038 288.1 $\color{gray}{\textsf{+4.4\%}}$ 142 704 676 684 461 1
Total 15 311 908.6 $\color{gray}{\textsf{+0.8\%}}$ 347 058 1 049 999 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 21804c4 · base 9028ade (merge-base) · workflow run

@alies-dev alies-dev changed the title fix(references): --find-references-to finds the calls of a function fix(references): --find-references-to finds the calls of a function Nov 22, 2026
@alies-dev
alies-dev self-requested a review November 22, 2026 11:12
@alies-dev alies-dev added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 22, 2026

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

Thanks, the fix looks correct. Both new FileReferenceTest cases fail on master and pass here. Since the reference is recorded after callable strings are resolved, call_user_func('App\helper'), array_map('App\helper', …), $s = 'App\helper'; $s(); and \App\helper(...) are found as well (part of #14198). It is also behind collect_locations, so normal, diff and unused-code runs are unaffected, and free functions are already roots in CodeUseGraph::isRoot().

A few small things:

  1. Leading \ only works for functions. The ltrim is applied only to the function lookup, so --find-references-to='\App\H' and '\App\H::a' still return nothing, while App\H finds the new H() and use App\H; lines. Moving $symbol = ltrim($symbol, '\\'); to the top of findReferencesToSymbol() would fix every symbol kind, and the inline ltrim could go.
  2. CLI help: Psalm.php still documents --find-references-to=[class|method|property]; it should mention function.
  3. Comment wording in FunctionCallAnalyzer: "where the functions of the code are called is found as where methods are" is hard to parse. Maybe // record the call sites of user functions, as Methods::methodExists() does for methods.

Not blocking, could be a follow-up: use function App\helper; lines aren't reported. For classes, CanAlias records use App\H; in use_referencing_locations, but the TYPE_FUNCTION branch only fills aliased_functions, so a rename based on this list would miss the import.

Also mention functions in the --find-references-to help.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alies-dev
alies-dev merged commit 7064835 into vimeo:master Nov 22, 2026
71 of 72 checks passed
jirkace added a commit to jirkace/psalm that referenced this pull request Nov 22, 2026
…meo#14199

vimeo#14648 records the call of a user function only when locations are
collected, and this PR recorded it a second time at another place for the
language server. Keep the one of vimeo#14648, with the name location and the
used return value, and record it in every run, as for methods.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

--find-references-to finds no references to a free function

2 participants