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.
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!
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 ...
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) {}
}
rules:
E0021:
max_children: 15
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!