← Home ← Codex ← DEBT ← Engine
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 it' (d5). The detection_hints list PHPStan, Psalm, and Rector — all specialist static analysis tools, not default linters or compilers. PHPStan can report unreachable code that should be caught by never, and flag functions that always throw without the never return type, but this requires running a dedicated SAST tool rather than a built-in compiler check or default linter.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix explicitly states: add `: never` return type to the function declaration. This is a single-line annotation change per function. Even if multiple helpers need updating, each fix is independently a one-liner with no cascading refactor required.

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

Closest to 'minimal commitment' (b1). The never return type is a localised annotation on individual functions that always throw or exit. It imposes no structural obligation on callers, no cross-cutting concern, and no architectural shape. Future maintainers are unaffected unless they modify the annotated function itself. The applies_to contexts (web, cli, queue-worker) are broad but the concept itself is a narrow type annotation.

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

Closest to 'notable trap' (t5). The misconception field explicitly identifies the canonical trap: developers conflate void and never, assuming void covers 'no useful return value' including throws/exits. The distinction — void returns (without a value) vs. never doesn't return at all — is documented and widely taught but still routinely confused by competent PHP developers moving from older versions. The common_mistakes confirm this is a well-known gotcha rather than a catastrophic or exotic surprise.

About DEBT scoring →

Also Known As

never type PHP never return PHP 8.1 never

TL;DR

Declares that a function never returns normally — it always throws an exception or calls exit(), helping static analysers prove code paths.

Explanation

The never return type (PHP 8.1) tells PHP and static analysers that a function will never reach its closing brace — it always throws, calls exit(), or enters an infinite loop. This enables static analysis tools to treat code after a call to such a function as unreachable, tightening type inference. Common uses: redirectAndExit(), throwNotFound(), and abort() helpers. Declaring a function as never that can actually return causes a TypeError at runtime.

Common Misconception

never and void return types are the same. void means the function returns without a value. never means the function never returns at all — it either throws an exception or calls exit(). This distinction helps static analysers prove code paths are unreachable.

Why It Matters

The never return type declares that a function always throws or exits — static analysers use it to correctly mark code after such calls as unreachable, eliminating false positive warnings.

Common Mistakes

  • Using void instead of never for functions that always throw — void means returns without a value, never means doesn't return.
  • Declaring never on a function that can sometimes return normally — PHP will throw a TypeError.
  • Not declaring never on redirect helpers — analysers think code after the redirect is reachable.

Code Examples

✗ Vulnerable
function redirect(string $url) { header('Location: ' . $url); exit; } // return type missing
✓ Fixed
function redirect(string $url): never { header('Location: ' . $url); exit; }

Added 15 Mar 2026
Edited 22 Mar 2026
Views 151
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 1 ping S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 2 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 2 pings T 0 pings W 1 ping T 0 pings F 0 pings S 2 pings S 2 pings M
Perplexity 1 Bing 1
Amazonbot 1 ChatGPT 1
Amazonbot 10 PetalBot 10 Perplexity 9 SEMrush 8 Ahrefs 7 Google 7 ChatGPT 6 Scrapy 6 Bing 4 Unknown AI 2 Applebot 2 Brave Search 2 Majestic 1 Meta AI 1 Twitter/X 1
crawler 73 crawler_json 3
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
PHP php A server-side scripting language that generates web pages and APIs — the code runs on the server, and only its output (usually HTML or JSON) reaches the browser.

PHP is often the first server-side language people meet, and understanding its execution model — script starts fresh on every request, no memory between requests — explains most of how the web backend works: sessions, databases, and caching all exist to bridge that per-request amnesia.

💡 Start with PHP 8.x, declare(strict_types=1), and PDO — skip any tutorial that mentions mysql_query().

Ask Codex about PHP →
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Add :never return type to functions that always throw an exception or call exit() — it tells PHPStan the code after a call to that function is unreachable, improving analysis quality
📦 Applies To
PHP 8.1+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Function that always throws without never return type; PHPStan reporting unreachable code that should be caught by never
Auto-detectable: ✓ Yes phpstan psalm rector
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Low ✓ Auto-fixable Fix: Low Context: Function


✓ schema.org compliant