Skip to content

Commit f274c08

Browse files
authored
PromotedConstructorPropertyFixer - get promoted property name from assignment, not from property (#734)
1 parent a5d2ce1 commit f274c08

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-3366-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3367-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)

src/Fixer/PromotedConstructorPropertyFixer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,12 @@ private function promoteProperties(Tokens $tokens, int $classIndex, ConstructorA
156156

157157
$tokensToInsert = [new Token([\T_PUBLIC, 'public'])];
158158
if ($propertyIndex !== null) {
159-
$tokensToInsert = $this->removePropertyAndReturnTokensToInsert($tokens, $propertyIndex, $constructorParameterIndex);
159+
$tokensToInsert = $this->removePropertyAndReturnTokensToInsert($tokens, $propertyIndex);
160160
}
161161

162+
$assignedPropertyIndex = $tokens->getPrevTokenOfKind($constructorPromotableAssignments[$constructorParameterName], [[\T_STRING]]);
163+
$tokens[$constructorParameterIndex] = new Token([\T_VARIABLE, '$' . $tokens[$assignedPropertyIndex]->getContent()]);
164+
162165
$this->removeAssignment($tokens, $constructorPromotableAssignments[$constructorParameterName]);
163166
$this->updateParameterSignature(
164167
$tokens,
@@ -296,10 +299,8 @@ private function getClassProperties(Tokens $tokens, int $classIndex): array
296299
/**
297300
* @return array<Token>
298301
*/
299-
private function removePropertyAndReturnTokensToInsert(Tokens $tokens, int $propertyIndex, int $parameterIndex): array
302+
private function removePropertyAndReturnTokensToInsert(Tokens $tokens, int $propertyIndex): array
300303
{
301-
$tokens[$parameterIndex] = $tokens[$propertyIndex];
302-
303304
$visibilityIndex = $tokens->getPrevTokenOfKind($propertyIndex, [[\T_PRIVATE], [\T_PROTECTED], [\T_PUBLIC], [\T_VAR]]);
304305
\assert(\is_int($visibilityIndex));
305306

tests/Fixer/PromotedConstructorPropertyFixerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,20 @@ public function __construct(string $y) {
682682
',
683683
];
684684

685+
yield 'promote with different name when not defined' => [
686+
'<?php class Foo {
687+
public function __construct(public string $x) {
688+
}
689+
}
690+
',
691+
'<?php class Foo {
692+
public function __construct(string $y) {
693+
$this->x = $y;
694+
}
695+
}
696+
',
697+
];
698+
685699
yield 'promote in multiple classes' => [
686700
'<?php
687701
abstract class Foo { // promote here

0 commit comments

Comments
 (0)