Skip to content

Commit

Permalink
Fix multi property default assign (#2986)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 14, 2022
1 parent 2427c5a commit ee04ee1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector\Fixture;

final class MultiProperties
{
private $thing1, $thing2, $thing3;

public function __construct()
{
$this->thing1 = 1;
$this->thing2 = 2;
$this->thing3 = 3;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector\Fixture;

final class MultiProperties
{
private $thing1 = 1, $thing2 = 2, $thing3 = 3;

public function __construct()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use Rector\CodeQuality\NodeAnalyzer\ConstructorPropertyDefaultExprResolver;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
Expand Down Expand Up @@ -71,28 +72,26 @@ public function refactor(Node $node): ?Node

// resolve property defaults
$defaultPropertyExprAssigns = $this->constructorPropertyDefaultExprResolver->resolve($constructClassMethod);

if ($defaultPropertyExprAssigns === []) {
return null;
}

$hasChanged = false;
foreach ($defaultPropertyExprAssigns as $defaultPropertyExprAssign) {
$property = $node->getProperty($defaultPropertyExprAssign->getPropertyName());
if (! $property instanceof Property) {
continue;
}

if ($property->isReadonly()) {
continue;
}
$propertyProperties = $this->getNonReadonlyPropertyProperty($node);

foreach ($defaultPropertyExprAssigns as $defaultPropertyExprAssign) {
foreach ($propertyProperties as $propertyProperty) {
if (! $this->isName($propertyProperty, $defaultPropertyExprAssign->getPropertyName())) {
continue;
}

$propertyProperty = $property->props[0];
$propertyProperty->default = $defaultPropertyExprAssign->getDefaultExpr();
$propertyProperty->default = $defaultPropertyExprAssign->getDefaultExpr();

$hasChanged = true;
$hasChanged = true;

$this->removeNode($defaultPropertyExprAssign->getAssignExpression());
$this->removeNode($defaultPropertyExprAssign->getAssignExpression());
}
}

if (! $hasChanged) {
Expand All @@ -101,4 +100,22 @@ public function refactor(Node $node): ?Node

return $node;
}

/**
* @return PropertyProperty[]
*/
private function getNonReadonlyPropertyProperty(Class_ $class): array
{
$propertyProperties = [];

foreach ($class->getProperties() as $property) {
if ($property->isReadonly()) {
continue;
}

$propertyProperties = array_merge($propertyProperties, $property->props);
}

return $propertyProperties;
}
}

This file was deleted.

19 changes: 0 additions & 19 deletions tests/Issues/Issue7498/Fixture/fixture.php.inc

This file was deleted.

9 changes: 0 additions & 9 deletions tests/Issues/Issue7498/Fixture/fixture_b.php.inc

This file was deleted.

10 changes: 0 additions & 10 deletions tests/Issues/Issue7498/config/configured_rule.php

This file was deleted.

0 comments on commit ee04ee1

Please sign in to comment.