What Is "Laravel Pint" And Do I Need It As a Laravel Developer?

What Is "Laravel Pint" And Do I Need It As a Laravel Developer?

YES, you do need Laravel Pint, do you think the Laravel team was stupid when they included it in all new Laravel applications?

Okay, let me start with the real reason why you are here...

What Is Laravel Pint?

Here is how the Laravel team describes it:

Laravel Pint is an opinionated PHP code style fixer for minimalists. Pint is built on top of PHP-CS-Fixer and makes it simple to ensure that your code style stays clean and consistent.

Laravel Pint is a tool that fixes your code to follow standards, in other words, it scans your entire code, detects coding standards problems, and fixes them for you in seconds, how cool is that!

Let's have a look at it in action...

pint1.jpg

That's definitely not a piece of code that you would like to be on your computer, but let's be honest, it happens sometimes.

Some things need to be changed in this code like long array syntax, double quotes, and the extra blank space.

Let's see how Laravel Pint will fix that for you...

pint3.jpg

With one command, Laravel Pint made your code perfect!

pint2.jpg

It also shows you how many files have been scanned, which files fixed, and the errors it fixed, what do you want more than that ?!

How to install Laravel Pint?

Let me stop you just right there, if you think installing it is hard and takes hours then you're wrong.

In fact, if you are using a new Laravel application then my friend Laravel Pint is already installed and you just need to run it.

If it's not installed in your project yet, then one command is all that you need to install it.

composer require laravel/pint --dev

How to use Laravel Pint: Running Pint

To simply run Laravel Pint to scan and fix all the files, use this command:

./vendor/bin/pint

To view more details about what Laravel Pint is changing in your files, provide the -v option:

./vendor/bin/pint -v

If you would like Laravel Pint to only scan your files and show you style errors without fixing them (without editing files), provide the --test option:

./vendor/bin/pint --test

How to configure Laravel Pint?

Laravel Pint doesn't need any configuration to be used, you can install it and directly use it without thinking about configuring it.

However, if you want to use a different preset, tweak the rules, or customize inspected folders, you can do that by creating a pint.json file in the root directory of your project.

Presets

Presets are the groups of rules that Laravel Pint follows to detect and fix the code style issues in your code.

By default, Laravel Pint uses the Laravel preset but you still can choose the one you want using the pint.json file:

{
    "preset": "psr12"
}

If you wish, you can also specify a different preset by providing the --preset option:

./vendor/bin/pint --preset psr12

At the time of writing this article, Laravel Pint supports three presets: laravel, psr12, and symfony.

Rules

Rules are the actual guidelines that Laravel Pint uses to fix your code.

Presets are groups of Rules that should be perfect for most of your projects, but if you would like to tweak some rules, you can do that using the pint.json file:

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

Since Laravel Pint is built on top of PHP-CS-Fixer, you can use any of its rules: PHP-CS-Fixer Configurator.

Excluding Files / Folders

By default, Laravel Pint will scan all .php files in your project (except the ones in the vendor directory), if you want to exclude more folders, you can use the exclude configuration option in the pint.json file:

{
    "exclude": [
        "path/to/your-folder"
    ]
}

Want to exclude all files that contain a given name pattern? use the notName configuration option:

{
    "notName": [
        "*-your-cool-file.php"
    ]
}

Want to exclude an exact file? use the notPath configuration option:

{
    "notPath": [
        "path/to/your-file.php"
    ]
}

Do you need Laravel Pint?

After showing you what exactly it is and what it can do, I'll let you answer that question.

For me though, I see it as a must install tool for every project, I definitely will use it for all my upcoming projects.

Will you use Laravel Pint?
Are you already using it?
Any thoughts you want to add?