Fix DocComment fixer emitting tab indentation - #79
Merged
Conversation
Two fixes built their indentation as str_repeat("\t", column - 1). That is
wrong twice over: the standard enables Generic.WhiteSpace.DisallowTabIndent,
so the fixer emitted indentation its own ruleset then flags, and it used a
column offset as a repeat count, so a doc block indented by four spaces got
four tabs rather than one level.
Both now use the getIndentationWhitespace() helper already used elsewhere for
this, which reads the actual leading whitespace of the line.
The fixture added with the smoke tests recorded the tab output; it now records
the file's own indentation.
There was a problem hiding this comment.
Pull request overview
Fixes DocCommentSniff auto-fixes that previously generated incorrect indentation by (a) emitting tab characters and (b) using a column offset as a repeat count. The updated logic reuses the existing getIndentationWhitespace() helper so fixer output matches the file’s actual leading whitespace and stays compatible with Generic.WhiteSpace.DisallowTabIndent.
Changes:
- Replace tab-based indentation building with
getIndentationWhitespace()in twoDocCommentSnifffixing paths. - Update the
DocCommentfixer fixture output to reflect space indentation rather than tab indentation.
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| PhpCollective/Sniffs/Commenting/DocCommentSniff.php | Switch fixer indentation logic to getIndentationWhitespace() to avoid emitting tabs / incorrect indent depth. |
| tests/_data/DocComment/after.php | Refresh expected “after” fixture to match the corrected indentation output. |
Suppressed comments (1)
PhpCollective/Sniffs/Commenting/DocCommentSniff.php:83
- When splitting an inline close tag onto its own line, the fixer leaves behind the single space that previously separated the last word from
*/, resulting in trailing whitespace on the preceding* ...line (see tests/_data/DocComment/before.php line 10 vs after.php line 10). Since the ruleset enablesSquiz.WhiteSpace.SuperfluousWhitespace(PhpCollective/ruleset.xml:169), this can causephpcbfto introduce a new whitespace violation that then requires another fixer pass to clean up. Consider also removing anyT_DOC_COMMENT_WHITESPACEtokens between$prevand$commentEndon that same line when applying theContentBeforeClosefix.
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($commentEnd, $indentation . ' ' . $tokens[$commentEnd]['content']);
$phpcsFile->fixer->addNewlineBefore($commentEnd);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Two fixes in
DocCommentSniffbuilt indentation like this:That is wrong in two ways at once.
It emits tabs the standard forbids. The ruleset enables
Generic.WhiteSpace.DisallowTabIndent, so running phpcbf produced indentation that phpcs then reports:It uses a column offset as a repeat count. A doc block indented one level with four spaces sits at column 5, so it emitted four tabs - four levels of indentation for a one-level block.
On a normal space-indented file,
/** Summary. @param string $a */came out as:Both sites now use
getIndentationWhitespace(), the helper already used for this inDocBlockTagGrouping, which reads the line's actual leading whitespace:The
DocCommentfixture from #73 recorded the tab output as current behavior; it now records the file's own indentation. Regenerated from the fixer rather than hand-edited.