missing-return
What it does
Section titled “What it does”Reports a function or method whose native return type or @return tag is not void or never and whose end can be reached without a return or throw, on the last statement that reaches the end. Generators are not reported, and a call that never returns (exit, die, a @return never function) is not seen.
Why it matters
Section titled “Why it matters”The function runs off its end and returns null, which breaks the declared type: a native type gives a TypeError at run time, and a documented type gives the caller a null where it expects a value.
To fix, return a value on every path, or throw.
Example
Section titled “Example”<?phpfunction a($x): int { if ($x) { return 1; } }function b($x): int { if ($x) { return 1; } elseif ($x > 1) { return 2; } else { return 3; } }function c($x): int { if ($x): return 1; else: throw new E(); endif; }function d($x): int { if ($x) { return 1; } exit; }...function e($x): void { if ($x) { return; } }function f($x): iterable { if ($x) { yield 1; } }/** @return int */function g($x) { if ($x) { return 1; } }/** @return int|null */function h($x) { if ($x) { return 1; } }class K { function __construct() { } }Configuration
Section titled “Configuration”php: lint: rules: missing-return: warn # off (default), warn, errorSuppress one line with sudsy-disable-next-line missing-return in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.
| Tool | Rule | Match | What differs |
|---|---|---|---|
| phpstan | return.missing |
partial | A native or @return type that is not void, never, mixed or null and a reachable end of the body; exit and die end a path; a call of a @return never function in another file (bail(), abort()), a type narrowed to every case, and a loop with an inner break are not seen (27 extra against 19 covered, so the rule is not in the preset). |