Skip to content

Commit 98d936a

Browse files
authored
PromotedConstructorPropertyFixer - cleanup (#766)
1 parent ba930a2 commit 98d936a

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed

src/Analyzer/Analysis/ConstructorAnalysis.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function getConstructorPromotableParameters(): array
7272
continue;
7373
}
7474

75-
$constructorPromotableParameters[$index] = \substr($this->tokens[$index]->getContent(), 1);
75+
$constructorPromotableParameters[$index] = $this->tokens[$index]->getContent();
7676
}
7777

7878
return $constructorPromotableParameters;
@@ -112,7 +112,7 @@ public function getConstructorPromotableAssignments(): array
112112
}
113113

114114
$properties[$propertyIndex] = $this->tokens[$propertyIndex]->getContent();
115-
$variables[$index] = \substr($this->tokens[$index]->getContent(), 1);
115+
$variables[$index] = $this->tokens[$index]->getContent();
116116
$propertyToVariableMap[$propertyIndex] = $index;
117117
}
118118

src/Fixer/PromotedConstructorPropertyFixer.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/Analyzer/Analysis/ConstructorAnalysisTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,29 @@ public function testGettingConstructorPromotableParameters(array $expected, stri
4242
public static function provideGettingConstructorPromotableParametersCases(): iterable
4343
{
4444
yield 'simple parameters' => [
45-
[15 => 'a', 20 => 'b', 25 => 'i'],
45+
[15 => '$a', 20 => '$b', 25 => '$i'],
4646
'<?php class Foo {
4747
public function __construct(array $a, bool $b, int $i) {}
4848
}',
4949
];
5050

5151
yield 'parameters without types are not supported' => [
52-
[21 => 'i'],
52+
[21 => '$i'],
5353
'<?php class Foo {
5454
public function __construct($noType1, $noType2, int $i, $noType3) {}
5555
}',
5656
];
5757

5858
yield 'callable is not supported' => [
59-
[15 => 'a', 20 => 'b', 35 => 'i'],
59+
[15 => '$a', 20 => '$b', 35 => '$i'],
6060
'<?php class Foo {
6161
public function __construct(array $a, bool $b, callable $c1, CALLABLE $c1, int $i) {}
6262
}',
6363
];
6464

6565
if (\PHP_VERSION_ID >= 80000) {
6666
yield 'some already promoted' => [
67-
[22 => 'b', 39 => 's'],
67+
[22 => '$b', 39 => '$s'],
6868
'<?php class Foo {
6969
public function __construct(public array $a, bool $b, protected ?Bar\Baz\Qux $q, string $s, private OtherType $t) {}
7070
}',
@@ -91,7 +91,7 @@ public function testGettingConstructorPromotableAssignments(array $expected, str
9191
public static function provideGettingConstructorPromotableAssignmentsCases(): iterable
9292
{
9393
yield 'simple assignments' => [
94-
['x' => 30, 'y' => 39, 'z' => 48],
94+
['$x' => 30, '$y' => 39, '$z' => 48],
9595
'<?php class Foo {
9696
public function __construct($x, $y, $z) {
9797
$this->a = $x;
@@ -102,7 +102,7 @@ public function __construct($x, $y, $z) {
102102
];
103103

104104
yield 'duplicated assigned parameters' => [
105-
['x' => 30, 'z' => 59],
105+
['$x' => 30, '$z' => 59],
106106
'<?php class Foo {
107107
public function __construct($x, $y, $z) {
108108
$this->a = $x;
@@ -114,7 +114,7 @@ public function __construct($x, $y, $z) {
114114
];
115115

116116
yield 'duplicated assigned properties' => [
117-
['x' => 30],
117+
['$x' => 30],
118118
'<?php class Foo {
119119
public function __construct($x, $y, $z) {
120120
$this->a = $x;
@@ -125,7 +125,7 @@ public function __construct($x, $y, $z) {
125125
];
126126

127127
yield 'not allowed assignment' => [
128-
['e' => 86],
128+
['$e' => 86],
129129
'<?php class Foo {
130130
public function __construct($a, $b, $c, $d, $e) {
131131
$this->a = $a + 1;
@@ -138,7 +138,7 @@ public function __construct($a, $b, $c, $d, $e) {
138138
];
139139

140140
yield 'nested assignment' => [
141-
['x' => 30, 'z' => 60],
141+
['$x' => 30, '$z' => 60],
142142
'<?php class Foo {
143143
public function __construct($x, $y, $z) {
144144
$this->x = $x;

0 commit comments

Comments
 (0)