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,13 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleAssignRector\Fixture;

final class SkipCallInAssignTarget
{
public function run($container)
{
$container->get(SomeService::class)->enabled = true;
$container->get(SomeService::class)->enabled = true;
$container->get(SomeService::class)->enabled = true;
}
}
9 changes: 9 additions & 0 deletions rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ public function refactor(Node $node): ?Node
continue;
}

// the assignment target itself may contain a call with side effects,
// e.g. $object->getService()->property = 1; — removing the first assign would skip the call
if ($this->betterNodeFinder->findFirst(
$stmt->expr->var,
fn (Node $subNode): bool => $this->sideEffectNodeDetector->detectCallExpr($subNode)
) instanceof Node) {
continue;
}

// next stmts can have side effect as well
if (($nextAssign->var instanceof PropertyFetch || $nextAssign->var instanceof StaticPropertyFetch) && $this->sideEffectNodeDetector->detectCallExpr(
$nextAssign->expr
Expand Down
Loading