This rule checks if a class, interface, trait, or enum has too many public methods, which may indicate the class has an overly broad public API and should be refactored into smaller, more focused components.
This rule supports the following configuration options:
- Type:
int - Default:
10 - Description: The maximum number of public methods allowed in a class-like structure before triggering an error.
- Type:
string - Default:
^(get|set|is) - Description: A regular expression pattern (without delimiters) to match public method names that should be excluded from the count. By default, getter, setter, and boolean accessor methods are ignored. Set to an empty string
''to count all public methods.
- Type:
bool - Default:
true - Description: When
true, PHP magic methods (those starting with__, such as__construct,__toString,__get, etc.) are excluded from the public method count. Magic methods are PHP requirements rather than deliberate API choices, so they are ignored by default.
Add the rule to your PHPStan configuration:
includes:
- vendor/orrison/meliorstan/config/extension.neon
rules:
- Orrison\MeliorStan\Rules\TooManyPublicMethods\TooManyPublicMethodsRule
parameters:
meliorstan:
too_many_public_methods:
max_methods: 10
ignore_pattern: '^(get|set|is)'
ignore_magic_methods: true<?php
class UserService
{
public function __construct() {} // ✓ Ignored (magic method)
public function getName(): string { return ''; } // ✓ Ignored (matches ignore_pattern)
public function setName(string $name): void {} // ✓ Ignored (matches ignore_pattern)
public function isActive(): bool { return true; } // ✓ Ignored (matches ignore_pattern)
// 10 regular public methods, at the limit, no error
public function register(): void {}
public function activate(): void {}
public function deactivate(): void {}
public function suspend(): void {}
public function delete(): void {}
public function restore(): void {}
public function sendWelcomeEmail(): void {}
public function sendPasswordReset(): void {}
public function generateToken(): string { return ''; }
public function revokeToken(): void {}
}
// ✓ Valid (exactly 10 counted methods)
class GodService
{
public function processOrder(): void {}
public function cancelOrder(): void {}
public function refundOrder(): void {}
public function sendInvoice(): void {}
public function notifyCustomer(): void {}
public function updateInventory(): void {}
public function logActivity(): void {}
public function validatePayment(): void {}
public function chargeCard(): void {}
public function applyDiscount(): void {}
public function calculateTax(): void {}
}
// ✗ Error: Class "GodService" has 11 public methods, which exceeds the maximum of 10. Consider refactoring.parameters:
meliorstan:
too_many_public_methods:
ignore_magic_methods: false<?php
class Service
{
public function __construct() {} // Counted
public function __toString(): string { return ''; } // Counted
public function methodOne(): void {}
public function methodTwo(): void {}
public function methodThree(): void {}
public function methodFour(): void {}
public function methodFive(): void {}
public function methodSix(): void {}
public function methodSeven(): void {}
public function methodEight(): void {}
public function methodNine(): void {}
}
// ✗ Error: Class "Service" has 11 public methods, which exceeds the maximum of 10. Consider refactoring.parameters:
meliorstan:
too_many_public_methods:
ignore_pattern: ''<?php
class DataTransferObject
{
public function getName(): string { return ''; } // Counted (no pattern exclusion)
public function setName(string $n): void {} // Counted
public function getEmail(): string { return ''; } // Counted
public function setEmail(string $e): void {} // Counted
public function getAge(): int { return 0; } // Counted
public function setAge(int $a): void {} // Counted
public function isActive(): bool { return true; } // Counted
public function isVerified(): bool { return false; } // Counted
public function process(): void {} // Counted
public function validate(): void {} // Counted
public function execute(): void {} // Counted
}
// ✗ Error: Class "DataTransferObject" has 11 public methods, which exceeds the maximum of 10. Consider refactoring.parameters:
meliorstan:
too_many_public_methods:
ignore_pattern: '^(get|set|is|has|with|can)'<?php
class Builder
{
public function withTimeout(int $t): self { return $this; } // ✓ Ignored
public function withRetries(int $r): self { return $this; } // ✓ Ignored
public function hasItems(): bool { return true; } // ✓ Ignored
public function canProcess(): bool { return true; } // ✓ Ignored
public function build(): object { return new \stdClass(); } // Counted
}- The rule applies to classes, interfaces, traits, and enums
- Interface methods are always counted as public (interfaces are implicitly all-public)
- Static public methods are counted the same as instance public methods
- Protected and private methods are never counted
- The
ignore_patternis case-insensitive (e.g.,GetNameandgetnameare both matched) - Pattern delimiters (
/) are added automatically; only provide the pattern itself - Methods are counted based on their declaration in the specific class-like structure, not inherited methods
- Consider using this rule alongside
TooManyMethodsto distinguish between all-method bloat and public API bloat