From 04a885f2cf4ac0f859f96027d7f5d4022a13cb8a Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 23 Nov 2026 22:11:00 +0100 Subject: [PATCH] [DeadCode] Keep side effects of assigned value in RemoveUnusedPrivatePropertyRector --- .../keep_side_effect_of_assigned_call.php.inc | 32 +++++++++++++++++++ ...e_unused_assign_of_both_properties.php.inc | 1 + .../RemoveUnusedPrivatePropertyRector.php | 22 ++++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 rules-tests/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/keep_side_effect_of_assigned_call.php.inc diff --git a/rules-tests/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/keep_side_effect_of_assigned_call.php.inc b/rules-tests/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/keep_side_effect_of_assigned_call.php.inc new file mode 100644 index 00000000000..be4e15df507 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/keep_side_effect_of_assigned_call.php.inc @@ -0,0 +1,32 @@ +connection = (object) $factory->connect(); + $this->name = $name; + } +} + +?> +----- +connect(); + } +} + +?> diff --git a/rules-tests/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/remove_unused_assign_of_both_properties.php.inc b/rules-tests/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/remove_unused_assign_of_both_properties.php.inc index 6e719450806..71210043399 100644 --- a/rules-tests/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/remove_unused_assign_of_both_properties.php.inc +++ b/rules-tests/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture/remove_unused_assign_of_both_properties.php.inc @@ -24,6 +24,7 @@ final class RemoveUnusedAssignOfBothProperties { public function __construct(B $b) { + new C($b); } } diff --git a/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php b/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php index 88adce14513..566b99fe24e 100644 --- a/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php +++ b/rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php @@ -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; @@ -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; @@ -32,6 +35,7 @@ public function __construct( private readonly PropertyFetchFinder $propertyFetchFinder, private readonly PropertyWriteonlyAnalyzer $propertyWriteonlyAnalyzer, private readonly PhpDocInfoFactory $phpDocInfoFactory, + private readonly SideEffectNodeDetector $sideEffectNodeDetector, ) { } @@ -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; @@ -196,6 +200,13 @@ 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; } @@ -203,4 +214,13 @@ private function removePropertyAssigns(Class_ $class, string $propertyName): voi return $node; }); } + + private function cleanCastedExpr(Expr $expr): Expr + { + if (! $expr instanceof Cast) { + return $expr; + } + + return $this->cleanCastedExpr($expr->expr); + } }