Traits to Services: Refactoring for Testability (and for Agents)
The Trait Problem PHP traits are seductive. You've got some chat notification logic. Four controllers need it. Slap it in a trait, use ChatNotificationTrait, done. Except now you have: Hidden depen...

Source: DEV Community
The Trait Problem PHP traits are seductive. You've got some chat notification logic. Four controllers need it. Slap it in a trait, use ChatNotificationTrait, done. Except now you have: Hidden dependencies — the trait calls $this-> methods that don't exist in the trait itself Invisible coupling — change the trait, break four controllers, good luck figuring out which ones Untestable logic — you can't unit test a trait in isolation because it doesn't exist in isolation Global state smell — traits encourage reaching into the controller's properties This codebase had six traits doing serious work: Trait What It Did ChatNotificationTrait Send chat webhooks CrmApiTrait CRM sync OcrScanApiTrait OCR via document scanning API ConvertApiTrait Document conversion ExternalApiTrait Third-party API integration CalculationTrait Domain calculations Each one was used in multiple controllers. Each one mixed HTTP client logic, business rules, error handling, and configuration into a single use statemen