Designing Explicit Data Contracts
Rule of thumb:
If your array-shape PHPDoc needs line breaks, comments, or nested generics — congratulations, you’ve just designed an object. You just didn’t have the courage to admit it.
Introduction: The $data Anti-Pattern Nobody Wants to Own
Every PHP codebase has it.
function handle(array $data): void
{
// 🤞 good luck
}
It starts harmlessly. A quick prototype. A deadline. “We’ll refactor it later.”
Six months later, $data is a structural landfill:
undocumented keys
optional flags with magic meaning
half the validation duplicated across the codebase
and a PHPStan array-shape that looks like a legal contract
If your system relies on arrays to model domain concepts, you don’t have flexibility — you have structural debt.
Background: Why PHP Developers Fell in Love with Arrays
Let’s be fair. PHP trained us this way.
Historically:
No enums
Weak typing
No readonly properti
Discussion
Take the lead—comment now
Lead the way—your insights can inspire others.