Inertia, a bridge between Laravel and Vue.js
Here, at Origami Marketplace, we are big fans of Larave...
Here, at Origami Marketplace, we really are attached to code quality. Our code must be readable and maintainable.
I was able to work on the subject for a few days and I discovered a little gold nugget that I wanted to share.
So I present to you :
Laravel Pint is a PHP code style analyzer based on PHP-CS-Fixer created by Nuno Maduro (@enunomaduro), a Laravel core developer.
In addition to having a perfect name (actually, it’s a contraction of PHP and lint), this tool is particularly useful.
Well, it’s very simple, Laravel Pint will take care of the neatness of your code.
A bad indentation, a missing space or a misplaced brace, it can happen to anyone.
Let’s take an exemple :
That’s what we don’t want to see anymore at Origami Marketplace.
Several things here need to be changed, such as the long syntax for array creation or the alignment of operators.
Now, let’s try Laravel Pint and see what it does.
Laravel Pint has detected some errors in the file resources/lang/en/review.php which is our exemple file.
The error type is written just after the file name.
Here, we can see “array_syntax” error type, which tells you that your array is not using the right syntax.
Or “binary_operator_spaces” which shows that Pint saw that your operators are not well aligned.
Now, let’s see how Laravel Pint fix that :
Here you go ! Perfect coding style !
You’re wrong dear imaginary friend, Nuno Maduro focused on simplicity of setting up the tool.
In fact, to be more concrete, two commands are necessary:
Those two commands will be enough to install and launch code analysis with base configuration.
Few minutes will be enough to install and launch Laravel Pint with base configuration.
And you won’t need many more to create your own custom configuration.
In fact, all you need is a pint.json file in the root directory of your project in which you will define exactly what you want.
Basically, analysis will be done with PSR12 presets but Laravel Pint offers some more presets. At Origami Marketplace, we decided to use Laravel dedicated preset.
I won’t epilogue about our different configurations, but if you’re interested in them, you can take a look at the PHP-CS-Fixer configurator, it will help you understand them.
Here is our configuration file :
Perfect, now that we have everything we need.
Everyone can use Laravel Pint in their local environment.
We can assume that all developers will follow the rules and only push beautiful code, but that’s not funny.
Let’s assume that one of you coworkers decides to do as he pleases and only pushes dirty code, let’s call him Tommy.
Here is a few advices to thwart his plans:
Makefile
To simplify our developers life, we created a Makefile which contains the most commonly used commands. We needed a Laravel Pint command.
Tommy, has no excuses anymore, he cannot complain about forgetting the command or the length of if (which can quickly increase as we are using Docker).
Git hooks
A Git hook is a script which will be launched at a defined moment during git usage.
Here, we will focus on the pre-commit hook.
At Origami Marketplace, we decided to launch Laravel Pint before every commit.
This is pretty simple, Pint will repair sniffed errors before the code would be committed.
If some errors remain, the commit will be refused.
Of course, in this hook, we will use our Makefile command created before.
But there, Tommy can use the –no-verify option, which will allow him to push even with errors.
Git action
Finally, the killing blow sent to the teeth of the dirty code, the git action.
Git action is a to do list we will give to GitHub. It will execute every task of this list at a given moment.
Here, we decided to execute it every time a team member push something to GitHub
This action will simply launch Laravel Pint with the –test option which will let it analyze the code without fixing it.
GitHub won’t let you merge your Pull Request as long as there are errors.
Checkmate, Tommy !
It is always a pleasure to work as a developer on an important subject like this one.
And this is even more pleasant when you discover tools like that.
Thanks again Nuno Maduro for your work and making it easy to have a beautiful and clean code.
Here, at Origami Marketplace, we are big fans of Larave...
What is a hackathon? Hackathon: contraction of the word...
First of all, the first thing you need to know is: at O...
2 Comments
popipuu
We can not copy your code 🙁
Reply元旦快了
I do aggree with all of thhe ides you’ve ffered on your post.
ReplyTheyy are really convincing and will definitely work.
Still, tthe post arre very briedf for novices.
May you please prolong them a bbit from next time? Thanks ffor the
post.