Skip to content

fix(php): anonymous classes, imported class names and Eloquent locals decide a call - #3314

Merged
colbymchenry merged 2 commits into
mainfrom
claude/fx2-php-anon-namesakes
Nov 16, 2026
Merged

colbymchenry merged 2 commits into
mainfrom
claude/fx2-php-anon-namesakes

Conversation

@colbymchenry

Copy link
Copy Markdown
Owner

Summary

Three PHP regression classes from FINAL's report, each fixed by a rule that holds for the language:

Stacked on #3313 (C#): the branch carries that commit below its own; merge after it.

Root cause and fix

Anonymous classes. The index holds no node for new class … { … }, so a parent::x() or $this->x() in its body was judged by the named class whose method creates it. framework's test class extends PHPUnit's TestCase, from outside the project, and #3168 made parent:: past an outside parent reach no trait. The resolver now finds the anonymous class around a call (skipping strings, comments and heredocs, past constructor arguments) and looks the method up as PHP does: parent:: in what it extends; the others in its own methods (no node, so no edge), its traits, then what it extends. A parent::__construct($app) above QueueFake is QueueFake::__construct, not the QueueManager one it overrides.

Imported names. When a variable is named after a class ($url → Url), a short name the file imports with use now means the imported class: when the index holds it and it has the method, the call is its, before namesakes the file may also import under an alias. An imported class from outside the project leaves no namesake. A Laravel facade's name is shared by what the facade hands out ($validator = Validator::make(…)), and an imported class without the method says nothing, so both leave the candidates as before.

The class around the call. Inside the class a variable is named after, the guess now takes what the class declares or gets from its traits ($model->fill() inside Model), not what it inherits from a parent class. On framework none of the 76 such calls reached a parent class's method (58 reach the class's own, 6 a trait's), so none change; Drupal's $view->access() in the views area plugin View was the one that walked up to HandlerBase. The chain that sets $view can't be followed soundly: viewStorage is an EntityStorageInterface, whose load() returns an EntityInterface, which declares no getExecutable(). So ViewExecutable::access comes from the receiver's name, at 0.65, as in 1.6.2.

Eloquent locals. The static calls that hand back a model of the class they're called on now include createOrFirst, createQuietly, forceCreateQuietly, incrementOrCreate, findOrNew, findSole, firstOrFail and sole, and Laravel's tap(…) hands back what it wraps.

Validation

