Skip to content

Incorrect behavior of SimplifyForeachToArrayFilterRector #7594

@Wohlie

Description

@Wohlie

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.org/demo/91353eb4-621a-42a9-89f2-a45f7605b514

<?php

class FilterDefaultValue
{
    public function filter(array $records, $defaultValue): array
    {
        $nonDefault = [];
        foreach ($records as $key => $value) {
            if ($defaultValue !== $value) {
                $nonDefault[$key] = $value;
            }
        }

        return $nonDefault;
    }
}

Responsible rules

  • SimplifyForeachToArrayFilterRector

Expected Behavior

The refactored code will work with PHP >= 7.4
https://3v4l.org/CHTtV

But for PHP 7.3, which don't support arrow function, the use statement for $defaultValue is missing
https://3v4l.org/1ORPl

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