Skip to content

ForeachToArrayAnyRector breaks code with variable extraction #9339

@uncaught

Description

@uncaught

Bug Report

Subject Details
Rector version 2.1.4

ForeachToArrayAnyRector refactors a foreach which extracts its item value. It looks like it doesn't know what to do then. The code is broken.

Minimal PHP Code Causing Issue

  private function hasDependencyType(array $dependencies, string $type): bool {
-    /* @noinspection PhpLoopCanBeReplacedWithStdFunctionCallsInspection */
-    foreach ($dependencies as ['type' => $currentType]) {
-      if ($currentType === $type) {
-        return true;
-      }
-    }
-
-    return false;
+ return array_any($dependencies, fn() => $currentType === $type);
  }

Expected Behaviour

Either leave the code or correctly refactor it.

A correct version would look like:

  private function hasDependencyType(array $dependencies, string $type): bool {
    return array_any($dependencies, fn(array $dep) => $dep['type'] === $type);
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions