Skip to content

ForeachToArray* rules are breaking code which reused foreach variables #9236

@VincentLanglet

Description

@VincentLanglet

Bug Report

Subject Details
Rector version 2.1.0

I know it's not the best written code, but just wanted to report that if the developper has reused the foreach variable later, all the ForeachToArray* rector are breaking the code.

Minimal PHP Code Causing Issue

https://getrector.com/demo/b44622d0-2a83-49f9-88a4-6cc22bef9312

https://onlinephp.io/c/445115
vs
https://onlinephp.io/c/76d65

The echo is not executed anymore.

Expected Behaviour

If the foreach variables are used later in the code, the foreach shouldn't be replaced ; that works for

  • ForeachToArrayFindRector
  • ForeachToArrayAllRector
  • ForeachToArrayAnyRector
  • ForeachToArrayFindKeyRector

with both key and value.

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