{
    "slug": "unused_function",
    "term": "Unused Function",
    "category": "quality",
    "difficulty": "beginner",
    "short": "A function or method that is defined but never called — dead code that increases maintenance burden and confuses readers about what is part of the active API.",
    "long": "Unused private methods are detectable by static analysis and have no legitimate use — remove them. Unused public methods are harder because they may be called dynamically or from outside the codebase, but any that are confirmed unused should also be removed. Dead methods stay in codebases because developers are afraid to delete them ('someone might need it') — version control is the safety net; delete confidently. YAGNI applies: You Aren't Gonna Need It.",
    "aliases": [
        "dead method",
        "unused method",
        "unreferenced function"
    ],
    "tags": [
        "quality",
        "php",
        "yagni",
        "clean-code"
    ],
    "misconception": "Keeping unused methods is safe since they do not execute — they add noise, misdirect readers, and may be accidentally called via dynamic dispatch or reflection.",
    "why_it_matters": "Unused functions create false impressions of functionality — a reader spends time understanding code that is never used, or a developer 'discovers' the dead method and wires it in incorrectly.",
    "common_mistakes": [
        "Keeping methods 'just in case' — git history preserves them; remove now, restore if ever needed.",
        "Not running dead code detection tools — PHPStan detects unused private methods automatically.",
        "Unused abstract method implementations left after an interface changed.",
        "Helper functions added during development that became unnecessary after refactoring."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "dead_code",
        "yagni",
        "unused_variable",
        "static_analysis"
    ],
    "prerequisites": [
        "dead_code",
        "static_analysis",
        "refactoring"
    ],
    "refs": [
        "https://phpstan.org/blog/detecting-unused-private-properties-methods-constants"
    ],
    "bad_code": "class OrderService {\n    public function processOrder(Order $o): void { /* used */ }\n\n    private function formatOrderLegacy(Order $o): string {\n        // Never called anywhere — dead code\n        return $o->id . '-' . $o->total;\n    }\n\n    private function validateOrderV1(array $data): bool {\n        // Replaced by validateOrder() — never called\n        return isset($data['id']);\n    }\n}",
    "good_code": "// Remove unused private methods — git preserves the history:\nclass OrderService {\n    public function processOrder(Order $o): void { /* used */ }\n    // formatOrderLegacy() removed — was never called\n    // validateOrderV1() removed — replaced by validateOrder()\n}",
    "quick_fix": "Install phpstan/phpstan-dead-code and run at level 6+ — it finds private/protected methods never called; public methods require more careful analysis as they may be called via reflection or from tests",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/unused_function",
        "html_url": "https://codeclaritylab.com/glossary/unused_function",
        "json_url": "https://codeclaritylab.com/glossary/unused_function.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": "[Unused Function](https://codeclaritylab.com/glossary/unused_function) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/unused_function"
            }
        }
    }
}