1.2.381. Drop Else After Return

Avoid else clause when the then clause returns, but not the else. And vice-versa.

This way, the else block disappears, and is now the main sequence of the function.

This is also true if else has a return, and then not. When doing so, don’t forget to reverse the condition.

<?php

// drop the else
if ($a) {
    return $a;
} else {
    doSomething();
}

// drop the then
if ($b) {
    doSomething();
} else {
    return $a;
}

// return in else and then
if ($a3) {
    return $a;
} else {
    $b = doSomething();
    return $b;
}

?>

1.2.381.1. Suggestions

  • Remove the else clause and move its code to the main part of the method

1.2.381.2. Specs

Short name

Structures/DropElseAfterReturn

Rulesets

All, Analyze, CE, CI-checks, Suggestions

Exakat since

0.8.6

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

return

Available in

Entreprise Edition, Community Edition, Exakat Cloud