Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/Analyzer/Analysis/ConstructorAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,17 @@ public function getConstructorPromotableAssignments(): array
$closeBrace = $this->tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $openBrace);

$variables = [];
$variablesWithAssignments = [];
$properties = [];
$propertyToVariableMap = [];

for ($index = $openBrace + 1; $index < $closeBrace; $index++) {
if (!$this->tokens[$index]->isGivenKind(\T_VARIABLE)) {
continue;
}

foreach ($this->tokens->findGivenKind(\T_VARIABLE, $openBrace, $closeBrace) as $index => $token) {
$semicolonIndex = $this->tokens->getNextMeaningfulToken($index);
\assert(\is_int($semicolonIndex));
if (!$this->tokens[$semicolonIndex]->equals(';')) {
if ($this->tokens[$semicolonIndex]->equals('=')) {
$variablesWithAssignments[] = $token->getContent();
}
continue;
}

Expand All @@ -129,8 +129,12 @@ public function getConstructorPromotableAssignments(): array
continue;
}

if (\in_array($token->getContent(), $variablesWithAssignments, true)) {
continue;
}

$properties[$propertyIndex] = $this->tokens[$propertyIndex]->getContent();
$variables[$index] = $this->tokens[$index]->getContent();
$variables[$index] = $token->getContent();
$propertyToVariableMap[$propertyIndex] = $index;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Analyzer/Analysis/ConstructorAnalysisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,19 @@ public function __construct($x) {
}
}',
];

yield 'with assignment of parameter' => [
['$x' => 30, '$z' => 68],
'<?php class Foo {
public function __construct($x, $y, $z) {
$this->x = $x;
if (someCondition()) {
$y = -1;
}
$this->y = $y;
$this->z = $z;
}
}',
];
}
}
22 changes: 21 additions & 1 deletion tests/Fixer/PromotedConstructorPropertyFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,26 @@ public function __construct(int $x, int $y, int $z, bool $condition)
',
];

yield 'do not promote when there is assignment to variable' => [
<<<'PHP'
<?php
class Foo
{
private string $value;

public function __construct(string $value = '')
{
if ($value === 'deleted') {
$value = '';
}

$this->value = $value;
}
}

PHP,
];

yield 'promote nullable types' => [
'<?php class Foo {
private int $z;
Expand Down Expand Up @@ -990,7 +1010,7 @@ public function __construct(
}',
];

yield 'promote single property when propery is declared last' => [
yield 'promote single property when property is declared last' => [
'<?php class Foo {
public function namedConstructor($a, $b) { return new self($a . $b); }
public function __construct(private string $bar) {
Expand Down
Loading