Skip to content

Commit ab850fa

Browse files
committed
LineBreakAfterStatementsFixer no longer removes extra blank lines in consecutive closing curly braces
1 parent 4ef80d2 commit ab850fa

File tree

3 files changed

+8
-39
lines changed

3 files changed

+8
-39
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Changed
9+
- `ErickSkrauch\line_break_after_statements` no longer removes extra blank lines in consecutive closing curly braces. Use [`no_extra_blank_lines`](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/doc/rules/whitespace/no_extra_blank_lines.rst) with `curly_brace_block` tokens configuration for this fix.
810

911
## [1.1.0] - 2023-05-29
1012
### Added

src/Whitespace/LineBreakAfterStatementsFixer.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
final class LineBreakAfterStatementsFixer extends AbstractFixer implements WhitespacesAwareFixerInterface {
1717

1818
/**
19-
* There is no 'do', 'cause the processing of the 'while' also includes do {} while (); construction
19+
* There is no 'do', 'cause the processing of the 'while' also includes "do {} while ();" construction
2020
*/
2121
private const STATEMENTS = [
2222
T_IF,
@@ -92,31 +92,26 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
9292

9393
$endStatementIndex = $this->findStatementEnd($tokens, $index);
9494
$nextStatementIndex = $tokens->getNextMeaningfulToken($endStatementIndex);
95-
if ($nextStatementIndex === null) {
95+
if ($nextStatementIndex === null || $tokens[$nextStatementIndex]->equals('}')) {
9696
continue;
9797
}
9898

99-
if ($tokens[$nextStatementIndex]->equals('}')) {
100-
$this->fixBlankLines($tokens, $endStatementIndex + 1, 0);
101-
continue;
102-
}
103-
104-
$this->fixBlankLines($tokens, $endStatementIndex + 1, 1);
99+
$this->ensureBlankLine($tokens, $endStatementIndex + 1);
105100
}
106101
}
107102

108-
private function fixBlankLines(Tokens $tokens, int $index, int $countLines): void {
103+
private function ensureBlankLine(Tokens $tokens, int $index): void {
109104
$content = $tokens[$index]->getContent();
110105
// Apply fix only in the case when the count lines do not equals to expected
111-
if (substr_count($content, "\n") === $countLines + 1) {
106+
if (substr_count($content, "\n") === 2) {
112107
return;
113108
}
114109

115110
// The final bit of the whitespace must be the next statement's indentation
116111
Preg::matchAll('/[^\n\r]+[\r\n]*/', $content, $matches);
117112
$lines = $matches[0];
118113
$eol = $this->whitespacesConfig->getLineEnding();
119-
$tokens[$index] = new Token([T_WHITESPACE, str_repeat($eol, $countLines + 1) . end($lines)]);
114+
$tokens[$index] = new Token([T_WHITESPACE, str_repeat($eol, 2) . end($lines)]);
120115
}
121116

122117
private function findStatementEnd(Tokens $tokens, int $index): int {

tests/Whitespace/LineBreakAfterStatementsFixerTest.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -289,34 +289,6 @@ public function bar()
289289
yield [
290290
'<?php
291291
class Foo
292-
{
293-
public function bar()
294-
{
295-
foreach (["foo", "bar"] as $str) {
296-
if ($str === "foo") {
297-
// code
298-
}
299-
}
300-
}
301-
}',
302-
'<?php
303-
class Foo
304-
{
305-
public function bar()
306-
{
307-
foreach (["foo", "bar"] as $str) {
308-
if ($str === "foo") {
309-
// code
310-
}
311-
312-
}
313-
}
314-
}',
315-
];
316-
317-
yield [
318-
'<?php
319-
class Foo
320292
{
321293
public function foo()
322294
{

0 commit comments

Comments
 (0)