{
    "slug": "code_duplication_types",
    "term": "Types of Code Duplication (Clone vs Semantic)",
    "category": "quality",
    "difficulty": "intermediate",
    "short": "Not all duplication is equal — clone duplication (copy-paste) always warrants extraction, but semantic duplication (similar logic, different context) may be acceptable.",
    "long": "Clone duplication: identical or near-identical code — always extract. Semantic duplication: two code sections do similar things with different data or context — requires judgement. Rule of Three: tolerate one duplicate, refactor on the third occurrence. Types: Type 1 (exact clone), Type 2 (renamed variables), Type 3 (modified statements), Type 4 (same algorithm, different structure). Tools: phpcpd (PHP Copy/Paste Detector), jscpd, SonarQube. The wrong abstraction is worse than duplication — don't prematurely abstract code that merely looks similar but has diverging requirements.",
    "aliases": [],
    "tags": [
        "quality",
        "duplication",
        "refactoring",
        "dry"
    ],
    "misconception": "All duplication must be eliminated immediately — premature abstraction creates worse problems than duplication. Wait for the third occurrence before extracting.",
    "why_it_matters": "Clone duplication creates maintenance traps — fix a bug in one copy and forget the other. But wrong abstractions create coupling that's harder to undo than the original duplication.",
    "common_mistakes": [
        "Extracting two pieces of code that look similar but have different reasons to change — they'll diverge and the shared abstraction becomes a problem.",
        "Not using phpcpd/jscpd in CI to detect growing duplication.",
        "Applying Rule of Three too rigidly — sometimes the second occurrence warrants extraction if the pattern is clearly stable."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "extract_function",
        "extract_class_refactor",
        "kiss_principle",
        "single_responsibility"
    ],
    "prerequisites": [
        "dry_principle",
        "extract_method"
    ],
    "refs": [
        "https://phpmd.org/rules/design.html",
        "https://github.com/sebastianbergmann/phpcpd"
    ],
    "bad_code": "// Same validation logic in 3 controllers — copy-paste:\nif (strlen($name) < 2 || strlen($name) > 50) {\n    throw new ValidationException('Invalid name');\n}",
    "good_code": "// Extracted to validator on third occurrence:\nclass NameValidator {\n    public function validate(string $name): void {\n        if (strlen($name) < 2 || strlen($name) > 50) {\n            throw new ValidationException('Invalid name');\n        }\n    }\n}",
    "quick_fix": "Run phpcpd in CI. Extract on third occurrence. Use shared base classes, traits, or services for validated duplicate patterns. Be cautious — not all similar code should be unified.",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/code_duplication_types",
        "html_url": "https://codeclaritylab.com/glossary/code_duplication_types",
        "json_url": "https://codeclaritylab.com/glossary/code_duplication_types.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Types of Code Duplication (Clone vs Semantic)](https://codeclaritylab.com/glossary/code_duplication_types) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/code_duplication_types"
            }
        }
    }
}