Code Smell
debt(d3/e5/b5/t5)
Closest to 'default linter catches the common case' (d3). The term lists phpmd, phpcs, phpstan, rector, and phpcpd as detection tools with automated=yes. These are standard PHP quality tools that catch common code smells like long methods, duplicated code, and large classes without requiring specialist configuration.
Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix lists a catalogue of smells (god class, duplicate code, feature envy, etc.) — addressing these typically requires restructuring methods, extracting classes, or consolidating duplicated logic. This isn't a one-line fix; it's meaningful refactoring work within components.
Closest to 'persistent productivity tax' (b5). Code smells apply across all contexts (web, cli, queue-worker) and represent accumulated design debt. The term states 'ignoring them leads to systems where every change risks breaking something else' — this is a persistent drag on velocity across multiple work streams, though not quite system-defining.
Closest to 'notable trap' (t5). The misconception field explicitly states the trap: 'A code smell means the code is definitely wrong and must be fixed immediately.' Many developers treat smells as bugs rather than signals requiring investigation. The common_mistakes reinforce this — treating all smells as equally urgent, refactoring without tests, conflating style with structure.
Also Known As
TL;DR
Explanation
The term was popularised by Martin Fowler in "Refactoring". A code smell is not necessarily broken — the code may work — but it suggests structural issues that will make the code harder to change safely over time. Common smells: long methods, large classes, long parameter lists, feature envy, data clumps, primitive obsession, and duplicate code. Smells are signals to investigate, not mandates to refactor immediately.
Common Misconception
Why It Matters
Common Mistakes
- Treating all code smells as equally urgent — prioritise smells in frequently-changed, business-critical code.
- Refactoring smells without tests in place — you need a safety net before restructuring.
- Conflating code style violations (formatting) with structural smells — they require different remediation.
- Using smell detection tools as a pass/fail gate rather than a signal to investigate.
Code Examples
// Multiple smells in one function:
function p($d) { // cryptic name
global $db; // global state
if($d['t']==1) { // magic number
$r = $db->query("SELECT * FROM orders WHERE uid=".$d['u']); // SQL injection
foreach($r as $row) { echo $row['total'] * 0.9; } // magic number
}
}
function applyLoyaltyDiscount(array $request): void {
$orders = $this->orderRepo->findByUserId($request['user_id']);
$discount = LoyaltyDiscount::RATE; // named constant
foreach ($orders as $order) {
echo $order->total * (1 - $discount);
}
}