mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 08:50:50 +00:00
[DeadCode] Clean casted CallLike in Assign Expr on RemoveUnusedVariableAssignRector (#1889)
Co-authored-by: Jack Leigh <leighman@users.noreply.github.com>
This commit is contained in:
parent
7c00e0ab4b
commit
0cac723079
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;
|
||||
|
||||
final class CleanCastAssignExprCallLike
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$response = (object) $this->api_post("whatever", []);
|
||||
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;
|
||||
|
||||
final class CleanCastAssignExprCallLike
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$this->api_post("whatever", []);
|
||||
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -8,6 +8,7 @@ use PhpParser\Node;
|
|||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Cast;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
|
@ -109,13 +110,23 @@ CODE_SAMPLE
|
|||
|
||||
if ($this->hasCallLikeInAssignExpr($node->expr)) {
|
||||
// keep the expr, can have side effect
|
||||
return $node->expr;
|
||||
return $this->cleanCastedExpr($node->expr);
|
||||
}
|
||||
|
||||
$this->removeNode($node);
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function cleanCastedExpr(Expr $expr): Expr
|
||||
{
|
||||
if (! $expr instanceof Cast) {
|
||||
return $expr;
|
||||
}
|
||||
|
||||
$castedExpr = $expr->expr;
|
||||
return $this->cleanCastedExpr($castedExpr);
|
||||
}
|
||||
|
||||
private function hasCallLikeInAssignExpr(Expr $expr): bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirst(
|
||||
|
|
Loading…
Reference in New Issue
Block a user