diff --git a/rules-tests/DeadCode/Rector/Assign/RemoveDoubleAssignRector/Fixture/next_reassign_with_call_on_variable.php.inc b/rules-tests/DeadCode/Rector/Assign/RemoveDoubleAssignRector/Fixture/next_reassign_with_call_on_variable.php.inc new file mode 100644 index 00000000000..e91abff584c --- /dev/null +++ b/rules-tests/DeadCode/Rector/Assign/RemoveDoubleAssignRector/Fixture/next_reassign_with_call_on_variable.php.inc @@ -0,0 +1,42 @@ +getItems(); + } + + public function getItems() + { + return sort($this->items); + } +} + +?> +----- +getItems(); + } + + public function getItems() + { + return sort($this->items); + } +} + +?> diff --git a/rules-tests/DeadCode/Rector/Assign/RemoveDoubleAssignRector/Fixture/skip_next_reassign_with_call_on_property_fetch.php.inc b/rules-tests/DeadCode/Rector/Assign/RemoveDoubleAssignRector/Fixture/skip_next_reassign_with_call_on_property_fetch.php.inc new file mode 100644 index 00000000000..48ccb143a70 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Assign/RemoveDoubleAssignRector/Fixture/skip_next_reassign_with_call_on_property_fetch.php.inc @@ -0,0 +1,20 @@ +items = $input; + + $this->items = $this->getItems(); + } + + public function getItems() + { + return sort($this->items); + } +} \ No newline at end of file diff --git a/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php b/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php index def30b18b01..1b06f98baaa 100644 --- a/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php +++ b/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php @@ -6,7 +6,6 @@ use PhpParser\Node; use PhpParser\Node\Expr\Assign; -use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\Expr\Variable; @@ -104,7 +103,10 @@ public function refactor(Node $node): ?Node } // next stmts can have side effect as well - if ($nextAssign->expr instanceof MethodCall) { + if (($nextAssign->var instanceof PropertyFetch || $nextAssign->var instanceof StaticPropertyFetch) && $this->sideEffectNodeDetector->detectCallExpr( + $nextAssign->expr, + $scope + )) { continue; }