Skip to content

Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector too eager #3333

@HenkPoley

Description

@HenkPoley

Bug Report

Subject Details
Rector version Rector v0.7.22
Installed as composer dependency

Complex if-else if block is reduced to something too simple.

'else if' could be handled similar to 'elseif'

Minimal PHP Code Causing Issue

https://getrector.org/demo/e02bb6b3-6008-4554-927e-695821918b6c

Notice how $anotherObject is missing from the result

Notice how if you write else if as elseif it does the correct thing.

Expected Behaviour

Code should be processed while keeping this view of it in mind:

        if ($someName && $someObject->someName == $someName) {
            return true;
        }

        if (!$someName && !$funnel->someName) {
            return true;
        }

        if (!$someName && $anotherObject->someName == $funnel->someName) {
            return true;
        }

        return false;

I guess another way is to merge the if (!$someName):

        if ($someName && $someObject->someName == $someName) {
            return true;
        }

        if(!$someName) {
            if (!$funnel->someName) {
                return true;
            }

            if ($anotherObject->someName == $funnel->someName) {
                return true;
            }
        }
        
        return false;

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