phanalist

E0027: The “Brain Class” Rule (God Class)

Imagine a company where one person knows everything: accounting, engineering, sales, HR, and IT. If that person gets sick, the whole company shuts down. A God Class (or Brain Class) is a class that centralizes too much logic — it has too many methods AND too many fields.

How the rule works

We count the total methods and properties in a class, trait, or enum. If both exceed their thresholds (default 15 methods AND 10 fields), it’s flagged as a potential God Class.


❌ The “Brain Class” Example

This class does everything — order management, user management, billing, reporting, inventory, notifications, caching, and logging.

class OrderManager
{
    private Database $db;
    private Cache $cache;
    private Logger $log;
    private Mailer $mail;
    // ... 7 more properties ...

    public function createOrder(): void { /* ... */ }
    public function updateOrder(): void { /* ... */ }
    public function cancelOrder(): void { /* ... */ }
    public function createUser(): void { /* ... */ }
    public function blockUser(): void { /* ... */ }
    public function generateReport(): void { /* ... */ }
    public function processRefund(): void { /* ... */ }
    public function sendInvoice(): void { /* ... */ }
    public function updateInventory(): void { /* ... */ }
    public function sendNotification(): void { /* ... */ }
    public function clearCache(): void { /* ... */ }
    public function writeAuditLog(): void { /* ... */ }
    // ... more methods ...
}

✅ The “Focused” Example

Each class has one job and does it well.

class OrderService
{
    public function __construct(
        private OrderRepository $orders,
        private BillingService $billing,
    ) {}

    public function create(Cart $cart): Order { /* ... */ }
    public function cancel(Order $order): void { /* ... */ }
}

class UserService
{
    public function __construct(
        private UserRepository $users,
    ) {}

    public function register(string $email): User { /* ... */ }
    public function block(User $user): void { /* ... */ }
}

Configuration

rules:
  E0027:
    max_methods: 15
    max_fields: 10

The Junior’s Rule of Thumb:

If Phanalist flags a class as a God Class, your class is trying to do everything itself. Split it into focused classes — each should have one area of responsibility and a name that reflects it.