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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

final class KeepSideEffectOfAssignedCall
{
private $connection;

private $name;

public function __construct(Factory $factory, string $name)
{
$this->connection = (object) $factory->connect();
$this->name = $name;
}
}

?>
-----
<?php

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

final class KeepSideEffectOfAssignedCall
{
public function __construct(Factory $factory, string $name)
{
$factory->connect();
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class RemoveUnusedAssignOfBothProperties
{
public function __construct(B $b)
{
new C($b);
}
}

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

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
Expand All @@ -18,6 +20,7 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\DeadCode\NodeAnalyzer\PropertyWriteonlyAnalyzer;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use Rector\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
Expand All @@ -32,6 +35,7 @@ public function __construct(
private readonly PropertyFetchFinder $propertyFetchFinder,
private readonly PropertyWriteonlyAnalyzer $propertyWriteonlyAnalyzer,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly SideEffectNodeDetector $sideEffectNodeDetector,
) {
}

Expand Down Expand Up @@ -173,7 +177,7 @@ private function removePropertyAssigns(Class_ $class, string $propertyName): voi
$this->traverseNodesWithCallable($class, function (Node $node) use (
$class,
$propertyName
): null|int|Return_|Arg {
): null|int|Return_|Arg|Expression {
if (! $node instanceof Expression && ! $node instanceof Return_) {
if ($node instanceof Arg && $node->value instanceof Assign) {
$assign = $node->value;
Expand All @@ -196,11 +200,27 @@ private function removePropertyAssigns(Class_ $class, string $propertyName): voi
}

if ($node instanceof Expression) {
// keep the side effect of the assigned value, e.g. a "new" or a call
$assignedExpr = $this->cleanCastedExpr($assign->expr);
if ($this->sideEffectNodeDetector->detect($assignedExpr)) {
$node->expr = $assignedExpr;
return $node;
}

return NodeVisitor::REMOVE_NODE;
}

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

private function cleanCastedExpr(Expr $expr): Expr
{
if (! $expr instanceof Cast) {
return $expr;
}

return $this->cleanCastedExpr($expr->expr);
}
}
Loading