Skip to content

Commit

Permalink
[DeadCode] Skip key used in next stmt on RemoveUnusedForeachKeyRector (
Browse files Browse the repository at this point in the history
#5153)

* [DeadCode] Skip key used in next stmt on RemoveUnusedForeachKeyRector

* [DeadCode] Skip key used in next stmt on RemoveUnusedForeachKeyRector

* [ci-review] Rector Rectify

* continue

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Oct 10, 2023
1 parent 6cb63f3 commit 6d96068
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector\Fixture;

final class SkipKeyUsedInNexStmt
{
function lastKey ($container, $defKey = null) {
$result = $defKey;

foreach ($container as $result => $value) {
// next
}

return $result;
}
}
51 changes: 37 additions & 14 deletions rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php
Expand Up @@ -5,8 +5,10 @@
namespace Rector\DeadCode\Rector\Foreach_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Foreach_;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeManipulator\StmtsManipulator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -18,7 +20,8 @@
final class RemoveUnusedForeachKeyRector extends AbstractRector
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder
private readonly BetterNodeFinder $betterNodeFinder,
private readonly StmtsManipulator $stmtsManipulator
) {
}

Expand Down Expand Up @@ -48,31 +51,51 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [Foreach_::class];
return [StmtsAwareInterface::class];
}

/**
* @param Foreach_ $node
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->keyVar instanceof Expr) {
if ($node->stmts === null) {
return null;
}

$keyVar = $node->keyVar;
$hasChanged = false;
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Foreach_) {
continue;
}

$isNodeUsed = (bool) $this->betterNodeFinder->findFirst(
$node->stmts,
fn (Node $node): bool => $this->nodeComparator->areNodesEqual($node, $keyVar)
);
if (! $stmt->keyVar instanceof Variable) {
continue;
}

if ($isNodeUsed) {
return null;
$keyVar = $stmt->keyVar;

$isNodeUsed = (bool) $this->betterNodeFinder->findFirst(
$stmt->stmts,
fn (Node $node): bool => $this->nodeComparator->areNodesEqual($node, $keyVar)
);

if ($isNodeUsed) {
continue;
}

if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, (string) $this->getName($keyVar))) {
continue;
}

$stmt->keyVar = null;
$hasChanged = true;
}

$node->keyVar = null;
if ($hasChanged) {
return $node;
}

return $node;
return null;
}
}

0 comments on commit 6d96068

Please sign in to comment.