never Return Type (PHP 8.1)
Also Known As
never return type PHP
PHP never
always-throw function
TL;DR
Declaring a function's return type as never signals it always throws or exits — enabling static analysers to prune unreachable code branches.
Explanation
The never return type (PHP 8.1) declares that a function will never return normally — it always throws an exception, calls exit(), or enters an infinite loop. This is stricter than void (which returns, just without a value). Static analysers use never to mark code after a call as unreachable and to verify that all branches of a conditional throw rather than fall through. Practical uses: exception factory helpers (throw new InvalidArgumentException(...) extracted into a method), router 404 handlers that always abort(), and test assertion helpers that always throw on failure. A function declared never that actually returns causes a TypeError at runtime.
Common Misconception
✗ The never return type is the same as void. void functions return normally with no value. never functions never return at all — they always throw or exit. Static analysers use never to prove unreachability of code after such calls.
Why It Matters
The never return type declares that a function will never return normally — it must throw an exception or call exit(). Static analysers use it to prune unreachable code paths after such calls.
Common Mistakes
- Not using never on redirect-and-exit helpers — analysers cannot tell that code after the call is unreachable.
- Using void instead of never — void means the function returns without a value; never means it doesn't return at all.
- Declaring never on a function that can sometimes return normally — this is a type error PHP will enforce.
- Not understanding that never makes the function a dead end in control flow — useful for abort, throw helpers.
Code Examples
✗ Vulnerable
// Without 'never' — analyser doesn't know redirect() exits:
function redirect(string $url): void { // Should be: never
header('Location: ' . $url);
exit();
}
redirect('/login');
$user = getCurrentUser(); // Analyser flags this as reachable — false positive
// With 'never', analyser correctly marks this as unreachable
✓ Fixed
function abort(int $code): never {
http_response_code($code);
exit();
}
function fail(string $msg): never {
throw new RuntimeException($msg);
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
27
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
Amazonbot 9
Perplexity 7
Unknown AI 3
Ahrefs 1
Google 1
Also referenced
How they use it
crawler 20
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Use 'never' as the return type for functions that always throw or always call exit/die — it signals to static analysis that code after the call is unreachable
📦 Applies To
PHP 8.1+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
Function that always throws exception or calls exit() without never return type — PHPStan cannot detect unreachable code
Auto-detectable:
✓ Yes
phpstan
psalm
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Low
✓ Auto-fixable
Fix: Low
Context: Function