Skip to content

Commit f824fcd

Browse files
authored
MultilinePromotedPropertiesFixer - add option "keep_blank_lines" (#874)
1 parent 5642e74 commit f824fcd

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-3409-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3411-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)
@@ -170,6 +170,7 @@ Multiline comments or PHPDocs must contain an opening and closing line with no a
170170
A constructor with promoted properties must have them in separate lines.
171171
Configuration options:
172172
- `minimum_number_of_parameters` (`int`): minimum number of parameters in the constructor to fix; defaults to `1`
173+
- `keep_blank_lines` (`bool`): whether to keep blank lines between properties; defaults to `false`
173174
```diff
174175
<?php class Foo {
175176
- public function __construct(private array $a, private bool $b, private int $i) {}

src/Fixer/MultilinePromotedPropertiesFixer.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
final class MultilinePromotedPropertiesFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface
3030
{
3131
private int $minimumNumberOfParameters = 1;
32+
private bool $keepBlankLines = false;
3233
private WhitespacesFixerConfig $whitespacesConfig;
3334

3435
public function getDefinition(): FixerDefinitionInterface
@@ -54,17 +55,24 @@ public function getConfigurationDefinition(): FixerConfigurationResolverInterfac
5455
->setAllowedTypes(['int'])
5556
->setDefault($this->minimumNumberOfParameters)
5657
->getOption(),
58+
(new FixerOptionBuilder('keep_blank_lines', 'whether to keep blank lines between properties'))
59+
->setAllowedTypes(['bool'])
60+
->setDefault($this->keepBlankLines)
61+
->getOption(),
5762
]);
5863
}
5964

6065
/**
61-
* @param array<string, int> $configuration
66+
* @param array{minimum_number_of_parameters?: int, keep_blank_lines?: bool} $configuration
6267
*/
6368
public function configure(array $configuration): void
6469
{
6570
if (\array_key_exists('minimum_number_of_parameters', $configuration)) {
6671
$this->minimumNumberOfParameters = $configuration['minimum_number_of_parameters'];
6772
}
73+
if (\array_key_exists('keep_blank_lines', $configuration)) {
74+
$this->keepBlankLines = $configuration['keep_blank_lines'];
75+
}
6876
}
6977

7078
public function setWhitespacesConfig(WhitespacesFixerConfig $config): void
@@ -170,11 +178,20 @@ private function fixParameters(Tokens $tokens, int $openParenthesis, int $closeP
170178
continue;
171179
}
172180

173-
$tokens->ensureWhitespaceAtIndex(
174-
$index + 1,
175-
0,
176-
$this->whitespacesConfig->getLineEnding() . $indent . $this->whitespacesConfig->getIndent(),
177-
);
181+
$this->fixParameter($tokens, $index + 1, $indent);
178182
}
179183
}
184+
185+
private function fixParameter(Tokens $tokens, int $index, string $indent): void
186+
{
187+
if ($this->keepBlankLines && $tokens[$index]->isWhitespace() && \str_contains($tokens[$index]->getContent(), "\n")) {
188+
return;
189+
}
190+
191+
$tokens->ensureWhitespaceAtIndex(
192+
$index,
193+
0,
194+
$this->whitespacesConfig->getLineEnding() . $indent . $this->whitespacesConfig->getIndent(),
195+
);
196+
}
180197
}

tests/Fixer/MultilinePromotedPropertiesFixerTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function testConfiguration(): void
3333
self::assertArrayHasKey(0, $options);
3434
self::assertSame('minimum_number_of_parameters', $options[0]->getName());
3535
self::assertSame(1, $options[0]->getDefault());
36+
self::assertSame('keep_blank_lines', $options[1]->getName());
37+
self::assertFalse($options[1]->getDefault());
3638
}
3739

3840
public function testIsRisky(): void
@@ -51,7 +53,7 @@ public function testFix(string $expected, ?string $input = null, array $configur
5153
}
5254

5355
/**
54-
* @return iterable<array{0: string, 1: null|string, 2?: array<string, int>}>
56+
* @return iterable<array{0: string, 1: null|string, 2?: array<string, bool|int>}>
5557
*/
5658
public static function provideFixCases(): iterable
5759
{
@@ -219,5 +221,43 @@ public function __construct(int $x, int $y) {}
219221
['minimum_number_of_parameters' => $numberOfParameters],
220222
];
221223
}
224+
225+
yield 'blank lines removed' => [
226+
'<?php class Foo {
227+
public function __construct(
228+
private int $x,
229+
private int $y,
230+
private int $z
231+
) {}
232+
}',
233+
'<?php class Foo {
234+
public function __construct(
235+
private int $x,
236+
237+
private int $y,
238+
239+
private int $z
240+
) {}
241+
}',
242+
];
243+
244+
yield 'blank lines kept' => [
245+
'<?php class Foo {
246+
public function __construct(
247+
private int $x,
248+
private int $y,
249+
250+
private int $z
251+
) {}
252+
}',
253+
'<?php class Foo {
254+
public function __construct(
255+
private int $x, private int $y,
256+
257+
private int $z
258+
) {}
259+
}',
260+
['keep_blank_lines' => true],
261+
];
222262
}
223263
}

0 commit comments

Comments
 (0)