Skip to content

Incorrect behavior of RemoveUnusedNonEmptyArrayBeforeForeachRector #7486

@Wohlie

Description

@Wohlie

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.org/demo/e30d69ac-ec54-4bcd-b58b-78e14c498bfd

<?php

class A
{
    /**
     * @return array|null
     * @throws Exception
     */
    public function getTierPrices()
    {
        return random_int(0, 1) ? [] : null;
    }
}

class B
{
    public function getPrice(A $product): float
    {
        $tierPrices = $product->getTierPrices();
        if (!empty($tierPrices)) {
            foreach ($tierPrices as $tierPrice) {
                if ($tierPrice >= 0) {
                    return $tierPrice;
                }
            }
        }

        return 0.0;
    }
}

Responsible rules

  • RemoveUnusedNonEmptyArrayBeforeForeachRector

Expected Behavior

The method getTierPrices return an array or null. This means the if statement can't be removed.

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