Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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))) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this mean, the logic only works when the var usage is directly after the loop?

the initial problem also exists on e.g.

<?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
        }

       $x = 1;
       $y = 2;

        return $result;
      }
}

Copy link
Copy Markdown
Member Author

@samsonasik samsonasik Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean no, that checked with array_slice() :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is check all next stmts:

        $stmts = array_slice($stmtsAware->stmts, $jumpToKey, null, true);
        return (bool) $this->betterNodeFinder->findVariableOfName($stmts, $variableName);

continue;
}

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

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

return $node;
return null;
}
}