A/B against b300cce (main before #3312, a byte-identical speedup), every changed edge read against source:

Repo Moved Added Lost Notes
framework 974cd809 57 42 0 added: 37 calls inside anonymous classes, the 2 Eloquent locals, 3 $artisan->resolveCommands() under use Illuminate\Console\Application as Artisan; moved: 36 $builder calls in a test that imports the query builder as Builder, 6 $cache calls under an aliased cache-contract import (off wrong word guesses), 8 inside anonymous classes, 3 factory locals, 4 onto an imported contract or class; 14 more keep their target at a lower confidence
drupal-core (FINAL's sparse copy) 1 0 5 the 5 lost are false HandlerBase::access edges in files importing Drupal\Core\Url; moved $view->access() onto ViewExecutable::access
laravel 5c82f40 (laravel/framework at an older commit) 57 42 0 the same classes as framework
bookstack 045c6a98 0 0 0 byte-identical
invoiceplane e5e2529 0 0 0 byte-identical
gin, flask (controls) 0 0 0 byte-identical

Against 1.6.2, per class:

Class 1.6.2 main this PR
parent:: and $this-> inside anonymous classes: framework's 108 calls (laravel has the same 108; drupal-core, bookstack and invoiceplane have none) 71 linked, 14 of them wrong 64 linked 102 linked: 1.6.2's other 57 kept, its 14 wrong ones corrected, 31 new
Drupal: FINAL's 10 HandlerBase::access edges none, which is right 10 false 4 false (see below); the 5 in files importing Url are gone, and $view->access() reaches ViewExecutable::access as in 1.6.2
framework: the 5 Eloquent factory locals main had before #3168 (at 4499d00) none none 2 (createOrFirst, tap); the 3 through Orchestra's factory stay unlinked
  • Index CPU on framework, the largest PHP repo here, 10 interleaved rounds (one voided by another agent's job): median 32.89 s against 34.06 s, a per-pair median of +1.2% with a spread (sd) of 4.2%, so within noise. V8 CPU profiles, two interleaved runs of each, put the new code at about 0.25 s of 34 s, 0.7%: reading each file's anonymous classes once (0.2 s, the blanking of strings and comments 0.09 s of it) and its use imports once (0.03 s). The time inside the resolver's per-reference entry point is 10.24 s against 10.21 s.
  • Tests: __tests__/php-anonymous-class-scope.test.ts (red on main in 4 of 7), __tests__/php-receiver-namesakes.test.ts (2 of 4) and a new Eloquent case in __tests__/php-receiver-class-ancestry.test.ts (red on main); the rest are guards that pass on both. One fixture there moved: its $comments->pluck() sat inside class Comments itself, the case the class-around-the-call rule now reads differently; it now sits in a CommentReport class beside it, which keeps what the test is about (a parent read through the name the file imports).
  • CHANGELOG: one line, for anonymous classes, which is new against 1.6.2 (31 calls neither 1.6.2 nor main linked, 14 that 1.6.2 linked wrong). The imported-name and Eloquent changes correct fix(resolution): bind Class.method() to the class itself, not a class whose name contains it #1935's and fix(php): a receiver reaches the class PHP means, through its real parents and traits #3168's unreleased behaviour, so they add none; fix(php): a receiver reaches the class PHP means, through its real parents and traits #3168's condensed sentence already says use imports decide a variable's class.
  • Suite: one full run for the stack, through the gate with CODEGRAPH_KERNEL_EXPECT=1 and 4 workers, on its top commit 9160022d one rebase back, which held this commit unchanged: 888 test files passed and 6 skipped; 11,215 tests passed, 2 expected failures, 88 skipped, none failed. The stack then rebased cleanly onto 22c1104 (perf(resolution): blank string contents by slices, not an array of characters #3312, a byte-identical speedup in strip-comments.ts and jsScopeFile, whose hunks are far from these and which none of this code calls), so under the narrowed rule (a) the rebased commit re-ran its own tests and perf(resolution): blank string contents by slices, not an array of characters #3312's area on bf7aad0: 28 files, 567 passed and 2 expected failures (the PHP, Laravel and Drupal suites, the string-blanking differential, strip-comments and the JS scope suites).

Left out

  • 4 false HandlerBase::access edges stay on the sparse Drupal copy, in files that don't import Url (VersionHistoryController ×3, StringFormatter ×1). There the views plugin is the one class named Url in the checkout, and fix(php): a receiver reaches the class PHP means, through its real parents and traits #3168 lets a variable named after the one class of a name reach what it inherits; with core/lib/Drupal/Core/Url.php present they reach Url::access. Dropping that inheritance removes them, and with them about 40 true edges on drupal-core ($term->id() reaching ContentEntityBase::id through the one Term), and moves 13 $theme->getName() calls to a wrong target, so it isn't done. Reading what a call returns (toUrl()'s @return \Drupal\Core\Url) would settle them; that is a feature of its own.
  • 3 Eloquent locals stay unlinked: $user = UserFactory::new()->create() with Orchestra's factory outside the project, whose model can't be read (1.6.2 never had them either).
  • parent::x() above a parent from outside the project (fix(php): a receiver reaches the class PHP means, through its real parents and traits #3168, deliberate) was recounted on the whole class: drupal-core 106 trait guesses removed, 99 false and 7 right by luck (VocabularyListBuilder, RoleListBuilder, whose outside parent DraggableListBuilder uses DraggableListBuilderTrait); framework 55, 51 false and 4 inside anonymous classes, which this PR now links right. Telling the 7 apart needs the outside parent's traits, so it stays as fix(php): a receiver reaches the class PHP means, through its real parents and traits #3168 left it.

🤖 Generated with Claude Code

colbymchenry and others added 2 commits November 16, 2026 11:39
jellyfin's `path.NormalizePath('/')` on a `string path` reached the overload
taking nothing past `this`. Since PR 3170 a receiver of a keyword type reaches
the extension methods of the name, the first of them was taken, and the
overload check counted the `this` parameter as an argument, so one argument
fit the overload with none.

The `this` parameter is now the receiver of a call through a value, and an
argument only through the class or bare inside it. Of the overloads the class
declares, the call takes the one its argument count fits, then the one whose
`out` or `ref` parameters match what the call writes. When several fit alike,
the overload picked stays if it is one of them; when none fits, or the pick is
none of several, there is no edge. The choice comes before the caller is
ruled out, so `path.NormalizePath(Path.DirectorySeparatorChar)` inside the
overload taking nothing links the one taking a `char`.

Validation, base b300cce against this change:
- jellyfin a46fa455: 7 moved, the 7 NormalizePath calls FINAL found (5 onto
  the `char` overload, 2 onto the `out char` one); 1 added, PathExtensions.cs
  line 186 onto the `char` overload; 1 lost, a self-loop at StreamExtensions.cs
  line 37 onto the `(Stream, Encoding)` overload, where the two one-argument
  overloads differ only by type.
- nopCommerce 51f116ac: 1 lost, a self-loop at MigrationExtensions.cs line 353
  whose call fits three other overloads by count.
- serilog 81af75f3, eShopOnWeb f21d6233, controls gin and flask: byte-identical.
- The class against 1.6.2, jellyfin's NormalizePath calls: 1.6.2 1 right and
  0 wrong, main 2 right and 7 wrong, this change 10 right and 0 wrong.
- Every call of an overloaded extension method on the four repos was counted:
  jellyfin has 23 overload sets and 82 calls linked into one, 73 of them fit
  several overloads alike (`SetPreference(string[])` and `SetPreference<T>(T[])`),
  linked to the first by 1.6.2 and main alike, and they keep it; nopCommerce
  13 sets and 2 such calls, serilog 2 sets and none linked, eShopOnWeb none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… decide a call

Three regression classes FINAL found in PHP, and the rule behind each.

framework's `parent::many($keys)` inside `new class extends ArrayStore { … }`
linked nothing since PR 3168: the index holds no node for an anonymous class,
so `parent::` was judged by the test class around it, whose parent comes from
outside the project. `parent::`, `self::`, `static::` and `$this->` inside an
anonymous class now mean that class: its own methods (which have no node, so
no edge), the traits it uses, then what it extends, looked up as PHP does, so
the call reaches `RetrievesMultipleKeys::many`, ArrayStore's trait, and a
`parent::__construct($app)` above `QueueFake` is QueueFake's own, not the one
it overrides.

Drupal's `$url->access()` under `use Drupal\Core\Url;`, with Url outside a
sparse checkout, went to the views field plugin `Url` and up to
`HandlerBase::access` (PR 1935). A short name a file's `use` imports now means
that class there: when the index holds it and it has the method, the call is
its; an imported class from outside the project leaves no namesake; a Laravel
facade or an imported class lacking the method says nothing, as before. In
`class View`, `$view = $this->viewStorage->load('x')->getExecutable();
$view->access(1)` went to the class's inherited `HandlerBase::access`: inside
the class a variable is named after, the guess now takes what the class
declares or its traits give, not what it inherits from a parent, and the call
reaches `ViewExecutable::access` by its name, as in 1.6.2. The chain's own type
can't be followed soundly: `viewStorage` is an `EntityStorageInterface`, whose
`load()` returns an `EntityInterface`, which has no `getExecutable()`.

PR 3168 dropped two of framework's Eloquent factory locals that REG's main had:
`$model1 = X::createOrFirst(…)` and `$post = tap(X::create(…), fn …)`. The
static calls that hand back a model now include `createOrFirst`, the `Quietly`
variants, `incrementOrCreate`, `findOrNew`, `findSole`, `firstOrFail` and
`sole`, and `tap(…)` hands back what it wraps. The other three losses are
`UserFactory::new()->create()` with Orchestra's factory outside the project,
whose model can't be read, and stay unlinked, as in 1.6.2.

Validation, base b300cce against this change, every changed edge read:
- framework 974cd809: 57 moved, 42 added, 0 lost, 14 with the same target at
  a lower confidence. Added: 37 anonymous-class calls, the 2 Eloquent locals,
  3 `$artisan->resolveCommands()` under `use …\Console\Application as Artisan`.
  Moved: 36 calls on `$builder` in a test that imports the query builder as
  `Builder`, off the Eloquent builder; 6 on `$cache` under an aliased cache
  contract import, off wrong word guesses; 8 inside anonymous classes; 3
  factory locals (`firstOrFail`); 4 onto imported contracts or classes.
- drupal-core (FINAL's sparse copy): 5 lost, all false `HandlerBase::access`
  edges from files that import `Drupal\Core\Url`; 1 moved, `$view->access()`
  onto `ViewExecutable::access` (1.6.2's target).
- laravel 5c82f40 (laravel/framework at an older commit): the same 57 moved,
  42 added and 0 lost.
- bookstack 045c6a98: byte-identical.
- invoiceplane e5e2529, controls gin and flask: byte-identical.
- 4 false `HandlerBase::access` edges stay, from files that import no `Url`
  (the views plugin is the one class of that name in the sparse checkout).
  Dropping inheritance through the one class of a name removes them, and on
  drupal-core 40 true ones too (`$term->id()` reaching ContentEntityBase), and
  moves 13 `$theme->getName()` calls to a wrong target: not done.
- `parent::x()` above a parent from outside the project, PR 3168's deliberate
  change, counted on the whole class: drupal-core 106 trait guesses removed,
  99 false and 7 true by luck; framework 55, 51 false and 4 inside anonymous
  classes, which this change now links right. Kept.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant