Laravel Pint: Automatically Format Code with One Command

If you're a web developer, you know that code should be clear and readable. If you write clear and readable code, other developers and team members can easily understand it.

If you don't write clear and readable code and then manually correct it later, it will take a lot of time.

Laravel Pint
Laravel Pint - Built by the Laravel Team

If you're a Laravel developer and don't write clear and readable code in your Laravel project, there's no need to manually correct it. Laravel includes the "PINT" package, which helps make code clear and readable in a minimum of two to five minutes. "PINT" is specifically designed for Laravel. This package formats code according to the Laravel or PSR-12 coding standard, reducing your code maintenance effort.

In this article, we'll learn about "Laravel Pint", what it is, how it's used, and how to install "Laravel Pint".

What is Laravel Pint?

Laravel Pint is an open-source code formatting package. Pint prioritizes Laravel's rules and styles and formats code according to those rules and styles.
  1. When multiple developers work on a project, the code written can vary significantly. Pint ensures that all code is formatted in the same way. If the code is not formatted correctly, it formats it, making it easier to read.
  2. There's no need to manually correct indentation, braces, and spaces. Pint does all of this automatically, so you don't have to worry about formatting the code while writing, but just focus on writing the code.
  3. Laravel Pint was created by the Laravel team, so setting up Pint in a Laravel project doesn't require much effort.

How to Install Laravel Pint Package

Installing Laravel Pint is quite easy. You can install Laravel Pint by following these simple steps.

1.Install your project in an editor like "Vs Code" and open the terminal and run the following command to install Pint.


composer require laravel/pint --dev


Installing Laravel Pint using Composer
Installing Laravel Pint with Composer in VS Code Terminal

2. This command adds "Laravel Pint" to Laravel's vendor folder without affecting your production dependencies.

3. Basic Usage: Formatting Your Code with Pint
Once Laravel Pint is installed, you can format the code for your entire project by simply running this command.


./vendor/bin/pint


4. This command scans your project files and automatically fixes formatting issues according to Laravel's coding standards.

Speeding up code formatting with parallel processing

Formatting code for large projects can be time-consuming. To address this problem, Laravel Pint offers a parallel processing mode. In this mode, you run the following command:

This command processes multiple files simultaneously, speeding up formatting time.

To do this, you can run the following command:


./vendor/bin/pint --parallel


Formatting Specific Files and Folders

You don't always need to format your entire project. Laravel Pint allows you to format the code of specific folders and files.


./vendor/bin/pint app/Models/User.php


Preview changes without formatting

If you want to see which files will be changed without making any changes to your code, run:


./vendor/bin/pint --test


This "dry-run" mode lets you review the changes Pint makes before applying them.

Laravel Pint Formatting Code Automatically
Laravel Pint formatting PHP code according to Laravel standards


Frequently Asked Questions

Q1:- Can Laravel Pint be used outside of a Laravel project?
A1:- Yes, Laravel Pint can be used outside of Laravel. You can use it in any PHP project.

Q2:- How is Laravel Pint different from PHP-CS-Fixer?
A2:- Laravel Pint is essentially a wrapper for PHP-CS-Fixer with Laravel's coding standards already configured.

Q3:- Does Laravel Pint change my code logic?
A3:- No, Pint only formats the code, not the logic; it only formats spaces, indentation, and braces.

Conclusion

Laravel Pint is a must-have package for any Laravel project. It focuses on code clarity and readability, eliminating the hassle of manual formatting.

Whether you're working on a project alone or with a team, adopting Laravel Pint will save you time and make your code look professional. You can use it in your Laravel projects.

Useful Links

Post a Comment

Previous Post Next Post