Skip to content

Commit 2071827

Browse files
authored
PromotedConstructorPropertyFixer - do not promote when there is assignment to variable (#1063)
1 parent 4895cc8 commit 2071827

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

src/Analyzer/Analysis/ConstructorAnalysis.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,17 @@ public function getConstructorPromotableAssignments(): array
110110
$closeBrace = $this->tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $openBrace);
111111

112112
$variables = [];
113+
$variablesWithAssignments = [];
113114
$properties = [];
114115
$propertyToVariableMap = [];
115116

116-
for ($index = $openBrace + 1; $index < $closeBrace; $index++) {
117-
if (!$this->tokens[$index]->isGivenKind(\T_VARIABLE)) {
118-
continue;
119-
}
120-
117+
foreach ($this->tokens->findGivenKind(\T_VARIABLE, $openBrace, $closeBrace) as $index => $token) {
121118
$semicolonIndex = $this->tokens->getNextMeaningfulToken($index);
122119
\assert(\is_int($semicolonIndex));
123120
if (!$this->tokens[$semicolonIndex]->equals(';')) {
121+
if ($this->tokens[$semicolonIndex]->equals('=')) {
122+
$variablesWithAssignments[] = $token->getContent();
123+
}
124124
continue;
125125
}
126126

@@ -129,8 +129,12 @@ public function getConstructorPromotableAssignments(): array
129129
continue;
130130
}
131131

132+
if (\in_array($token->getContent(), $variablesWithAssignments, true)) {
133+
continue;
134+
}
135+
132136
$properties[$propertyIndex] = $this->tokens[$propertyIndex]->getContent();
133-
$variables[$index] = $this->tokens[$index]->getContent();
137+
$variables[$index] = $token->getContent();
134138
$propertyToVariableMap[$propertyIndex] = $index;
135139
}
136140

tests/Analyzer/Analysis/ConstructorAnalysisTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,19 @@ public function __construct($x) {
255255
}
256256
}',
257257
];
258+
259+
yield 'with assignment of parameter' => [
260+
['$x' => 30, '$z' => 68],
261+
'<?php class Foo {
262+
public function __construct($x, $y, $z) {
263+
$this->x = $x;
264+
if (someCondition()) {
265+
$y = -1;
266+
}
267+
$this->y = $y;
268+
$this->z = $z;
269+
}
270+
}',
271+
];
258272
}
259273
}

tests/Fixer/PromotedConstructorPropertyFixerTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,26 @@ public function __construct(int $x, int $y, int $z, bool $condition)
927927
',
928928
];
929929

930+
yield 'do not promote when there is assignment to variable' => [
931+
<<<'PHP'
932+
<?php
933+
class Foo
934+
{
935+
private string $value;
936+
937+
public function __construct(string $value = '')
938+
{
939+
if ($value === 'deleted') {
940+
$value = '';
941+
}
942+
943+
$this->value = $value;
944+
}
945+
}
946+
947+
PHP,
948+
];
949+
930950
yield 'promote nullable types' => [
931951
'<?php class Foo {
932952
private int $z;
@@ -990,7 +1010,7 @@ public function __construct(
9901010
}',
9911011
];
9921012

993-
yield 'promote single property when propery is declared last' => [
1013+
yield 'promote single property when property is declared last' => [
9941014
'<?php class Foo {
9951015
public function namedConstructor($a, $b) { return new self($a . $b); }
9961016
public function __construct(private string $bar) {

0 commit comments

Comments
 (0)