Laravel Pint. What is it? Why do you need it? How to use it?

What is Laravel Pint?

Laravel Pint is a first-party package from the Laravel core team for PHP code style fixing. It is built on top of PHP-CS-Fixer and makes it simple to ensure your code style is clean and consistent.

Pint is installed by default on the new Laravel applications, so you can use it immediately as soon as you create a new Laravel project.

Why do you need it?

This is an important question. Why do you really need Pint?

Your role as a developer on a project is to work on the solution that adds value to the project. And you should not keep going back and forth on formatting your code here and there, also not to mention to review those linting/formatting manually before the commits or merging a pull request.

So Pint or basically any linting tool takes this responsibility off your shoulders and allows you to think and work on solutions rather than wasting time on code styling and formatting.

With the clarification on why you need Pint on your project, let’s see how you can install and use Laravel Pint on your project.

How to install and use Laravel Pint on your project?

I was thinking to add the process for installing Pint on the Laravel project, but as I was doing research for this post, I found that the new Laravel projects already have it installed by default. So you can use it without any effort on configuring it.

Installation

However, if you want to install it on your existing project, you may install Laravel Pint via Composer:

composer require laravel/pint --dev

Formatting code using Pint

By adding the composer package, the pint binary is added to your vendor/bin directory. You can run pint to fix your code style just by issuing the below command.

./vendor/bin/pint

Based on your preference you may only want to update the files that are changed. You can use the below command, it will fix the code styling for only the files that have uncommitted changes according to Git.

./vendor/bin/pint --dirty

You can find more details on configuring Pint according to your requirements on the official Laravel Pint documentation.

Conclusion

Pint could be a very handy tool especially when you are working with teams. You can set up workflows that can detect code styling issues before merging a pull request. You can also set up the code styling rules that everyone should follow, and you can use Pint to validate that too.