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

Unreachable Code

Code Quality PHP 5.0+ Beginner
debt(d5/e1/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5), because detection_hints.tools lists phpstan, psalm, and rector — all specialist static analysis tools requiring explicit configuration (e.g., PHPStan level 4+). It is not caught by the compiler or a default linter, but it is reliably caught once these tools are enabled.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix confirms enabling PHPStan level 4+ surfaces the issues, and common_mistakes show the fixes are local: move cleanup code before return, remove code after throw, delete dead else blocks. Each instance is a small, local edit.

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

Closest to 'localised tax' (b3). Unreachable code is a per-function quality issue — it doesn't spread architectural gravity across the codebase. It applies broadly (web, cli, queue-worker) but each instance is self-contained, imposing maintenance friction only in the affected file rather than shaping the whole system.

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

Closest to 'serious trap' (t7). The misconception field states developers believe unreachable code after a return is 'harmless' when it actually signals a logic error in what was supposed to run before the return. This contradicts intuition: code is silently skipped rather than causing any obvious failure, meaning the real bug (wrong placement of logic) is hidden rather than surfaced.

About DEBT scoring →

Also Known As

dead code after return code after throw unreachable statement

TL;DR

Code that can never execute because it follows a return, throw, exit, or an always-true condition — a sign of logic errors or forgotten cleanup.

Explanation

Unreachable code appears after unconditional return/throw/exit/break/continue, inside dead conditions, or in catch blocks that can never be triggered. It is always a defect: either the code was meant to execute (logic error) or it is leftover debris from refactoring. PHPStan and Psalm report unreachable statements. The never return type in PHP 8.1 lets static analysers know that certain functions never return, improving detection.

Common Misconception

Unreachable code after a return is harmless — it indicates a logic error in what was intended to run before the return, not just wasted lines.

Why It Matters

Code after a return that was meant to execute (a missing conditional) silently does nothing — unreachable code hides bugs rather than causing obvious failures.

Common Mistakes

  • Placing cleanup code after return instead of before it.
  • Adding code after a throw in an exception handler — the throw terminates the current path.
  • A catch block for an exception the try block cannot throw — wastes maintenance effort.
  • Early return refactoring that leaves else blocks unreachable but visually present.

Code Examples

✗ Vulnerable
// Code after return — never executes:
function processOrder(Order $order): string {
    return 'processed';
    $this->audit->log($order); // Never runs — silently skipped!
    $order->markComplete();    // Never runs
}

// Unreachable catch:
try {
    $result = purePHPCalculation($x); // Cannot throw
} catch (DatabaseException $e) { // Unreachable
    handleError($e);
}
✓ Fixed
// Correct order — execution before return:
function processOrder(Order $order): string {
    $this->audit->log($order); // Runs first
    $order->markComplete();    // Runs second
    return 'processed';        // Returns last
}

// Remove unreachable catch:
$result = purePHPCalculation($x); // No try needed

Added 16 Mar 2026
Edited 5 Apr 2026
Views 67
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 2 pings M 0 pings T 4 pings W 1 ping T 1 ping F 1 ping S 3 pings S 0 pings M 2 pings T 2 pings W 1 ping 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 2 pings M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Scrapy 11 Amazonbot 8 Google 7 Perplexity 7 ChatGPT 5 Ahrefs 4 SEMrush 4 Bing 3 Unknown AI 2 Claude 1 Meta AI 1 Common Crawl 1 PetalBot 1
crawler 51 crawler_json 4
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Enable PHPStan level 4+ to catch unreachable code after return, throw, and exit statements — it's a sign the logic was wrong when the code was written
📦 Applies To
PHP 5.0+ any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Code after return or throw statement; else after if with return; unreachable case in switch after another case returns
Auto-detectable: ✓ Yes phpstan psalm rector
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Function
CWE-561


✓ schema.org compliant