Skip to content

Resolve names added by fixes only when a fix adds names - #6967

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
skip-name-traversal-without-new-names
Nov 6, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
skip-name-traversal-without-new-names

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Since 95d9e8b (Write class names added by fixes relative to namespace and uses), RuleErrorTransformer::fix() runs php-parser's NameResolver together with NewClassNamesVisitor over the whole file for every fixable error. In each of those traversals, NewClassNamesVisitor::findTakenNames() also walks the file twice and parses every PHPDoc in it. So a file with many fixable errors does whole-file work once per error. tests/bench/data/bug-14996.php reports a fixable #[\Override] error on each of its 100 methods. Those fixes only remove the attribute, so all that traversal changed nothing.

What changed

  • ReplacingNodeVisitor now records the nodes the fixes returned (getNewNodes()).
  • NewClassNamesVisitor::hasNewNames() checks only those nodes, using the same conditions the visitor acts on: a FullyQualified without origNode, or a doc comment that differs from the original node's (or that the original node lacks).
  • RuleErrorTransformer runs the name-resolving traversal only when that check finds something. Every node outside the replaced subtrees is an unchanged clone made by CloningVisitor, so the skipped traversal could not have changed the printed code. The fix output stays the same.

No limits were added. None of the changed classes is shadowed by turbo-ext.

Fixes that do add class names, like adding a missing #[\Override] with checkMissingOverrideMethodAttribute, still do one whole-file traversal per error. This PR doesn't change that.

Measurements

Of the files in tests/bench/data, only bug-14996.php reaches RuleErrorTransformer::fix() (100 calls). No other benchmark makes a fixable error, so no other benchmark is affected.

Instructions counted with cachegrind (bin/phpstan analyse at level 8 with bleedingEdge and OPcache, second run). Net = the file minus an empty file analysed with the same commit:

commit bug-14996.php net instructions
74f4da6 (parent of 95d9e8b) 10.68 G
bcca2b0 (2.3.x before this PR) 17.39 G
this PR 10.88 G (−37.4 %)

Wall clock was measured with hyperfine: 2 warmup runs, then 10 runs per side, with the result cache cleared before each run. The command was bin/phpstan analyse -c <config> --debug --no-progress tests/bench/data/bug-14996.php. tests/bench/phpstan.neon does not exist, so the config includes conf/bleedingEdge.neon with level: 8 and inferPrivatePropertyTypeFromConstructor: true. The machine was an Amp orb (Intel Xeon @ 2.60GHz, 16 vCPUs) running PHP 8.4.27 (NTS) with OPcache, without the turbo extension.

file 2.3.x bcca2b0 (median) this PR (median) speed-up
bug-14996.php 3.137 s (2.954–3.302) 2.362 s (2.268–2.466) 1.33×

Tests run

  • make tests, without and with the turbo extension loaded (PHP_INI_SCAN_DIR): OK, 23472 tests, 106 skipped.
  • make phpstan, make cs: clean.
  • turbo-ext: make build-turbo, tests/smoke.php (ALL OK) and tests/signature-parity.php (OK) with the extension loaded. bin/side-by-side.php pairs all methods. In this environment it reports the generated headers as stale (self vs. the class name in typed static properties), and that happens on 2.3.x without this change too.
  • --fix on a copy of bug-14996.php, and on a file whose fixes add #[\Override] under use Override as Foo, gave byte-identical fixed files before and after this change. The second file still gets #[Foo].

Writing the added class names relative to the namespace and uses traverses the whole file. Skipping it when the fixed nodes contain no added names and no changed PHPDocs makes it cost nothing for fixes that only remove or change code, like removing #[\Override] from every method.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1b134-dc47-7297-bf0e-a2cadcd064cc
@ondrejmirtes
ondrejmirtes merged commit 087443f into 2.3.x Nov 6, 2026
1038 of 1043 checks passed
@ondrejmirtes
ondrejmirtes deleted the skip-name-traversal-without-new-names branch November 6, 2026 16:31
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