Skip to content

Commit

Permalink
[DeadCode] Allow remove assign variable with next method call on Remo…
Browse files Browse the repository at this point in the history
…veDoubleAssignRector (#6443)
  • Loading branch information
samsonasik authored Nov 16, 2024
1 parent e7800e9 commit c7dfbe7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

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

class NextReassignWithCallOnVariable
{
private $items;

public function create($input)
{
$items = $input;
$items = $this->getItems();
}

public function getItems()
{
return sort($this->items);
}
}

?>
-----
<?php

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

class NextReassignWithCallOnVariable
{
private $items;

public function create($input)
{
$items = $this->getItems();
}

public function getItems()
{
return sort($this->items);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

class SkipNextReassignWithCallOnPropertyFetch
{
private $items;

public function create($input)
{
$this->items = $input;

$this->items = $this->getItems();
}

public function getItems()
{
return sort($this->items);
}
}
6 changes: 4 additions & 2 deletions rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit c7dfbe7

Please sign in to comment.