Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode] Handle used in assign return on RemoveUnusedPrivatePropertyRector #5608

Merged
merged 1 commit into from
Feb 12, 2024
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
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;
});
}
}
Loading