3030 * typeLength: non-negative-int,
3131 * nameLength: positive-int,
3232 * nameIndex: int,
33+ * isVariadic: bool,
3334 * }
3435 */
3536final 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
0 commit comments