{
    "slug": "extract_class_refactor",
    "term": "Extract Class Refactoring",
    "category": "quality",
    "difficulty": "intermediate",
    "short": "Extract Class splits a large class into two — moving a cohesive subset of fields and methods into a new class that the original delegates to.",
    "long": "A class doing too much (God Class) should be split. Extract Class: identify a cohesive group of fields and methods (e.g., address-related fields in a User class), create a new class (Address), move the fields and methods, update the original class to hold a reference. Signs you need it: class has many unrelated fields, methods only use a subset of fields, natural groupings exist (User + Address, Order + PaymentDetails). In PHP: create the new class, inject it via constructor, update all references. Opposite of Inline Class.",
    "aliases": [],
    "tags": [
        "quality",
        "refactoring",
        "oop",
        "clean-code"
    ],
    "misconception": "Extract Class is only for God Classes — any class where you can identify a natural cohesive subset of responsibilities is a candidate.",
    "why_it_matters": "Extracted classes are smaller, more focused, independently testable, and reusable — reducing coupling and improving cohesion across the codebase.",
    "common_mistakes": [
        "Creating classes with only getters/setters (anaemic) — extract behaviour too, not just data.",
        "Not updating all references after extraction — leaving the old fields in place as duplicates.",
        "Extracting too aggressively — small classes with one field and one method add unnecessary indirection."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "god_class",
        "cyclomatic_reduction",
        "single_responsibility",
        "value_object"
    ],
    "prerequisites": [
        "single_responsibility",
        "value_object"
    ],
    "refs": [
        "https://refactoring.guru/extract-class"
    ],
    "bad_code": "class User {\n    public string $name;\n    public string $street;\n    public string $city;\n    public string $postcode;\n    public string $country;\n\n    public function formatAddress(): string {\n        return \"$this->street, $this->city\";\n    }\n}",
    "good_code": "class Address {\n    public function __construct(\n        public readonly string $street,\n        public readonly string $city,\n        public readonly string $postcode,\n        public readonly string $country,\n    ) {}\n\n    public function format(): string {\n        return \"$this->street, $this->city\";\n    }\n}\n\nclass User {\n    public function __construct(\n        public readonly string $name,\n        public readonly Address $address,\n    ) {}\n}",
    "quick_fix": "Identify cohesive field groups in large classes. Create a new class, move the fields and their methods, replace with a reference in the original.",
    "severity": "medium",
    "effort": "high",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/extract_class_refactor",
        "html_url": "https://codeclaritylab.com/glossary/extract_class_refactor",
        "json_url": "https://codeclaritylab.com/glossary/extract_class_refactor.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": "[Extract Class Refactoring](https://codeclaritylab.com/glossary/extract_class_refactor) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/extract_class_refactor"
            }
        }
    }
}