1.2.741. Multiple Returns¶
Functions and methods that have multiple return statement.
This makes it difficult to maintain : since the function may be short-circuited early, some later instruction may be omitted.
Ideally, guard clauses, which check if arguments are valid or not at the beginning of the method are the only exception to this rule. Currently, the engine doesn’t spot guard clauses.
<?php
function foo() {
// This is a guard clause, that checks arguments.
if ($a < 0) {
return false;
}
$b = 0;
for($i = 0; $i < $a; $i++) {
$b += bar($i);
}
return $b;
}
?>
See also Single Function Exit Point.
1.2.741.1. Connex PHP features¶
1.2.741.1.1. Specs¶
Short name |
Functions/MultipleReturn |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Slow (1 hour) |
Precision |
Very high |
Available in |