Phpinspectionsea: [false-positive] Redudant else keyword

Created on 6 Mar 2018  ·  7Comments  ·  Source: kalessil/phpinspectionsea

Given code:

    private function deadline($rawDeadline): DateTimeImmutable
    {
        if ($rawDeadline instanceof DateTimeImmutable) {
            return $rawDeadline;
        } elseif ($rawDeadline instanceof DateTime) {
            return DateTimeImmutable::createFromMutable($rawDeadline);
        }

        $deadline = DateTimeImmutable::createFromFormat('d.m.Y|', $rawDeadline);
        if (!$deadline) {
            throw new WrongInput("Дата $rawDeadline не соответствует формату дд.мм.гггг.");
        }

        return $deadline;
    }

Expected no inspection violation here

screenshot_20180306_203339

PhpStorm 2018.1 EAP
Build #PS-181.3986.12, built on February 28, 2018
PhpStorm EAP User
Expiration date: March 30, 2018
JRE: 1.8.0_152-release-1136-b16 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.15.0-1-amd64

PHP EA version 2.3.18

enhancement fixed

Most helpful comment

Thats not a false-positive. It suggests you to write this instead:

    private function deadline($rawDeadline): DateTimeImmutable
    {
        if ($rawDeadline instanceof DateTimeImmutable) {
            return $rawDeadline;
        } 

        if ($rawDeadline instanceof DateTime) {
            return DateTimeImmutable::createFromMutable($rawDeadline);
        }

        $deadline = DateTimeImmutable::createFromFormat('d.m.Y|', $rawDeadline);
        if (!$deadline) {
            throw new WrongInput("Дата $rawDeadline не соответствует формату дд.мм.гггг.");
        }

        return $deadline;
    }

Because of the return in the first if statement there's no need for an else (or elseif, in this case).

All 7 comments

Thats not a false-positive. It suggests you to write this instead:

    private function deadline($rawDeadline): DateTimeImmutable
    {
        if ($rawDeadline instanceof DateTimeImmutable) {
            return $rawDeadline;
        } 

        if ($rawDeadline instanceof DateTime) {
            return DateTimeImmutable::createFromMutable($rawDeadline);
        }

        $deadline = DateTimeImmutable::createFromFormat('d.m.Y|', $rawDeadline);
        if (!$deadline) {
            throw new WrongInput("Дата $rawDeadline не соответствует формату дд.мм.гггг.");
        }

        return $deadline;
    }

Because of the return in the first if statement there's no need for an else (or elseif, in this case).

It is not wrong. Try to change else if() with if() and you will have the same behavior here.

Yep, thank you for note about code, but inspection suggest to remove whole elseif left it with return and this is confusing at least. 2 ifs vs if with elseif is matter of codestyle, there is no error, it is valid code. And transformation suggested by inspection is misleading in this case, dont you think so?

Oh, right. I think that the message could be improved in that case to something like "else if could be replaced by if". The current message is a bit confuses, really.

PS. The QF for that case is working as expected (it replaces else if with if).

Understood, I'll update the message.

Thank you for quick replies, guys, it is amazing.

I updated messages, suppose to be less confusing =)

Was this page helpful?
0 / 5 - 0 ratings