1.2.383. 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.383.1. Connex PHP features¶
1.2.383.1.1. Suggestions¶
Remove the else clause and move its code to the main part of the method
1.2.383.1.2. Specs¶
Short name |
Structures/DropElseAfterReturn |
Rulesets |
|
Exakat since |
0.8.6 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Available in |