Introduction

Laravel Pint is quickly getting the attention of the Laravel community after the Laravel team decided to add it as a new dependency in the new Laravel release.

What is Laravel Pint, and how can you benefit from it? How to use it with Visual Studio Code and PhpStorm? How to configure it and add rules? These are the type of questions we will be answering in this article.

As a bonus, I will show you the configuration file that I use in all of my projects.

What is Laravel Pint

If you are a native English speaker, then you are probably pronouncing Pint as “paɪnt” because that's the word that you all know. But, the correct pronunciation for this particular case is “pɪnt”, since it's a combination of two words, PHP and Lint.

Laravel Pint is a code style fixer for PHP that helps you to keep your PHP code neat, clean, and organized with flexibility to choose any code style you want. By default, it comes with the Laravel code style standard, but you can easily switch to other code styles by creating a pint.json file in your root directory with this JSON:

{
    "preset": "psr12"
}

This package is built on top of PHP CS Fixer that we all know, so you can feel free to use it on all of your projects that use PHP. It doesn't have to be Laravel-specific, it can even be used with plain PHP or WordPress themes and plugins if you wish so.

As of the beginning of 2023, Laravel Pint currently supports 3 presets: laravel, psr12, and symfony. This list might change in the future, so keep checking the documentation page, or just create your own set of rules to work with whatever code style you want.

Installation

If you installed Laravel version 9 or higher, you don't need to install Pint since it comes with a fresh Laravel installation. For existing projects, you can install it from the composer command line by running:

composer require laravel/pint --dev

You can check if the package is installed by running:

./vendor/bin/pint --test

This command is probably going to give you plenty of errors like this:

Pint inspection errors

Don't worry, you don't need to manually fix them, Pint will do it automatically for you.

Usage

As we already know, to run Laravel Pint, we just need to run this command:

./vendor/bin/pint --test

We used --test flag because we wanted to see code style violations without any code mutations. When we run the same command without a --test flag, it will resolve all issues found in the code instead of showing you what needs to be fixed. A --test flag is useful for deployment to production when you run tests before deployment to make sure your code is properly formatted and all tests are passing.

However, in a development environment, you may not need it because you are working on your code and all the style changes will be selected as a separate commit.

Exclude files and directories

To exclude directories from being run by the Pint checker, you can just add exclude configuration to pint.json like this:

{
    "exclude": [
        "tests"
    ]
}

I don't exclude any directories, but it is a nice option to have for some rare cases.

You don't need to exclude the vendor directory, since it's already excluded by default. It also excludes Laravel blade files by default because they are not currently supported in Pint yet.

This is how you would exclude files instead of directories:

{
    "notName": [
        "*Test.php"
    ]
}

Let's take a look at how you can use Laravel Pint with code editors and how we can configure it.

Setup Laravel Pint for VS Code

To set up Laravel Pint for VS Code, you need to install Laravel Pint extension.

Laravel Pint Visual Studio Code extension

To enable formatting on save in your editor, use configurations like this:

{
    "[php]": {
        "editor.formatOnSave": true,
        "editor.defaultFormatter": "open-southeners.laravel-pint"
    }
}

With this configuration in place, when you save a PHP file, it will be automatically formatted according to the Laravel Pint preset that you specified in the pint.json file. Pretty simple to set up.

Setup Laravel Pint for PhpStorm

You can set up Laravel Pint without extensions. To do so, follow these 3 simple steps:

  1. Open the File Watchers settings menu.
  2. Enter correct configurations.
  3. Enable File watcher in the settings menu.

Here is a more detailed explanation with screenshots:

Step 1

Open your settings and go to “Tools” > “File Watchers” and click the “+” icon to add a custom file watcher:

Add custom file watcher

Step 2

These are the configurations that you need to add:

Name: pint
File type: PHP
Program: $ProjectFileDir$/vendor/bin/pint
Arguments: $FileRelativePath$
Output paths to refresh: $FileRelativePath$
Working directory: $ProjectFileDir$

Here is the screenshot:

Configure custom watcher

Notice Advanced Options that we have 4 checkboxes and only 1 is enabled.

