@@ -143,29 +143,20 @@ private function promoteProperties(Tokens $tokens, int $classIndex, ConstructorA
143143 continue ;
144144 }
145145
146- $ propertyType = '' ;
147- if ($ propertyIndex !== null ) {
148- $ propertyType = $ this ->getType ($ tokens , $ propertyIndex );
149- }
150-
151- $ parameterTypeType = $ this ->getType ($ tokens , $ constructorParameterIndex );
146+ $ propertyType = $ this ->getType ($ tokens , $ propertyIndex );
147+ $ parameterType = $ this ->getType ($ tokens , $ constructorParameterIndex );
152148
153- if (!$ this ->typesAllowPromoting ($ propertyType , $ parameterTypeType )) {
149+ if (!$ this ->typesAllowPromoting ($ propertyType , $ parameterType )) {
154150 continue ;
155151 }
156152
157- $ tokensToInsert = [new Token ([\T_PUBLIC , 'public ' ])];
158- if ($ propertyIndex !== null ) {
159- $ tokensToInsert = $ this ->removePropertyAndReturnTokensToInsert ($ tokens , $ propertyIndex );
160- }
161-
162153 $ assignedPropertyIndex = $ tokens ->getPrevTokenOfKind ($ constructorPromotableAssignments [$ constructorParameterName ], [[\T_STRING ]]);
163- $ this -> renameVariable (
164- $ tokens,
165- $ constructorAnalysis -> getConstructorIndex (),
166- $ tokens [ $ constructorParameterIndex ]-> getContent (),
167- ' $ ' . $ tokens [ $ assignedPropertyIndex ]-> getContent ()
168- );
154+ $ oldParameterName = $ tokens [ $ constructorParameterIndex ]-> getContent ();
155+ $ newParameterName = ' $ ' . $ tokens[ $ assignedPropertyIndex ]-> getContent ();
156+
157+ $ tokensToInsert = $ this -> removePropertyAndReturnTokensToInsert ( $ tokens , $ propertyIndex );
158+
159+ $ this -> renameVariable ( $ tokens , $ constructorAnalysis -> getConstructorIndex (), $ oldParameterName , $ newParameterName );
169160
170161 $ this ->removeAssignment ($ tokens , $ constructorPromotableAssignments [$ constructorParameterName ]);
171162 $ this ->updateParameterSignature (
@@ -242,16 +233,19 @@ private function isPropertyToPromote(Tokens $tokens, ?int $propertyIndex, bool $
242233 return \count ($ docBlock ->getAnnotations ()) === 0 ;
243234 }
244235
245- private function getType (Tokens $ tokens , int $ variableIndex ): string
236+ private function getType (Tokens $ tokens , ? int $ variableIndex ): string
246237 {
247- $ type = '' ;
238+ if ($ variableIndex === null ) {
239+ return '' ;
240+ }
248241
249242 $ index = $ tokens ->getPrevTokenOfKind ($ variableIndex , ['( ' , ', ' , [\T_PRIVATE ], [\T_PROTECTED ], [\T_PUBLIC ], [\T_VAR ], [CT ::T_ATTRIBUTE_CLOSE ]]);
250243 \assert (\is_int ($ index ));
251244
252245 $ index = $ tokens ->getNextMeaningfulToken ($ index );
253246 \assert (\is_int ($ index ));
254247
248+ $ type = '' ;
255249 while ($ index < $ variableIndex ) {
256250 $ type .= $ tokens [$ index ]->getContent ();
257251
@@ -262,7 +256,7 @@ private function getType(Tokens $tokens, int $variableIndex): string
262256 return $ type ;
263257 }
264258
265- private function typesAllowPromoting (string $ propertyType , string $ parameterTypeType ): bool
259+ private function typesAllowPromoting (string $ propertyType , string $ parameterType ): bool
266260 {
267261 if ($ propertyType === '' ) {
268262 return true ;
@@ -272,11 +266,11 @@ private function typesAllowPromoting(string $propertyType, string $parameterType
272266 $ propertyType = \substr ($ propertyType , 1 );
273267 }
274268
275- if (\substr ($ parameterTypeType , 0 , 1 ) === '? ' ) {
276- $ parameterTypeType = \substr ($ parameterTypeType , 1 );
269+ if (\substr ($ parameterType , 0 , 1 ) === '? ' ) {
270+ $ parameterType = \substr ($ parameterType , 1 );
277271 }
278272
279- return \strtolower ($ propertyType ) === \strtolower ($ parameterTypeType );
273+ return \strtolower ($ propertyType ) === \strtolower ($ parameterType );
280274 }
281275
282276 /**
@@ -304,8 +298,12 @@ private function getClassProperties(Tokens $tokens, int $classIndex): array
304298 /**
305299 * @return array<Token>
306300 */
307- private function removePropertyAndReturnTokensToInsert (Tokens $ tokens , int $ propertyIndex ): array
301+ private function removePropertyAndReturnTokensToInsert (Tokens $ tokens , ? int $ propertyIndex ): array
308302 {
303+ if ($ propertyIndex === null ) {
304+ return [new Token ([\T_PUBLIC , 'public ' ])];
305+ }
306+
309307 $ visibilityIndex = $ tokens ->getPrevTokenOfKind ($ propertyIndex , [[\T_PRIVATE ], [\T_PROTECTED ], [\T_PUBLIC ], [\T_VAR ]]);
310308 \assert (\is_int ($ visibilityIndex ));
311309
0 commit comments