Updated 5 December 2023
Laravel Pint is hotest package and CLI tool developed by the Laravel team, which is wrapper around PHP CS Fixer that is my go-to code standards tool. It is fixes coding styles to make the code more readable for developers. It is a package that is included in Laravel 9.x.x and latest verion and it is built on top of the PHP-CS-Fixer package. It can be used in both fresh Laravel applications and old ones.
It will be very usefull, especially when you have a team because team members will be avoiding using different coding styles.
Laravel pint requires PHP version 8, if you install it on a lower PHP version you may get an error in the composer.
1 |
composer require laravel/pint --dev |
After installation, you can choose two ways that the Laravel Pint package offers. First one is , you can directly allow the package to make changes in your project, and second one is, you can ask the package to tell you what changes will be implemented in your project to fix the coding style or to follow the PSR standards.
If you want Pint to fix coding styles without letting you know then run the below command.
1 |
./vendor/bin/pint |
1 |
./vendor/bin/pint --dirty |
This rule align_multiline_comment
will allow you to fix any comments that I call “borked”. They have all gone out of alignment and look weird. It is not a significant thing in terms of your code, but it is an annoyance when reading through it as your eyes will be drawn to it instead of what you want to focus on.
The rule array_indentation
will allow you to fix any arrays you are creating that has, again, become “borked” for some reason. Another code cleaning rule that will tidy up where spaces may be used in the wrong place etc. etc.
The array_syntax
rule is one that you may not need, depending on the age of your code. This rule will change the old array()
syntax to the new []
syntax. I keep this in case I have old code or am working with multiple developers who may fall into old habits.
The blank_line_after_namespace
rule is a housekeeping rule that I use to ensure that there is always a blank line under the namespace declaration on any class.
The blank_line_after_opening_tag
rule is similar to the previous rule but enforces a blank line after the opening PHP tag. I like to keep my code organized and uniform – these rules enable this.
The combine_consecutive_issets
rule is one that taught me that I could use more than one argument in an isset check -which was something new to me. This will convert any code that combines one or more isset checks into one clean check.
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)) { } |
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]) |
Laravel Pint package helps to clean the code and it fixes the coding styles. In this article, we have learned about installation and usage of Laravel Pint.
If you’re looking to build something amazing with Laravel and Vue, why not consider hire laravel developers who can bring your vision to life? You can also checkout our Bagisto Extensions.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.