diff --git a/PhpCollective/Sniffs/WhiteSpace/EmptyEnclosingLineSniff.php b/PhpCollective/Sniffs/WhiteSpace/EmptyEnclosingLineSniff.php index b3d8077..83508a0 100644 --- a/PhpCollective/Sniffs/WhiteSpace/EmptyEnclosingLineSniff.php +++ b/PhpCollective/Sniffs/WhiteSpace/EmptyEnclosingLineSniff.php @@ -91,7 +91,9 @@ public function process(File $phpcsFile, $stackPtr): void if ($contentLine < $braceLine + 1) { $phpcsFile->fixer->addNewline($curlyBraceStartIndex); } else { - for ($i = $curlyBraceStartIndex + 1; $i < $beginningOfLine - 1; $i++) { + // Replace first token with newline, remove the rest + $phpcsFile->fixer->replaceToken($curlyBraceStartIndex + 1, $phpcsFile->eolChar); + for ($i = $curlyBraceStartIndex + 2; $i < $beginningOfLine; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } } diff --git a/tests/PhpCollective/Sniffs/WhiteSpace/EmptyEnclosingLineSniffTest.php b/tests/PhpCollective/Sniffs/WhiteSpace/EmptyEnclosingLineSniffTest.php index e3f19ce..2881894 100644 --- a/tests/PhpCollective/Sniffs/WhiteSpace/EmptyEnclosingLineSniffTest.php +++ b/tests/PhpCollective/Sniffs/WhiteSpace/EmptyEnclosingLineSniffTest.php @@ -27,4 +27,31 @@ public function testEmptyEnclosingLineFixer(): void { $this->assertSnifferCanFixErrors(new EmptyEnclosingLineSniff()); } + + /** + * Tests that tabs are preserved when fixing empty enclosing line. + * + * @return void + */ + public function testTabsPreservedSniffer(): void + { + $this->prefix = 'tabs-'; + $this->assertSnifferFindsErrors(new EmptyEnclosingLineSniff(), 1); + $this->prefix = null; + } + + /** + * Tests fixer preserves indentation with tabs. + * + * This test verifies the fix for the bug where indentation was lost + * when removing empty lines after opening braces. + * + * @return void + */ + public function testTabsPreservedFixer(): void + { + $this->prefix = 'tabs-'; + $this->assertSnifferCanFixErrors(new EmptyEnclosingLineSniff()); + $this->prefix = null; + } } diff --git a/tests/_data/EmptyEnclosingLine/tabs-after.php b/tests/_data/EmptyEnclosingLine/tabs-after.php new file mode 100644 index 0000000..b31986a --- /dev/null +++ b/tests/_data/EmptyEnclosingLine/tabs-after.php @@ -0,0 +1,15 @@ +