Skip to content

Commit 5458830

Browse files
committed
Fixes #9. ErickSkrauch/align_multiline_parameters now correctly aligns variadic arguments
1 parent ec5f8a4 commit 5458830

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
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+
### Fixed
9+
- Bug #9: `ErickSkrauch/align_multiline_parameters` now correctly aligns variadic arguments.
810

911
## [1.2.3] - 2024-01-09
1012
### Fixed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ Forces aligned or not aligned multiline function parameters:
4141
string $string,
4242
- int $index = 0,
4343
- $arg = 'no type',
44+
- ...$variadic,
4445
+ int $index = 0,
4546
+ $arg = 'no type',
47+
+ ...$variadic
4648
): void {}
4749
```
4850

src/FunctionNotation/AlignMultilineParametersFixer.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* typeLength: non-negative-int,
3131
* nameLength: positive-int,
3232
* nameIndex: int,
33+
* isVariadic: bool,
3334
* }
3435
*/
3536
final class AlignMultilineParametersFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface {
@@ -83,7 +84,8 @@ public function isCandidate(Tokens $tokens): bool {
8384

8485
/**
8586
* Must run after StatementIndentationFixer, MethodArgumentSpaceFixer, CompactNullableTypehintFixer,
86-
* SingleSpaceAroundConstructFixer, TypesSpacesFixer
87+
* SingleSpaceAroundConstructFixer, TypesSpacesFixer, UnaryOperatorSpacesFixer,
88+
* FunctionTypehintSpaceFixer, TypeDeclarationSpacesFixer
8789
*/
8890
public function getPriority(): int {
8991
return -10;
@@ -131,6 +133,8 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
131133
$longestType = 0;
132134
$longestVariableName = 0;
133135
$hasAtLeastOneTypedArgument = false;
136+
/** @var bool|null $isVariadicArgTypeLong */
137+
$isVariadicArgTypeLong = null;
134138
/** @var list<DeclarationAnalysis> $analysedArguments */
135139
$analysedArguments = [];
136140
foreach ($arguments as $argument) {
@@ -148,6 +152,10 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
148152
$longestVariableName = $declarationAnalysis['nameLength'];
149153
}
150154

155+
if ($declarationAnalysis['isVariadic']) {
156+
$isVariadicArgTypeLong = $longestType === $declarationAnalysis['typeLength'];
157+
}
158+
151159
$analysedArguments[] = $declarationAnalysis;
152160
}
153161

@@ -170,9 +178,27 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
170178
}
171179

172180
if ($this->configuration[self::C_VARIABLES] !== null) {
173-
$whitespaceIndex = $argument['nameIndex'] - 1;
181+
if ($argument['isVariadic']) {
182+
$whitespaceIndex = $tokens->getPrevMeaningfulToken($argument['nameIndex']) - 1;
183+
} else {
184+
$whitespaceIndex = $argument['nameIndex'] - 1;
185+
}
186+
174187
if ($this->configuration[self::C_VARIABLES] === true) {
175-
$appendix = str_repeat(' ', $longestType - $argument['typeLength'] + (int)$hasAtLeastOneTypedArgument);
188+
$alignLength = $longestType - $argument['typeLength'] + (int)$hasAtLeastOneTypedArgument;
189+
if ($isVariadicArgTypeLong !== null) {
190+
if ($isVariadicArgTypeLong) {
191+
if (!$argument['isVariadic']) {
192+
$alignLength += 3;
193+
}
194+
} else {
195+
if ($argument['isVariadic']) {
196+
$alignLength -= 3;
197+
}
198+
}
199+
}
200+
201+
$appendix = str_repeat(' ', $alignLength);
176202
if ($argument['typeLength'] > 0) {
177203
$whitespaceToken = $appendix;
178204
} else {
@@ -197,6 +223,15 @@ private function getDeclarationAnalysis(Tokens $tokens, int $nameIndex, ?TypeAna
197223
$searchIndex = $nameIndex;
198224
$includeNextWhitespace = false;
199225
$typeLength = 0;
226+
227+
$isVariadic = false;
228+
$variadicTokenIndex = $tokens->getPrevMeaningfulToken($searchIndex);
229+
$variadicToken = $tokens[$variadicTokenIndex];
230+
if ($variadicToken->isGivenKind(T_ELLIPSIS)) {
231+
$isVariadic = true;
232+
$searchIndex = $variadicTokenIndex;
233+
}
234+
200235
if ($typeAnalysis !== null) {
201236
$searchIndex = $typeAnalysis->getStartIndex();
202237
$includeNextWhitespace = true;
@@ -237,6 +272,7 @@ private function getDeclarationAnalysis(Tokens $tokens, int $nameIndex, ?TypeAna
237272
'typeLength' => $typeLength,
238273
'nameLength' => $nameLength,
239274
'nameIndex' => $nameIndex,
275+
'isVariadic' => $isVariadic,
240276
];
241277
}
242278

tests/FunctionNotation/AlignMultilineParametersFixerTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,51 @@ function test(
216216
): void {}
217217
',
218218
];
219+
220+
yield 'variadic parameter (short)' => [
221+
'<?php
222+
function test(
223+
Stringable $string,
224+
int ...$params
225+
): void {}
226+
',
227+
'<?php
228+
function test(
229+
Stringable $string,
230+
int ...$params
231+
): void {}
232+
',
233+
];
234+
235+
yield 'variadic parameter (long)' => [
236+
'<?php
237+
function test(
238+
Stringable $string,
239+
DateTimeImmutable ...$params
240+
): void {}
241+
',
242+
'<?php
243+
function test(
244+
Stringable $string,
245+
DateTimeImmutable ...$params
246+
): void {}
247+
',
248+
];
249+
250+
yield 'variadic parameter (untyped)' => [
251+
'<?php
252+
function test(
253+
Stringable $string,
254+
...$params
255+
): void {}
256+
',
257+
'<?php
258+
function test(
259+
Stringable $string,
260+
...$params
261+
): void {}
262+
',
263+
];
219264
}
220265

221266
/**

0 commit comments

Comments
 (0)