1.2.737. 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.737.1. Specs

Short name

Functions/MultipleReturn

Rulesets

All

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Slow (1 hour)

Precision

Very high

Features

return

Available in

Entreprise Edition, Exakat Cloud