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

Extract Class Refactoring

quality Intermediate

TL;DR

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.

Explanation

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.

Common 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.

Code Examples

✗ Vulnerable
class User {
    public string $name;
    public string $street;
    public string $city;
    public string $postcode;
    public string $country;

    public function formatAddress(): string {
        return "$this->street, $this->city";
    }
}
✓ Fixed
class Address {
    public function __construct(
        public readonly string $street,
        public readonly string $city,
        public readonly string $postcode,
        public readonly string $country,
    ) {}

    public function format(): string {
        return "$this->street, $this->city";
    }
}

class User {
    public function __construct(
        public readonly string $name,
        public readonly Address $address,
    ) {}
}

Added 23 Mar 2026
Views 22
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
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 0 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 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 6 Unknown AI 3 Perplexity 3 Meta AI 2 Google 2 ChatGPT 1 Majestic 1 Ahrefs 1
crawler 17 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: High
⚡ 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.
📦 Applies To
web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
class.*{[\s\S]{2000,}}
Auto-detectable: ✗ No phpmd sonarqube
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: Class Tests: Update

✓ schema.org compliant