rector icon indicating copy to clipboard operation
rector copied to clipboard

Improve behavior of RemoveAlwaysElseRector

Open Wohlie opened this issue 3 years ago • 2 comments

Feature Request

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

Minimal PHP Code Causing Issue

See https://getrector.org/demo/a35ac601-a5d2-4f20-a10e-afd6d1019bae

<?php

final class DemoFile
{
    public function run($a)
    {
        if (1 === $a) {
            return 'a';
        } elseif (2 === $a) {
            return 'b';
        } elseif (3 === $a) {
        	return 'c';
        } elseif (4 === $a) {
            return 'd';
        } elseif (5 === $a) {
            return 'e';
        } else {
            return 'f';
        }
    }
}

Responsible rules

  • RemoveAlwaysElseRector

Expected Behavior

Currently, the rule RemoveAlwaysElseRector must use multiple times to break down all elseif statements. It should be nice, the rule must only be applied once to get the finale result:

<?php

final class DemoFile
{
    public function run($a)
    {
        if (1 === $a) {
            return 'a';
        }

        if (2 === $a) {
            return 'b';
        }

        if (3 === $a) {
            return 'c';
        }

        if (4 === $a) {
            return 'd';
        }

        if (5 === $a) {
            return 'e';
        }

        return 'f';
    }
}

Wohlie avatar Sep 15 '22 08:09 Wohlie

I thought \Rector\Set\ValueObject\SetList::EARLY_RETURN might fix it but doesn't seem the case: https://getrector.org/demo/a20b850f-0af8-412f-baca-467c66120127

JohJohan avatar Sep 15 '22 13:09 JohJohan

Feel free to provide improvement PR. As far as I remember, it was problematic to verify with other early return rules, so multiple runs is expected to ensure it printed correctly and got scope filled in the independent If_ node.

Actually, if you have auto-commit action for run rector, it will apply until no change required.

samsonasik avatar Sep 16 '22 09:09 samsonasik