diff --git a/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php b/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php index a6eab21fcf7..cb423a5b2af 100644 --- a/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php +++ b/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php @@ -93,6 +93,11 @@ public function getNodeTypes(): array public function refactor(Node $node): Node|null { if ($node instanceof If_) { + // cheap structural bail before pulling the scope + if (! $this->ifManipulator->isIfWithOnly($node, Foreach_::class)) { + return null; + } + $scope = ScopeFetcher::fetch($node); return $this->refactorIf($node, $scope); }