Skip to content

Commit

Permalink
update variable reference
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 23, 2023
1 parent b1fa483 commit 4a8e425
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ final class SkipMergedProperty
}

?>

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\NullableType;
Expand Down Expand Up @@ -84,12 +85,12 @@ public function getRuleDefinition(): RuleDefinition
<<<'CODE_SAMPLE'
class SomeClass
{
public float $someVariable;
public float $price;
public function __construct(
float $someVariable = 0.0
float $price = 0.0
) {
$this->someVariable = $someVariable;
$this->price = $price;
}
}
CODE_SAMPLE
Expand All @@ -98,7 +99,7 @@ public function __construct(
class SomeClass
{
public function __construct(
public float $someVariable = 0.0
public float $price = 0.0
) {
}
}
Expand Down Expand Up @@ -215,6 +216,22 @@ public function refactor(Node $node): ?Node
$param,
$paramTagValueNode
);

// update variable to property fetch references
$this->traverseNodesWithCallable((array) $constructClassMethod->stmts, function (Node $node) use (
$promotionCandidate,
$propertyName
): ?PropertyFetch {
if (! $node instanceof Variable) {
return null;
}

if (! $this->isName($node, $promotionCandidate->getParamName())) {
return null;
}

return new PropertyFetch(new Variable('this'), $propertyName);
});
}

return $node;
Expand Down
17 changes: 17 additions & 0 deletions rules/Php80/ValueObject/PropertyPromotionCandidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Rector\Php80\ValueObject;

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class PropertyPromotionCandidate
Expand All @@ -28,6 +30,21 @@ public function getParam(): Param
return $this->param;
}

public function getParamName(): string
{
$paramVar = $this->param->var;

if (! $paramVar instanceof Variable) {
throw new ShouldNotHappenException();
}

if (! is_string($paramVar->name)) {
throw new ShouldNotHappenException();
}

return $paramVar->name;
}

public function getStmtPosition(): int
{
return $this->expression->getAttribute(AttributeKey::STMT_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ final class Fixture
{
public function __construct(private PromotedPropertyObject $promotedPropertyObject)
{
dump($promotedPropertyObject);
dump($promotedPropertyObject->someCall('Y-m-d'));
dump($this->promotedPropertyObject);
dump($this->promotedPropertyObject->someCall('Y-m-d'));
}
}

Expand Down

0 comments on commit 4a8e425

Please sign in to comment.