Laravel Stats: How it was made and what's on the horizon
This article has been published a while ago.
If this is a technical article some information might be out of date. If something is terribly broken, let me know and I will update the article accordingly.
Update 05.02.2018
Version 1.5 of Laravel Stats has recently been released. It adds the mentioned JSON Output and Custom Component Features.
v1.5.0 on GitHub
I've recently released a new Laravel Package: laravel-stats. In this post, I would like to describe how I went from idea to release, how the internals of the package work and what features are on the roadmap.
In summary, the package reads a folder, goes through all classes and classifies each class to a Laravel Component. It then counts and sums the number of Classes, Methods and Lines of Code and displays the results in a nice table.
From Idea to Release #
I can't remember when I saw the tweet for the first time. DHH once shared a screenshot which showed the size of the Basecamp 3 codebase.
Basecamp 3 packs the most potent lines of code I've ever written AND also the most of them ever for a version 1: pic.twitter.com/TOV6094RbN
— DHH (@dhh) September 24, 2015
It shows the number of Controller, Models and much more. I immediately thought: "Hey, that's so cool. Would be great if this would exist for Laravel." I made a note, forgot about it and went on with my life.
Suddenly the same Tweet was discussed in an episode of the Laravel Podcast. While listening to the episode I thought: "I should give this a shot and try to figure out, how to write a package that does this." Back then I didn't have much experience writing Laravel Packages and could just build a simple prototype (which never got released). Life got in the way and I moved the package down my priority list.
In September, I stumbled upon my old prototype and tried to optimize it. In the same week Taylor tweeted about what people would like to see in Laravel 5.6. Out of nowhere Adam Wathan tweeted that he would like so see artisan stats
and then I had to chime in.
After I hit publish on my Tweet I suddenly realized, that I now had the pressure of actually releasing the package to the public. So I've rewritten the code multiple times and sent Adam a link to the first BETA version. He was excited. The package got shared around in the Laravel community, went semi-viral (at least for my standards) and I received a lot of valuable feedback.
v1.0.0
got released 4 weeks after my Tweet. (At the time of writing this post the package is already at v1.3.3
)
How it works #
As described at the beginning, the main goal of the package is to get you an overview of your current project by associating the PHP Classes in your project to Laravel Components[^A component, in this case, is a Controller or a Model. It's basically a class which extends a certain Laravel core class or implement a Contract.].
The package recognizes these components by using the native PHP ReflectionClass. Each class in a project is then classified by a set of Classifiers (yeah, naming is hard).
I highly recommend reading the source of the package on GitHub. It's quite simple and easy to understand. (I especially like the decorated ReflectionClass)
Feature Roadmap #
As I'm writing this article the package reached already v1.3.3
and all Core Laravel Components can be identified by the package. The following features are on my roadmap for v2.0.0 v1.5.0
:
JSON Output Format #
In addition to the default table, I would like to have the statistics also in JSON. This would open the gates for other tools to use the output and react to it (for example a CI service which fails if the Code to Test radio is below a certain threshold).
Custom Components #
Even though the package is called Laravel stats I think it would be a cool addition if you could define your own set of Components for your project. I work on some bigger projects where we've written our own bigger service classes/components. If you could get an overview of how many methods or lines of code these services classes use could be really useful.
So that's Laravel Stats. I'm really happy with this little package so far. I learned so much during development and after releasing it to the public. It was also the first time I got valuable feedback through Tweets and Pull Requests (never thought I would spend so much time reading through PRs). Over the next weeks, I will work on the mentioned features.