How to make code clean and consistent.


In this blog, we will discuss the default CLI app that comes with Laravel 9 to make your code clean and consistent. If you're a developer immersed in modern PHP, you know how essential it is to maintain a clean and consistent code style. Over the years, PHP has evolved, and with initiatives like PHP-FIG and the release of PSRs, the community has emphasized code quality, consistency, and adherence to standards.
As we all know Laravel has huge open-source packages to help in every situation, we have a package to help us in this situation also and the package is Laravel Pint. Laravel Pint is a zero-dependency PHP code style fixer for minimalists - built on top of PHP-CS-Fixer. For Laravel 9 and other latest versions, this package is included in the default installation of Laravel, for older versions you can install this by using the composer.
Automating code style checks and fixes based on a preset, making it effortless to ensure your code conforms to best practices. Let's dive into the world of Laravel Pint and see how it simplifies code styling by using some simple steps.
Installation
Install the package for the Larvel 8 and other older versions.
composer require laravel/pint --dev
Using Laravel Pint
As we have discussed earlier Laravel pint can be used without doing any configuration you can fix the code style issue of your application by just running the below command.
./vendor/bin/pint
No need to worry about configurations, presets or any other setup, Changes will be done according to the PSR-12 styling, and any issue will be automatically fixed. It will work like a magical command to keep your code clean and consistent!
Use case scenarios
Run the command to check and fix all the files of your application
./vendor/bin/pint
Run on a specific Folder
./vendor/bin/pint app/Models
Run on a specific File
./vendor/bin/pint app/Models/User.php
View the list of updated files by Pint
./vendor/bin/pint -v
Check the files without changing them
./vendor/bin/pint --test
Run the test only on the files with uncommitted changes
./vendor/bin/pint --dirty
Configuration
By default, we do not need any configuration to run the pint but we can change the configuration according to your need. You can change the preset rules and inspected folders by creating the pint.json
file in your root directory.
Preset
Preset defines a set of rules to fix the code style issue in the file.
By default, the preset value is Laravel so it will follow the opinionated coding style of Laravel. You can change the value as per your needs in
pint.json
supported presets arelaravel
,per
,psr12
, andsymfony
.{ "preset": "psr12" }
Rules
As presets are already a set of predefined rules, probably you will not change the configuration files, however, there are options available to change the rules.
As Pint is built on PHP-CS-Fixer you can use any of it's rules available for styling.
{ "preset": "psr12", "rules": { "simplified_null_return": true, "braces": false, "new_with_braces": { "anonymous_class": false, "named_class": false } }
Excluding Folders/Files
Pint will check all the
.php
files except thevendor
directory, you can exclude the files and folders you want{ "exclude": [ "my-specific/folder" ], "notName": [ "*-my-file.php" ], "notPath": [ "path/to/excluded-file.php" ] }
Conclusion
Laravel Pint is a must install tool for any PHP developer working on projects, packages, or anything else that involves writing PHP code. With its simplicity, zero-dependency approach, and flexibility in configuration, it's a powerful addition to your toolkit. Say goodbye to messy PHP arrays and complex rule configurations. Laravel Pint streamlines the process of maintaining a clean and consistent code style. Start using it in your projects today and experience the magic of effortless code styling. ๐
Subscribe to my newsletter
Read articles from Abhishek Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Abhishek Jha
Abhishek Jha
Behold, the coding maestro! ๐ฉ Armed with PHP, Laravel, Linux, HTML, JavaScript, Jquery & Vue, I conquer both frontend and backend realms. ๐ Pressure? No match for me. I deliver excellence and exceed expectations with flair. ๐ Stay ahead, stay cool, and let's conquer the coding universe together! ๐ป