Skip to content

Commit

Permalink
[DeadCode] Handle used in assign return on RemoveUnusedPrivatePropert…
Browse files Browse the repository at this point in the history
…yRector (#5608)
  • Loading branch information
samsonasik committed Feb 12, 2024
1 parent 5edb023 commit 14eb72c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;

class UsedInAssignReturn
{
private $count = 0;

public function run()
{
return $this->count = 2;
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;

class UsedInAssignReturn
{
public function run()
{
return 2;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\TraitUse;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
Expand Down Expand Up @@ -155,8 +156,8 @@ private function shouldSkipClass(Class_ $class): bool

private function removePropertyAssigns(Class_ $class, string $propertyName): void
{
$this->traverseNodesWithCallable($class, function (Node $node) use ($class, $propertyName): ?int {
if (! $node instanceof Expression) {
$this->traverseNodesWithCallable($class, function (Node $node) use ($class, $propertyName): null|int|Return_ {
if (! $node instanceof Expression && ! $node instanceof Return_) {
return null;
}

Expand All @@ -169,7 +170,12 @@ private function removePropertyAssigns(Class_ $class, string $propertyName): voi
return null;
}

return NodeTraverser::REMOVE_NODE;
if ($node instanceof Expression) {
return NodeTraverser::REMOVE_NODE;
}

$node->expr = $node->expr->expr;
return $node;
});
}
}

0 comments on commit 14eb72c

Please sign in to comment.