Analyzes PHP code for class length issues. Detects classes exceeding 300 lines, God class indicators, cohesion issues, SRP violations.
From accnpx claudepluginhub dykyi-roman/awesome-claude-code --plugin accThis skill uses the workspace's default tool permissions.
Analyze PHP code for class size and cohesion issues.
| Lines | Classification |
|---|---|
| 1-100 | ā Ideal |
| 101-200 | ā ļø Acceptable |
| 201-300 | š” Large - review needed |
| 301-500 | š Too large - split |
| 501+ | š“ God class - urgent refactoring |
// GOD CLASS: Does everything
class OrderManager
{
// Handles orders
public function createOrder() {}
public function updateOrder() {}
public function cancelOrder() {}
// Handles payments
public function processPayment() {}
public function refundPayment() {}
// Handles shipping
public function createShipment() {}
public function trackShipment() {}
// Handles inventory
public function reserveStock() {}
public function releaseStock() {}
// Handles notifications
public function sendOrderConfirmation() {}
public function sendShippingNotification() {}
// Handles reporting
public function generateOrderReport() {}
public function exportToExcel() {}
// 50+ more methods...
}
// GOOD: Split by responsibility
class OrderService {}
class PaymentService {}
class ShippingService {}
class InventoryService {}
class NotificationService {}
class ReportingService {}
// LOW COHESION: Methods don't use same properties
class UserService
{
private $userRepository;
private $emailService;
private $paymentGateway;
private $logService;
private $cacheService;
// User methods use $userRepository
public function findUser() {}
public function updateUser() {}
// Email methods use $emailService (unrelated)
public function sendEmail() {}
public function validateEmail() {}
// Payment methods use $paymentGateway (unrelated)
public function processPayment() {}
public function checkBalance() {}
}
// HIGH COHESION: All methods use same core dependencies
class UserService
{
public function __construct(
private UserRepository $userRepository,
private PasswordHasher $hasher,
) {}
public function findUser(int $id): User {}
public function createUser(UserData $data): User {}
public function updateUser(User $user, UserData $data): void {}
public function changePassword(User $user, string $password): void {}
}
// TOO MANY DEPENDENCIES: Indicates SRP violation
class OrderProcessor
{
public function __construct(
private OrderRepository $orderRepository,
private ProductRepository $productRepository,
private UserRepository $userRepository,
private PaymentGateway $paymentGateway,
private ShippingService $shippingService,
private InventoryService $inventoryService,
private EmailService $emailService,
private SmsService $smsService,
private PushNotificationService $pushService,
private LoggerInterface $logger,
private CacheInterface $cache,
private EventDispatcher $eventDispatcher,
// 10+ more...
) {}
}
// GUIDELINE: Max 5-7 dependencies
class OrderProcessor
{
public function __construct(
private OrderRepository $orderRepository,
private PaymentProcessor $paymentProcessor,
private NotificationService $notificationService,
private EventDispatcher $eventDispatcher,
) {}
}
// FEATURE ENVY: Class manipulates other class's data extensively
class OrderPrinter
{
public function print(Order $order): string
{
$output = "Order: " . $order->getId() . "\n";
$output .= "Customer: " . $order->getCustomer()->getName() . "\n";
$output .= "Address: " . $order->getCustomer()->getAddress()->getStreet() . "\n";
$output .= "City: " . $order->getCustomer()->getAddress()->getCity() . "\n";
$total = 0;
foreach ($order->getItems() as $item) {
$output .= $item->getProduct()->getName() . ": ";
$output .= $item->getQuantity() . " x " . $item->getPrice() . "\n";
$total += $item->getQuantity() * $item->getPrice();
}
// Many more lines accessing Order internals...
}
}
// BETTER: Move logic to Order class
class Order
{
public function format(): string
{
// Order knows how to format itself
}
}
// TOO MANY PUBLIC METHODS: API surface too large
class UserService
{
public function findById() {}
public function findByEmail() {}
public function findByPhone() {}
public function findByUsername() {}
public function findActive() {}
public function findInactive() {}
public function create() {}
public function update() {}
public function delete() {}
public function activate() {}
public function deactivate() {}
public function ban() {}
public function unban() {}
public function verify() {}
// 20+ more public methods
}
// BETTER: Split into focused classes
class UserFinder {}
class UserModifier {}
class UserStatusManager {}
// Before: One large class
class Order
{
// Order data and methods (30 methods)
// Pricing logic (10 methods)
// Shipping logic (8 methods)
// Notification logic (5 methods)
}
// After: Multiple focused classes
class Order {} // Core order data
class OrderPricing {} // Price calculation
class OrderShipping {} // Shipping logic
class OrderNotifier {} // Notifications
// Before: Class does everything
class OrderService
{
public function complete(Order $order): void
{
$order->complete();
$this->updateInventory($order);
$this->sendEmail($order);
$this->createInvoice($order);
$this->notifyWarehouse($order);
}
}
// After: Event-driven
class OrderService
{
public function complete(Order $order): void
{
$order->complete();
$this->eventDispatcher->dispatch(new OrderCompletedEvent($order));
}
}
// Separate listeners handle each concern
class UpdateInventoryListener {}
class SendConfirmationEmailListener {}
class CreateInvoiceListener {}
| Lines | Severity |
|---|---|
| 201-300 | š” Minor |
| 301-500 | š Major |
| 501+ | š“ Critical |
### Class Length: [ClassName] is too large
**Severity:** š /š“
**Location:** `file.php`
**Lines:** 450
**Methods:** 35
**Dependencies:** 12
**Issue:**
Class `OrderManager` is 450 lines with 35 methods, indicating multiple responsibilities.
**Responsibilities Detected:**
1. Order CRUD operations
2. Payment processing
3. Shipping management
4. Email notifications
5. Reporting
**Suggested Split:**
OrderService (100 lines) āāā OrderRepository PaymentProcessor (80 lines) ShippingManager (70 lines) OrderNotifier (50 lines) OrderReporter (60 lines)
apply* event handlers grow naturallyAggregateRoot or similar basetests/ directoryProvides UI/UX resources: 50+ styles, color palettes, font pairings, guidelines, charts for web/mobile across React, Next.js, Vue, Svelte, Tailwind, React Native, Flutter. Aids planning, building, reviewing interfaces.