Skip to content

Incorrect behavior of SimplifyForeachToArrayFilterRector #7570

@P3gucka

Description

@P3gucka

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.org/demo/e6253027-77ca-4230-97ee-7eaa5740ac01

<?php

final class DemoFile
{
    public function run(array $params)
    {
        $result = [];
        foreach ($params as $key => $value) {
            if (preg_match('/reg/', $key) > 0) {
                $result[$key] = $value;
            }
        }

        return $result;
    }
}

Responsible rules

  • SimplifyForeachToArrayFilterRector

  • SimplifyUselessVariableRector

  • RemoveDoubleAssignRector

Expected Behavior

<?php

final class DemoFile
{
    public function run(array $params)
    {
        return array_filter($params, fn($key) => preg_match('/reg/', $key) > 0, ARRAY_FILTER_USE_KEY);
    }
}

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