rector icon indicating copy to clipboard operation
rector copied to clipboard

earlyReturn confused by elseif

Open anibalsanchez opened this issue 1 year ago • 3 comments

Bug Report

Subject Details
Rector version v1.2.4

The earlyReturn dropped a whole code branch.

INPUT:

    protected function checkACL($area)
    {
        if (in_array(strtolower($area), ['false', '0', 'no', '403'])) {
            return false;
        }

        if (in_array(strtolower($area), ['true', '1', 'yes'])) {
            return true;
        } elseif (empty($area)) {
            return true;
        } else {
// ... A big block of code
        }
        return false;
    }

OUTPUT:

    protected function checkACL($area)
    {
        if (in_array(strtolower($area), ['false', '0', 'no', '403'])) {
            return false;
        }

        if (in_array(strtolower($area), ['true', '1', 'yes'])) {
            return true;
        } 

        return empty($area);
    }

Minimal PHP Code Causing Issue

php v8.3.6

Expected Behaviour

If the elseif can be refactored, the rector must do so to keep the functionality as precise as the original code. In this case, it seems that it assumed to be equivalent to return empty($area);.

If the elseif is too complex, rector must skip it.

anibalsanchez avatar Aug 26 '24 16:08 anibalsanchez

Thank you for your report!

We'll need an isolated failing demo link from: http://getrector.com/demo, that way we can reproduce the bug.

TomasVotruba avatar Aug 26 '24 18:08 TomasVotruba

I tried to create the isolated test case, but it didn't produce the same result.

anibalsanchez avatar Aug 27 '24 10:08 anibalsanchez

We're unable to provide fix without failing reproducer we can add to our test suite.

TomasVotruba avatar Aug 27 '24 10:08 TomasVotruba

closing as answered and needs reproducible code/config to make a fix.

samsonasik avatar Sep 01 '24 13:09 samsonasik