@@ -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 );
0 commit comments