Fix phpcbf conflict on anonymous class bodies in ConsistentIndent#68
Merged
Merged
Conversation
PhpCollective.WhiteSpace.ConsistentIndent and the bundled
Generic.WhiteSpace.ScopeIndent disagreed on the expected indent of an
anonymous class body when the class is passed as an argument to a
multi-line function call, e.g.:
return new Service(
new class ($payload) extends Base {
public function foo(): void {}
},
);
The class scope opens inside an unclosed parenthesis, so the body carries
one continuation indent level. getExpectedIndent() only counts scope
conditions and misses it, so ConsistentIndent dedented the body while
ScopeIndent re-indented it. phpcbf then oscillated until it hit the loop
limit and reported "FAILED TO FIX" (exit 7), leaving the file unfixable.
Defer to ScopeIndent for anonymous class bodies, the same way closures
and arrays are already skipped, by adding an isInsideAnonClass() guard.
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.
Summary
PhpCollective.WhiteSpace.ConsistentIndentand the bundledGeneric.WhiteSpace.ScopeIndentdisagree on the expected indentation of an anonymous class body when the anonymous class is passed as an argument to a multi-line function call:The class scope is opened inside an unclosed parenthesis, so the body carries one extra continuation indent level.
getExpectedIndent()only counts scopeconditions, so it misses that level and dedents the body, whileScopeIndentre-indents it.phpcbfthen oscillates between the two until it hits the loop limit and reportsFAILED TO FIX(exit 7) — the file becomes unfixable.Fix
Defer to
ScopeIndentfor anonymous class bodies — the same way closures, arrays and switch/case blocks are already skipped — via a newisInsideAnonClass()guard.Before:
phpcbfran the full 50 loops (~75s) and reportedFAILED TO FIX(a negative net fix count, the tell-tale sign of two fixers fighting).After: the file converges in one pass (~8s), no conflict.
Tests
Added a correctly-indented anonymous-class-as-argument case to the
ConsistentIndentbefore/after fixtures. Before this change the fixer wrongly dedented the body (fixer test red); now it is left untouched and no longer flagged.