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

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.

And concretely, what is it?

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 :

Bad identation laravel pint

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 fixed

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 :

laravel pint fixed detailled

Here you go ! Perfect coding style !

“Another tool which takes hours to set up…”

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.

Talking about 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 :

Laravel pint configuration

Now, automate it

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).

Laravel pint makefile

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.

Laravel pint precommit

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 !

Laravel pint fail

In short

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.

Share This Article

Related Post

Scrum methodology

What can SCRUM methodology do for us on a dai

First of all, the first thing you need to know is: at O...

inertia and laravel

Inertia, a bridge between Laravel and Vue.js

Here, at Origami Marketplace, we are big fans of Larave...

Hackhaton how and why

Internal Hackathon: why and how?

What is a hackathon? Hackathon: contraction of the word...

Leave a Comment