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

Duplicate Code

quality PHP 5.0+ Beginner
debt(d5/e3/b5/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list phpcpd, phpcs, and rector — all specialist/dedicated tools that must be explicitly added to the workflow. Duplicate code is not caught by the compiler or a default linter out of the box; it requires running a dedicated copy-paste detector like phpcpd.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix states: extract the duplicated block into a shared method, trait, or parent class, using a parameter for slight variations. This is a focused refactor within one component or class hierarchy — more than a single-line swap but not a cross-file architectural change.

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

Closest to 'persistent productivity tax' (b5). Duplicate code applies across web, cli, and queue-worker contexts and spreads across multiple files over time. Every future bug fix or rule change must be applied in multiple places, slowing down many work streams. It isn't quite load-bearing across the whole architecture (b7), but it consistently taxes maintainers.

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

Closest to 'notable trap' (t5). The misconception field captures a documented gotcha: developers assume extracting all duplicate code into a shared function always improves design, but two identical snippets in unrelated modules may represent different concepts that will diverge — forcing them together creates hidden coupling. This is a well-known DRY misapplication that many developers eventually learn.

About DEBT scoring →

Also Known As

copy-paste code code duplication cloned code

TL;DR

Identical or near-identical blocks of code in multiple places — the most common source of maintenance bugs.

Explanation

When the same logic appears in multiple places, a bug fix or requirement change must be applied everywhere it exists — and inevitably one copy is missed. Duplicate code also makes understanding a codebase harder because readers must verify that two similar-looking blocks are truly identical in behaviour. The fix is to extract the duplicated logic into a named function or method, then call it from all the original sites.

Common Misconception

Extracting all duplicate code into a shared function always improves design. Two identical snippets in unrelated modules may represent different concepts that happen to look alike today but will diverge — forcing them together creates hidden coupling.

Why It Matters

Duplicate code means a bug fix or rule change must be made in multiple places — one copy always gets missed, causing inconsistent behaviour and maintenance debt that compounds with every new duplication.

Common Mistakes

  • Copy-pasting a code block with minor variations instead of extracting a parameterised function.
  • Duplicate validation logic in both the model and the controller — they diverge over time.
  • Near-duplicate switch statements for related concepts — use a strategy pattern or data-driven lookup.
  • Not using array_map/array_filter/array_reduce and writing the same loop logic in multiple places.

Code Examples

✗ Vulnerable
// In UserController
$user = User::find($id);
if (!$user) { abort(404); }

// In PostController (exact copy)
$user = User::find($id);
if (!$user) { abort(404); }
✓ Fixed
// Extracted helper or base controller method
public function findOrFail(int $id): User {
    return User::find($id) ?? abort(404);
}
// Or use Eloquent's built-in:
$user = User::findOrFail($id);

Added 15 Mar 2026
Edited 22 Mar 2026
Views 28
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 1 ping S 1 ping S 2 pings M 0 pings T 1 ping W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S
No pings yet today
Perplexity 9 Amazonbot 7 Ahrefs 5 Majestic 1 SEMrush 1 Google 1
crawler 23 crawler_json 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Extract the duplicated block into a shared method, trait, or parent class; if context differs slightly use a parameter to handle the variation
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Identical or near-identical code blocks appearing in 2+ places; phpcpd detects copy-paste
Auto-detectable: ✓ Yes phpcs phpcpd rector
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: High Context: File Tests: Update

✓ schema.org compliant