Once you are done with the watcher, don't forget to enable it with a checkmark like this:

Enable custom watcher

Step 3

Above the File Watchers, there is also “Actions on Save”. Select it and enable the “File Watcher: pint” there:

Enable the file watcher

That's it, you can go and try if Laravel Pint formats your code properly. If you get any errors, make sure your PhpStorm is up-to-date and you entered the correct paths. When I did it for the first time, I had a typo in the $FileRelativePath$ variable, that is why I pasted the actual text in the article, so make sure you spell it correctly.

Configure Laravel Pint

You already know how to change the preset, so let's focus on the rules. Just to clarify it, you don't need to worry about rules at all. Default presets are more than enough for 99% of the projects out there. But if you want to make some tweaks to existing presets, you can overwrite existing rules in your pint.json configuration file or extend it.

A preset is just a preconfigured set of rules. You can have many rules in one preset, including your own rules.

Take a look at the pint.json file from the documentation to have an idea how you define rules:

{
    "preset": "laravel",
    "rules": {
        "simplified_null_return": true,
        "braces": false,
        "new_with_braces": {
            "anonymous_class": false,
            "named_class": false
        }
    }
}

Since Laravel Pint is written on top of PHP Coding Standards Fixer, you can just use rules from the official sources of CS Fixer. Here is the link.

PHP Coding Standards Fixer docs

I spent many hours on this page looking for rules that I would use for myself. I'm going to share them with you.

Declare strict types

Be aware that some rules are risky and some are not. As you might guess, risky rules can break your code. For example, a declare_strict_types rule forces strict types declaration in all the files. It has a very high chance of breaking your code. Take a look at the output that I got by running Pint with this rule in config:

Declare strict types Laravel Pint configuration

Even thought this rule is risky, it still has to be in every project in my opinion. Now, after running without --test flag, I get 86 files changed.

Laravel Pint automatic code changes

I used to look for files without declare(strict_types=1); and manually add it, but Pint with CS Fixer makes it as easy as possible. Now it's automated for me. I save my time to spend on other things than worrying about something that has to be in PHP by default.

Strict comparison

A strict_comparison is another risky rule which I use in every project. When I write, for example, this line:

$bool = 1 == 2;

After saving the file, it will be formatted to:

$bool = 1 === 2;

With this rule in place, you can make sure that your team doesn't accidentally use a weak comparison operator.

No unused imports

A no_unused_imports rule is not risky and should be used by everyone. All it does is removing all the unused imports. The Laravel framework full of these unused imports when you install a fresh version, make sure to add this rule to your Laravel project.

If you are eager to learn more about Laravel and PHP, I highly recommend using Laracasts. I've been watching Laracasts for a while, and it's been growing very rapidly thanks to a remarkable team of content creators there.

My configuration file

If you worked with Go, also known as Golang, you've definitely enjoyed the auto-formatting features of the language ecosystem. I love this about Go, so I want the same thing for PHP. My Laravel Pint's setup does almost the same thing, I wish it could remove unused variables too.

Here is the link to my configuration file on GitHub:

gist.github.com/SerhiiCho/5c449d100ee2791cf732a013cad4e909

I stick to PSR-12 everywhere I can and add additional rules that are not part of the PSR-12.

Conclusion

What I love about Laravel Pint is that it's basically a CS Fixer on steroids. If you are like me who uses CS Fixer in all PHP projects, then Laravel Pint is a tool for you.

The best part about Laravel Pint, is that instead of yelling at me that I put a curly brace not where it supposes to be. It will take care of this issue without distracting me from thinking about a particular problem.

We, as developers, have more important things to do than worrying about code style. To me, it is a tool that makes my job faster, more productive and more fun. It is also flexible enough to just configure my rules once, and then I just repeat them in my next projects.

Please let me know what do you think about Laravel Pint in the comments section below! Can you tell me what rule should I add to my config file that I didn't mention in this article? If you want to read something about keeping your code clean, you can read this one: “A Dungeon of Smell: Deeply nested code”. You will not regret it, it's worth reading.

Keywords: static, analysis, phpcs, psr12, formatter, package, backend