Laravel Pint is the official code style fixer developed by the Laravel team.
Built on top of PHP-CS-Fixer, it automatically formats code according to Laravel coding standards.
This makes the code cleaner, more readable, and easier to maintain.
In this article, “How to Configure Laravel Pint and How It Works,” we will explore its installation, configuration, and usage.
Laravel includes it by default in version 9.x and later. Developers can also install it in older applications.
Automating code formatting, it reduces manual effort and helps maintain a consistent codebase.
Installation of Laravel Pint
|
1 |
composer require laravel/pint --dev |
After installation, you can use the Laravel Pint package in two ways.
If you want Pint to automatically fix coding style issues, run the command below.
|
1 |
./vendor/bin/pint |
|
1 |
./vendor/bin/pint --dirty |
Understanding Common Laravel Pint Rules
(i) Align Multiline Comments
The align_multiline_comment rule fixes multiline comments that have become misaligned.
This improves readability and helps maintain a clean code structure.
(ii) Array Indentation
The array_indentation rule ensures that arrays are properly indented.
It helps keep array structures clean, consistent, and easy to read.
(iii) Laravel Pint Array Syntax Rule
The Laravel Pint array_syntax rule converts the older array () syntax to the modern [] syntax.
This helps maintain consistency and follows current PHP coding standards.
(iv) Blank Line After Namespace
The blank_line_after_namespace rule adds a blank line after a namespace declaration.
This improves code organization and readability.
(v) Blank Line After Opening Tag
The blank_line_after_opening_tag rule ensures there is a blank line after the opening PHP tag.
It helps keep files clean and consistently formatted.
(vi) Combine Consecutive Issets
The combine_consecutive_issets rule merges multiple isset () checks into a single statement.
This makes the code shorter, cleaner, and easier to maintain.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * Before */ if (isset($abc) && isset($xyz)) { } /** * After */ if (isset($abc, $xyz)) { } |
(vi). Combine Consecutive Unsets.
The combine_consecutive_unsets rule merges multiple consecutive unset () statements into a single statement.
This reduces repetitive code and makes it cleaner and more concise.
|
1 2 3 4 5 6 7 8 9 10 |
/** * Before */ unset($array['demo-1']); unset($array['demo-2']); /** * After */ unset($array['demo-1'], $array['demo-2]) |