Skip to content

Fix docblock indentation loss when fixing empty enclosing line#52

Merged
dereuromark merged 2 commits intomasterfrom
fix-empty-enclosing-line-indentation
Apr 10, 2026
Merged

Fix docblock indentation loss when fixing empty enclosing line#52
dereuromark merged 2 commits intomasterfrom
fix-empty-enclosing-line-indentation

Conversation

@dereuromark
Copy link
Copy Markdown
Contributor

Summary

  • Fixed a bug where EmptyEnclosingLineSniff caused docblock indentation to be lost when combined with other fixers

Problem

When processing a file with tabs and an empty line after a class opening brace, running phpcbf with the full PhpCollective ruleset caused the first content (typically a docblock) to lose its indentation entirely.

Input:

class Example {

	/**
	 * @param string $foo
	 */
	public function test(string $foo): void {

Before fix (incorrect):

class Example
{
/**
 * @param string $foo
 */
    public function test(string $foo): void

After fix (correct):

class Example
{
    /**
     * @param string $foo
     */
    public function test(string $foo): void

Root Cause

The EmptyEnclosingLineSniff removed all tokens between the opening brace and the first content line, including the newline that should remain. When combined with Generic.WhiteSpace.DisallowTabIndent (which converts tabs to spaces), the fixer loop interactions caused the indentation token to be lost.

Solution

The fix explicitly preserves one newline after the opening brace by:

  1. Replacing the first token after the brace with a newline character
  2. Removing the remaining tokens up to (but not including) the indentation of the content line

All existing tests pass.

When EmptyEnclosingLine sniff fixed empty lines after class opening
brace, it removed all tokens including the newline that should remain.
Combined with other fixers (like DisallowTabIndent that converts tabs
to spaces), this caused the first content line (typically a docblock)
to lose its indentation entirely.

The fix explicitly preserves one newline after the opening brace by
replacing the first token with a newline character, then removing
the remaining tokens up to the content line.
Adds test cases to verify that the EmptyEnclosingLine sniff
correctly preserves indentation when removing empty lines
after class opening braces, particularly when using tabs.
@dereuromark dereuromark merged commit 024d26f into master Apr 10, 2026
4 checks passed
@dereuromark dereuromark deleted the fix-empty-enclosing-line-indentation branch April 10, 2026 14:53
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