Skip to content

Commit

Permalink
[DeadCode] Skip assign inside if cond on RemoveAlwaysTrueIfConditionR…
Browse files Browse the repository at this point in the history
…ector (#3447)

Co-authored-by: Naugrimm <Naugrimm@users.noreply.github.com>
  • Loading branch information
samsonasik and Naugrimm committed Mar 4, 2023
1 parent 658e48f commit b2e90e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector\Fixture;

final class SkipAssignInIfCond
{
public function run()
{
if (($value = "foo") !== null) {
echo $value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
Expand Down Expand Up @@ -103,6 +104,11 @@ public function refactor(Node $node): If_|null|array
return null;
}

$hasAssign = (bool) $this->betterNodeFinder->findFirstInstanceOf($node->cond, Assign::class);
if ($hasAssign) {
return null;
}

if ($node->stmts === []) {
$this->removeNode($node);
return null;
Expand Down

0 comments on commit b2e90e8

Please sign in to comment.