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

Unreachable Code

quality PHP 5.0+ Beginner

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 34
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 4 pings M 0 pings T 0 pings W 3 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 4 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S
Perplexity 7 Amazonbot 7 Google 6 ChatGPT 5 Unknown AI 2 Ahrefs 1 Bing 1
crawler 27 crawler_json 2
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