Repository navigation
Resolve names added by fixes only when a fix adds names - #6967
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since 95d9e8b (Write class names added by fixes relative to namespace and uses),
RuleErrorTransformer::fix()runs php-parser'sNameResolvertogether withNewClassNamesVisitorover 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.phpreports a fixable#[\Override]error on each of its 100 methods. Those fixes only remove the attribute, so all that traversal changed nothing.What changed
ReplacingNodeVisitornow records the nodes the fixes returned (getNewNodes()).NewClassNamesVisitor::hasNewNames()checks only those nodes, using the same conditions the visitor acts on: aFullyQualifiedwithoutorigNode, or a doc comment that differs from the original node's (or that the original node lacks).RuleErrorTransformerruns the name-resolving traversal only when that check finds something. Every node outside the replaced subtrees is an unchanged clone made byCloningVisitor, 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]withcheckMissingOverrideMethodAttribute, still do one whole-file traversal per error. This PR doesn't change that.Measurements
Of the files in
tests/bench/data, onlybug-14996.phpreachesRuleErrorTransformer::fix()(100 calls). No other benchmark makes a fixable error, so no other benchmark is affected.Instructions counted with cachegrind (
bin/phpstan analyseat level 8 with bleedingEdge and OPcache, second run). Net = the file minus an empty file analysed with the same commit: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.neondoes not exist, so the config includesconf/bleedingEdge.neonwithlevel: 8andinferPrivatePropertyTypeFromConstructor: 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.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.make build-turbo,tests/smoke.php(ALL OK) andtests/signature-parity.php(OK) with the extension loaded.bin/side-by-side.phppairs all methods. In this environment it reports the generated headers as stale (selfvs. the class name in typed static properties), and that happens on 2.3.x without this change too.--fixon a copy of bug-14996.php, and on a file whose fixes add#[\Override]underuse Override as Foo, gave byte-identical fixed files before and after this change. The second file still gets#[Foo].