From 344e96e7a51d5a2e33fae5b0e8606394a940ac03 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 10 Oct 2023 18:16:49 +0700 Subject: [PATCH 1/4] [DeadCode] Skip key used in next stmt on RemoveUnusedForeachKeyRector --- .../Foreach_/RemoveUnusedForeachKeyRector.php | 50 ++++++++++++++----- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php b/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php index ed2172fe2d1..9226d686915 100644 --- a/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php +++ b/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php @@ -6,7 +6,10 @@ 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; @@ -18,7 +21,8 @@ final class RemoveUnusedForeachKeyRector extends AbstractRector { public function __construct( - private readonly BetterNodeFinder $betterNodeFinder + private readonly BetterNodeFinder $betterNodeFinder, + private readonly StmtsManipulator $stmtsManipulator ) { } @@ -48,31 +52,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) { + return null; + } + + 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; } } From 2ec6c19c30e2bc75db7a4344d3551a2e369435ae Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 10 Oct 2023 18:16:53 +0700 Subject: [PATCH 2/4] [DeadCode] Skip key used in next stmt on RemoveUnusedForeachKeyRector --- .../Fixture/skip_key_used_in_next_stmt.php.inc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 rules-tests/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector/Fixture/skip_key_used_in_next_stmt.php.inc diff --git a/rules-tests/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector/Fixture/skip_key_used_in_next_stmt.php.inc b/rules-tests/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector/Fixture/skip_key_used_in_next_stmt.php.inc new file mode 100644 index 00000000000..d473d4dbb52 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector/Fixture/skip_key_used_in_next_stmt.php.inc @@ -0,0 +1,16 @@ + $value) { + // next + } + + return $result; + } +} From 05b80581e535a462d2fb348d1a68a0259d3500ce Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 10 Oct 2023 11:18:44 +0000 Subject: [PATCH 3/4] [ci-review] Rector Rectify --- rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php | 1 - 1 file changed, 1 deletion(-) diff --git a/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php b/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php index 9226d686915..58087fb30dc 100644 --- a/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php +++ b/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php @@ -5,7 +5,6 @@ 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; From 4f6d7a8dc3c6f6ffae2004d8f6e35fde5b0d3364 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 10 Oct 2023 18:19:22 +0700 Subject: [PATCH 4/4] continue --- rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php b/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php index 58087fb30dc..0d65fd1df47 100644 --- a/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php +++ b/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php @@ -81,7 +81,7 @@ public function refactor(Node $node): ?Node ); if ($isNodeUsed) { - return null; + continue; } if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, (string) $this->getName($keyVar))) {