What is Laravel Pint
as in the definition by the official repository,
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
and in me, Pint is the most sophisticated code formatter I have ever used in the reference of PHP. You can find more details about laravel/pint here.
Installation
composer require laravel/pint --dev
General Usage
To use the pint binary all you have to do is run the following command in the root of your laravel project.
./vendor/bin/pint
Configuration
The default setup of laravel pint does not require any configuration and as per the official docs, it for PSR-12 Style Guide. Still, if you are unhappy with some default rules you can change them by creating a pint.json in the root of your project
and adding the preset to it.
Currently the following presets are supported
- psr12
- laravel
- Symfony
Since Pint is based on PHP-CS-Fixer the configuration rules can be found here.
Sample pint.json
{
"preset": "symfony",
"rules": {
"concat_space": {
"spacing": "one"
}
}
}
Approaches
For using pint in vscode, I have the following two approaches.
- Using the Extension (you can find the extension here Vscode Laravel Pint Extension)
- Creating a vscode task for the laravel pint. The second approach is explained below in detail.
So far we are good to use laravel/Pint, but opening the terminal and running a binary every time you need to format id one hell of a job. To reduce that effort we will bind laravel/pint with our VS Code with the help of VS Code Task
Creating VS Code task for Laravel Pint
- To create a task create a
.vscode directory in the root of your project
, if you don't have one. - Add a
new file named tasks.json
to the .vscode directory - Add the following content to the newly created file
{
"version": "2.0.0",
"tasks": [
{
"label": "Pint Formatter",
"type": "shell",
"command": "./vendor/bin/pint",
"problemMatcher": [],
"presentation": {
"reveal": "silent"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Modification
As commented by Gsinti, you can modify the command to work with your current file. Update your command as follows.
"command": "./vendor/bin/pint ${file}",
You can run this task directly from the Tremial -> Run Tasks
from the navigation but will do it a bit simpler.
Adding KeyBinding (Keyboard Shortcut) to run Laravel pint
- Open the Keyboard shortcut panel, either from
file -> preferences -> keyboard Shortcuts
orCtrl+k Ctrl+s
- Ones open click on the file icon on the top to open the JSON file of the keyboard shortcuts.
- Once open add the following lines to the file.
[
{
"key": "ctrl+shift+l",
"command": "workbench.action.tasks.runTask",
"args": "Pint Formatter"
}
]
Now all you have to do is hit Ctrl + Shift + l
and your laravel project will formatted with laravel pint.
Conclusion
I have been using the unsatisfactory formatters for a long time. All of them have one thing but lack another. Laravel Pint is one who has it all, combining it with vscode is like proving your laravel project a superpower.
Feel free to pick your brain in the comments.