Designing Explicit Data Contracts
#blogPostAsWebApp: https://voku.github.io/PHPArrayBox/
Rule of thumb:
If your array-shape PHPDoc ne...
For further actions, you may consider blocking this person and/or reporting abuse
Funnily enough, around 5 minutes ago, I was juggling with render arrays in a Drupal 11 instance... I see why they still have them, though, legacy and all, but gosh, I so wish they had some actual OOP. The worst part is that they're mixing these deeply nested arrays that only work by convention with value objects.
Great post, thank you! I kinda needed to read this to know that I'm not the only person out there to think that this isn't sustainable.
I let PHP enforce contracts. The problem I have with PHPStan to enforce contracts is that bugs can still occur when running the code in production.
I don't want to let PHPStan be the Typescript of PHP.
To be clear, I'm not against PHPStan.
It is the
array<string, scalar>andnon-empty-stringcomments that gives me the creeps.In the end PHPStan is an additional tool. It shouldn't be considered as part of the language.
Why not, even the language developers from php itself though of this more than once, same as hack (Facebook) who has introduced Generics since many years this way. docs.hhvm.com/hack/reified-generic...
I think you misunderstood the last sentence of my comment.
It is not that I don't want generics in PHP, I don't want generics to handled only by PHPStan.
I understand, that's why I used that example..., what if the php foundation would include it into php, same as hack did it? (Hack supports generics for type safety, but by default they are erased at runtime (type erasure).)
At my first job as a junior developer, my boss built a php framework that was passing arrays in every little function that he created. Although he was a seasoned developer I believe he liked the simplicity of the array.
Me and my colleagues tried to change his mind. He somehow backed up by accepting laravel as a framework but still....You could see that he loved his precious arrays :)
You can generate a phpstan baseline file, where you ignore all old errros, and just start to write maintainable code.
For frameworks and other tools there are many phpstan extensions that supports you e.g. auto detect types directory from your sql queries or other magic stuff, if you really need it.
yes, it always depends on the project and the costs that the company has if something breaks in production.
Nice post! Should be required reading for any PHP developer! Good work!