Skip to content

Skip writing new class names when no fix added one - #6934

Open
SanderMuller wants to merge 1 commit into
phpstan:2.3.xfrom
SanderMuller:skip-unneeded-class-name-traversal
Open

SanderMuller wants to merge 1 commit into
phpstan:2.3.xfrom
SanderMuller:skip-unneeded-class-name-traversal

Conversation

@SanderMuller

Copy link
Copy Markdown
Contributor

Since 95d9e8b, RuleErrorTransformer runs NameResolver and NewClassNamesVisitor over the whole file for every fixable error. The visitor also collects the names that the file already uses, which walks the file again and parses every PHPDoc in it. In a file with a namespace, it does this twice. A fix that adds no class name needs none of this work.

ReplacingNodeVisitor now keeps the nodes that the fixes returned. The traversal runs only when these nodes contain a new fully qualified name or a changed PHPDoc.

The bug-14996 benchmark has one fix per method, and none of them adds a name. It failed on every 2.3.x push since 95d9e8b, at +35% to +84%. Before that commit it also failed now and then, at +20% to +33%, so this change does not make the job always green.

phpbench mode for bug-14996.php, 4 interleaved rounds, PHP 8.5, macOS arm64:

Commit Time Peak memory
a99d1d2 (before 95d9e8b) 0.77 s
5e92535 (2.3.x) 1.34 s 120 MB
5e92535 + this PR 0.79 s 104 MB

Tests:

  • NewClassNamesVisitorTest checks the new check on its own. Each case fails when its branch of the check is removed.
  • AddNamesRule gets an @add-doc-name tag that changes only the PHPDoc. Without it, every fix in the tests also added a name in PHP code, so no test covered the PHPDoc branch through the transformer.

This PR and #6929 both change NewClassNamesVisitor. They merge without conflicts, and the Fixable tests pass on the merged result.

Fixes that add a class name still run the full traversal for each error. A follow-up can collect the names that the file uses only when a name needs a new use statement.

🤖 Generated with Claude Code

RuleErrorTransformer ran NameResolver and NewClassNamesVisitor over the
whole file for every fixable error. NewClassNamesVisitor also collects
the names the file already uses, twice in a file with a namespace, and
parses every PHPDoc in the file for that. A fix that adds no class name
does not need any of it.

ReplacingNodeVisitor now keeps the nodes that the fixes returned. The
traversal runs only when one of them contains a fully qualified name
without an original node, or a PHPDoc that differs from the original.

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