Skip to content

Commit

Permalink
[DeadCode] Skip RemoveUnusedVariableAssignRector on assign to dynamic…
Browse files Browse the repository at this point in the history
… variable (#1884)

Co-authored-by: Jack Leigh <leighman@users.noreply.github.com>
  • Loading branch information
samsonasik and leighman committed Mar 1, 2022
1 parent 2ad95b6 commit 10de06f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

final class SkipDynamicVariable
{
public function run($something)
{
foreach($something as $key => $mapped)
{
${$key . "_key"} = $mapped;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Rector\DeadCode\NodeAnalyzer\UsedVariableNameAnalyzer;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php74\Tokenizer\FollowedByCurlyBracketAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -38,7 +39,8 @@ public function __construct(
private readonly ConditionSearcher $conditionSearcher,
private readonly UsedVariableNameAnalyzer $usedVariableNameAnalyzer,
private readonly SideEffectNodeDetector $sideEffectNodeDetector,
private readonly ExprUsedInNextNodeAnalyzer $exprUsedInNextNodeAnalyzer
private readonly ExprUsedInNextNodeAnalyzer $exprUsedInNextNodeAnalyzer,
private readonly FollowedByCurlyBracketAnalyzer $followedByCurlyBracketAnalyzer
) {
}

Expand Down Expand Up @@ -135,7 +137,7 @@ private function shouldSkip(Assign $assign): bool
}

if (! $variable->name instanceof Variable) {
return false;
return $this->followedByCurlyBracketAnalyzer->isFollowed($this->file, $variable);
}

return (bool) $this->betterNodeFinder->findFirstNext(
Expand Down

0 comments on commit 10de06f

Please sign in to comment.