← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

never Return Type (PHP 8.1)

PHP PHP 8.1+ Intermediate
debt(d5/e1/b1/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches' (d5). PHPStan and Psalm (listed in detection_hints.tools) can detect functions that always throw or exit but lack the never return type, enabling unreachable code detection. These are specialist static analysis tools, not default linters or compiler errors.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch' (e1). The quick_fix states simply adding 'never' as the return type declaration — this is a single-line change to the function signature with no refactoring required.

b1 Burden Structural debt — long-term weight of choosing wrong

Closest to 'minimal commitment' (b1). The never return type is a per-function declaration with no architectural reach. It applies to individual helper functions (redirect helpers, abort functions) and doesn't impose any ongoing maintenance tax on the codebase.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception explicitly states developers confuse never with void — a documented gotcha. void means 'returns without a value' while never means 'never returns at all'. This is a meaningful distinction that most PHP developers eventually learn, but it's not immediately obvious from the names alone.

About DEBT scoring →

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);
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 51
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 2 pings T 0 pings F 0 pings S 3 pings S 0 pings M 1 ping T 1 ping W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 1 ping M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Amazonbot 9 Perplexity 7 Scrapy 6 Ahrefs 4 Unknown AI 3 SEMrush 3 Claude 2 ChatGPT 2 Google 1 Bing 1 Meta AI 1 PetalBot 1
crawler 36 crawler_json 3 pre-tracking 1
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


✓ schema.org compliant