Skip to content

Commit d6fd445

Browse files
committed
Cover edge cases for ErickSkrauch/align_multiline_parameters in aligned mode
1 parent e29d67a commit d6fd445

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

src/FunctionNotation/AlignMultilineParametersFixer.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn
104104
]);
105105
}
106106

107+
/**
108+
* TODO: This fixer can be reimplemented using a simpler idea:
109+
* we need to find the position of the farthest token $ and =, and then align all other tokens to them.
110+
* The current implementation strongly relies on the fact, that the code will be well written and some
111+
* additional fixer like TypeDeclarationSpacesFixer will be used as well
112+
*/
107113
protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
108114
// There is nothing to do
109115
if ($this->configuration[self::C_VARIABLES] === null && $this->configuration[self::C_DEFAULTS] === null) {
@@ -133,8 +139,6 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
133139
$longestType = 0;
134140
$longestVariableName = 0;
135141
$hasAtLeastOneTypedArgument = false;
136-
/** @var bool|null $isVariadicArgTypeLong */
137-
$isVariadicArgTypeLong = null;
138142
/** @var list<DeclarationAnalysis> $analysedArguments */
139143
$analysedArguments = [];
140144
foreach ($arguments as $argument) {
@@ -153,7 +157,16 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
153157
}
154158

155159
if ($declarationAnalysis['isVariadic']) {
156-
$isVariadicArgTypeLong = $longestType === $declarationAnalysis['typeLength'];
160+
// We want to do the alignment on the $ symbol.
161+
// If none of the arguments has a type or the longest type is too short,
162+
// the variadic argument will not be able to take the correct position.
163+
// So we extend the type length so that all variables are aligned in a column by the $ symbol
164+
$longestTypeDelta = $longestType - $declarationAnalysis['typeLength'];
165+
if ($longestType === $declarationAnalysis['typeLength']) {
166+
$longestType += 3;
167+
} elseif ($longestTypeDelta < 3) { // Ensure there is enough space for "..."
168+
$longestType += 3 - $longestTypeDelta - (int)($declarationAnalysis['typeLength'] === 0);
169+
}
157170
}
158171

159172
$analysedArguments[] = $declarationAnalysis;
@@ -186,16 +199,8 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
186199

187200
if ($this->configuration[self::C_VARIABLES] === true) {
188201
$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-
}
202+
if ($argument['isVariadic']) {
203+
$alignLength -= 3; // 3 is the length of "..."
199204
}
200205

201206
$appendix = str_repeat(' ', $alignLength);

tests/FunctionNotation/AlignMultilineParametersFixerTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,21 @@ function test(
232232
',
233233
];
234234

235+
yield 'variadic parameter (short, edge case)' => [
236+
'<?php
237+
function test(
238+
float $float,
239+
int ...$params
240+
): void {}
241+
',
242+
'<?php
243+
function test(
244+
float $float,
245+
int ...$params
246+
): void {}
247+
',
248+
];
249+
235250
yield 'variadic parameter (long)' => [
236251
'<?php
237252
function test(
@@ -261,6 +276,40 @@ function test(
261276
): void {}
262277
',
263278
];
279+
280+
yield 'variadic parameter (untyped, edge case 1)' => [
281+
'<?php
282+
interface A {}
283+
function test(
284+
A $a,
285+
...$params
286+
): void {}
287+
',
288+
'<?php
289+
interface A {}
290+
function test(
291+
A $a,
292+
...$params
293+
): void {}
294+
',
295+
];
296+
297+
yield 'variadic parameter (untyped, edge case 2)' => [
298+
'<?php
299+
interface A {}
300+
function test(
301+
$a,
302+
...$params
303+
): void {}
304+
',
305+
'<?php
306+
interface A {}
307+
function test(
308+
$a,
309+
...$params
310+
): void {}
311+
',
312+
];
264313
}
265314

266315
/**

0 commit comments

Comments
 (0)