4 ms·
> src/Grouchy/MessageStream.php:17: Class has too many methods: 12. Can be up to 10 methods. > src/Grouchy/MessageStream.php:66: Your function is too long. Cur
by thaumaturgy 6y ago
> src/Grouchy/MessageStream.php:17: Class has too many methods: 12. Can be up to 10 methods.
> src/Grouchy/MessageStream.php:66: Your function is too long. Currently using 40 lines. Can be up to 20 lines.
sigh.
One of the annoyances of modern PHP practices is the tendency to write one class, per file, which extends or implements one other class, and adds one or two functions to it. Every single logical abstraction in a PHP project can have somewhere between one to thirty different classes associated with it, and larger PHP projects end up with a thousand or more files. All of this is enabled by composer's PSR-4 autoloading support, so as long as you give your classes the right kind of names and put the files in the right places, you don't have to think about what it means to hit the disk that many times on a page load. Consequently, there are more and more harebrained caching schemes showing up to kind of paper over this architectural problem. And tracking some unexpected behavior through that many different pieces just suuuuuuucks.
I fully get that god objects are bad. But metrics like these are making complex PHP projects worse, not better. PHP is not, and should not be, Java. Java has already shown us what the end result of these practices looks like. Let's not do that.
I desperately want a good code analysis tool for PHP: something that can look behind whether there's exactly one space before a '*' in a comment block and into deeper things like, "hey, on this line over here you tried to call a function that doesn't exist in the class the variable is supposed to be referencing".
Some further reading:
Aim small, miss small: https://www.deconstructconf.com/2018/stuart-halloway-aim-small-miss-small-writing-correct-programs https://www.deconstructconf.com/2018/stuart-halloway-aim-sma...
Composition over inheritance: https://github.com/jupeter/clean-code-php#prefer-composition-over-inheritance https://github.com/jupeter/clean-code-php#prefer-composition...
Small functions considered harmful: https://medium.com/@copyconstruct/small-functions-considered-harmful-91035d316c29 https://medium.com/@copyconstruct/small-functions-considered...
(There were a few other minor misbehaviors in this tool's output, but I'm less disappointed by those.)
edit: Sometimes I forget to mention the things I like about a project. The output this produces is super clean and easy to navigate, I liked it a lot. The dashboard presentation at the beginning of the output is super cool. It's a great idea to have a single thing you can drop in that sets up phpcs and similar tools with reasonable defaults. The developer for PHP Insights probably didn't make any of the decisions that resulted in line counts or function counts being part of the output. My disappointment was only that I got my hopes up that it was something different from what it was.
- larzang 6y ago> I desperately want a good code analysis tool for PHP: something that can look behind whether there's exactly one space before a '*' in a comment block and into deeper things like, "hey, on this line over here you tried to call a function that doesn't exist in the class the variable is supposed to be referencing". Then you want phpstan or psalm. They do actual type checking and static analysis rather than style linting, and do it quite well. If you do want an alternative for linting, Slevomat Coding Standard adds a huge number of highly-configurable and useful extra rules to PHPCS. I'm a big fan.
- thaumaturgy 6y agoWow, phpstan and psalm are both brilliant, and this is the first time I've been introduced to them. Thank you.
- Ayesh 6y agoInheritance is slowly fading away in favor of composition. Drupal is an absolute abomination when it comes to extending classes, as in 5-6 in the chain is not uncommon. However, Drupal was never called for its high modern code quality either. Projects like Symfony favor Interface model (the I in SOLID), and recent versions address some of the pain points you mentioned. In terms of analyzers, I rely on PHP Inspections EA plugin for PHPStorm (commercial IDE). PHPStan, Phan, and Psalm are the three most popular PHP static analyzers and they are getting so good!
- paledot 6y agoPHP has opcode caching. Not that it invalidates your other points, but you're not hitting the disk once per file per request. Just once per file per deploy.