Phpinspectionsea: [FR] If-return-return could be simplified: new patterns

Created on 17 Jan 2019  路  4Comments  路  Source: kalessil/phpinspectionsea

| Subject | Details |
| :------------- | :---------------------------------------------------------------------------- |
| Issue type | Feature request |
| Plugin | Php Inspections (EA Extended) |

Expected behaviour

I would like to propose new patterns for the "If-return-return could be simplified" inspection.

For all examples, we will suppose a function strictly_return_bool() which return only booleans. Note that it can be replaced with any statement that is strictly a boolean.

1)

function foo() {
    if(!strictly_return_bool()) {
        return true;
    }
    else{
        return false;
    }
    //can be replaced with return !strictly_return_bool();
}

2)

function foo() {
    $result = false;
    if(!strictly_return_bool()) {
        return true;
    }
    return $result;
    //can be replaced with return !strictly_return_bool();
}

3)

function foo() {
    $result = false;
    if(!strictly_return_bool()) {
        $result = true;
    }
    return $result;
    //can be replaced with return !strictly_return_bool();
}

If the nature of the bool statement is not a function call, it may be needed to add parentheses around the statement.
If the if and else are reversed, for example:

function foo() {
    if(!strictly_return_bool()) {
        return true;
    }
    else{
        return false;
    }
}

We will have toinverse the boolean in the return:

return !(!strictly_return_bool());

If the statement was already inversed, it would be nice to neutralize both "!" and just return strictly_return_bool()

Thanks

enhancement fixed

Most helpful comment

Looks like it was a lot of work, thanks a lot!

All 4 comments

Looks good, marking for backlog due to high bug-rate.

Implemented!

Looks like it was a lot of work, thanks a lot!

A little bit =)

Was this page helpful?
0 / 5 - 0 ratings