Skip to content

missing-return

PHPcorrectnessno fixdefault: off

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.

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.

Reported
<?php
function 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() { } }
php:
lint:
rules:
missing-return: warn # off (default), warn, error

Suppress one line with sudsy-disable-next-line missing-return in a comment of the file’s language.

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).