Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions config/extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,4 @@ services:
-
factory: Orrison\MeliorStan\Rules\NumberOfChildren\Config
arguments:
- %meliorstan.number_of_children.maximum%
-
class: Orrison\MeliorStan\Rules\NumberOfChildren\NumberOfChildrenCollector
tags:
- phpstan.collector
- %meliorstan.number_of_children.maximum%
9 changes: 8 additions & 1 deletion docs/NumberOfChildren.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This rule supports the following configuration option:

## Usage

Add the rule to your PHPStan configuration:
This rule relies on a PHPStan collector to gather inheritance information, so you must register both the rule and the collector when enabling it. Add the following to your PHPStan configuration:

```neon
includes:
Expand All @@ -24,6 +24,12 @@ includes:
rules:
- Orrison\MeliorStan\Rules\NumberOfChildren\NumberOfChildrenRule

services:
-
class: Orrison\MeliorStan\Rules\NumberOfChildren\NumberOfChildrenCollector
tags:
- phpstan.collector

parameters:
meliorstan:
number_of_children:
Expand Down Expand Up @@ -93,5 +99,6 @@ class Child6 extends PopularParent {} // ✗ This 6th child causes the violation

- **Direct children only**: This rule counts only direct subclasses (classes that directly extend the parent), not grandchildren or deeper descendants.
- **Cross-file analysis**: The rule uses PHPStan collectors to analyze class hierarchies across all files in your project.
- **Collector registration required**: Remember to register `NumberOfChildrenCollector` in any PHPStan config where you enable this rule.
- **Per-class violation**: The violation is reported on the parent class, not on its children.
- **Design consideration**: A high number of direct children often suggests that the parent class may be too generic or that the hierarchy could benefit from intermediate abstract classes or interfaces.
6 changes: 6 additions & 0 deletions tests/Rules/NumberOfChildren/config/default.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ includes:
rules:
- Orrison\MeliorStan\Rules\NumberOfChildren\NumberOfChildrenRule

services:
-
class: Orrison\MeliorStan\Rules\NumberOfChildren\NumberOfChildrenCollector
tags:
- phpstan.collector

parameters:
meliorstan:
number_of_children:
Expand Down
6 changes: 6 additions & 0 deletions tests/Rules/NumberOfChildren/config/maximum-one.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ includes:
rules:
- Orrison\MeliorStan\Rules\NumberOfChildren\NumberOfChildrenRule

services:
-
class: Orrison\MeliorStan\Rules\NumberOfChildren\NumberOfChildrenCollector
tags:
- phpstan.collector

parameters:
meliorstan:
number_of_children:
Expand Down
6 changes: 6 additions & 0 deletions tests/Rules/NumberOfChildren/config/maximum-two.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ includes:
rules:
- Orrison\MeliorStan\Rules\NumberOfChildren\NumberOfChildrenRule

services:
-
class: Orrison\MeliorStan\Rules\NumberOfChildren\NumberOfChildrenCollector
tags:
- phpstan.collector

parameters:
meliorstan:
number_of_children:
Expand Down