Unreachable Code
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
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
5 Apr 2026
Views
34
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
Perplexity 7
Amazonbot 7
Google 6
ChatGPT 5
Unknown AI 2
Ahrefs 1
Bing 1
How they use it
crawler 27
crawler_json 2
Related categories
⚡
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