Per-file batch fixing for --fix option#5550
Open
mabar wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
Collaborator
|
You've opened the pull request against the latest branch 2.2.x. PHPStan 2.2 is not going to be released for months. If your code is relevant on 2.1.x and you want it to be released sooner, please rebase your pull request and change its target to 2.1.x. |
70b6e85 to
3172df5
Compare
3172df5 to
5f1db78
Compare
Author
|
@phpstan-bot switched to 2.1.x 😘 |
5f1db78 to
df4076b
Compare
df4076b to
865f049
Compare
ondrejmirtes
requested changes
Apr 27, 2026
Member
ondrejmirtes
left a comment
There was a problem hiding this comment.
- How do you handle traits? Those files might be "fixed" from different usages at different points during the analysis.
- I'm torn about a problem I didn't realize. Let's say we gave two errors, both fixable, one ignored and one not. Running
--fixwill fix them both, right? We probably can't solve it becaus we need the one diff per file for performance, and at this point we don't (and should not) be aware what errors are ignored and not.
Member
|
Also, probably, depending on the answer to the previous one, I would not attach the diff to an Error anymore. With one diff per file, it doesn't make sense. So we need a new field for this in the result cache, merging logic in the ResultCacheManager too. |
Member
|
At the same time it'd also be useful to track how many errors applying a fix fixes... |
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.
Each file's fixable errors are now applied in a single AST traversal that produces one diff per file, replacing the previous one-diff-per-error pipeline merged via PhpMerge.
Why
Previously, every
RuleErrorBuilder::fixNode()call produced its own unified diff.Patcher::applyDiffs()then merged N diffs per file usingPhpMerge::mergeHunks(), looping with a full-fileDiffer::diffToArray()per hunk. Cost grows quadratically with hunk count.How
BatchReplacingNodeVisitormatches all of a file's pending fixes viaorigNodeattribute in a singleNodeTraverserpass.$scope->getType()works inside callables) and the result node is memoized.Patcher::applyDiffs()is unchanged in shape but only ever sees one diff per file in the new path.Benchmark
Via new PatcherBenchmarkTest, executed on AMD 9950X3D
Real-world test
1977-file project, using two custom fixable rules with 376 violations across 145 files, some of them with 2-4k LoC. Without cache, analysis ran for 53.1s without measurable change from the previous state. Fix ran for 62.3s adding 9.2s on top of analysis.