MultipleReturnStatements
warning off by defaultGroup: Architecture · PhpStorm name: MultipleReturnStatementsInspection
A method with many exit points is harder to follow than one with a single result path. The rule counts return statements per method and reports it above two configurable thresholds (a normal one and a severe one).
Example
Defaults (3 / 5):
php
<?php
class Grader {
public function letter(int $score) {
if ($score > 90) {
return 'A';
}
if ($score > 75) {
return 'B';
}
return 'C';
}
public function bucket(int $n) {
switch ($n) {
case 1: return 'one';
case 2: return 'two';
case 3: return 'three';
case 4: return 'four';
case 5: return 'five';
}
return 'many';
}
public function sorter() {
return function ($a, $b) {
if ($a < $b) { return -1; }
if ($a > $b) { return 1; }
return 0;
};
}
public function widget() {
return new class {
public function kind($v) {
if (is_int($v)) { return 'int'; }
if (is_string($v)) { return 'string'; }
return 'other';
}
};
}
}
function plain($x) {
if ($x) { return 1; }
if (!$x) { return 2; }
return 3;
}Reported:
- line 3: 3 return statements in this method; try to funnel them into a single exit.
- line 13: 6 return statements in this method; try to funnel them into a single exit.
- line 34: 3 return statements in this method; try to funnel them into a single exit.
Options
| Option | Type | Default | Effect |
|---|---|---|---|
COMPLAIN_THRESHOLD | int | 3 | Return count (inclusive) from which a warning is raised. |
SCREAM_THRESHOLD | int | 5 | Return count (inclusive) from which an error is raised instead. |
Configure
In custos.json:
json
{
"rules": {
"MultipleReturnStatements": {
"enabled": true,
"options": {
"COMPLAIN_THRESHOLD": 3,
"SCREAM_THRESHOLD": 5
}
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore MultipleReturnStatements
/**
* @noinspection MultipleReturnStatementsInspection
*/