phanalist

E0021: The “Too Many Kids” Rule (Number of Children - NOC)

Imagine a manager who has 2 direct reports. If the manager changes how they want status updates submitted, they only have to tell 2 people. It’s easy!

But what if the manager has 50 direct reports? If they change the status update rules, chaos ensues. Half the people will do it wrong, and it will take forever to fix.

Number of Children (NOC) measures how many classes directly extend your class.

How the rule works

We look at a class and count how many other classes say extends YourClass.

If a base class has too many children (default > 15), it becomes a “Fragile Base Class.” If you change one line of code in that base class, you might accidentally break 16 different subclasses at the same time!


❌ The “Too Many Kids” Example

If we change BaseController, we risk breaking 16 different parts of the application at once!

// VIOLATION! NOC is 16!
class BaseController {} 

class UserController extends BaseController {}
class PostController extends BaseController {}
class CommentController extends BaseController {}
// ... and 13 more controllers ...

✅ The “Delegation” Example

Instead of forcing everyone to inherit from one massive base class, provide small, focused services that controllers can use if they want to.

// No children!
class ResponseFactory {
    public function json($data) { /* ... */ }
}

class UserController {
    public function __construct(private ResponseFactory $response) {}
}

Configuration

rules:
  E0021:
    max_children: 15

The Junior’s Rule of Thumb:

If Phanalist flags your base class for NOC, it means your base class has become a god. Stop forcing every new class to inherit from it, and start injecting dependencies instead!