Maintaining a consistent code style is crucial for any software project’s readability, maintainability, and collaboration. In the Laravel ecosystem, Laravel Pint emerges as a powerful tool designed to automate code style fixing, effortlessly ensuring that your PHP code adheres to predefined standards.
In this guide, we will explore the Laravel Pint package, its features, installation, configuration, and integration of Laravel Pint, providing a comprehensive guide to enhance the Laravel development workflow.
Table of Contents
What is Laravel Pint Package?
Laravel Pint is an opinionated PHP code style fixer tailored for minimalists. Built on top of PHP-CS-Fixer, it simplifies the process of maintaining clean and consistent code styles in Laravel projects. By default, Pint adheres to Laravel’s coding style but offers flexibility through customizable presets and rules.
Why Use Laravel Pint Package?
Using Laravel Pint in your development workflow offers multiple benefits:
- Zero Configuration: Pint works out-of-the-box with Laravel’s default coding style, requiring minimal setup.
- Automated Code Formatting: Saves time by auto-fixing code style issues.
- Consistency across Projects: Ensures all team members follow the same coding standards.
- Integration with PHP-CS-Fixer: Leverages the robustness of PHP-CS-Fixer, providing a reliable foundation for code style fixing.
- Integration with CI/CD Pipelines: Easily integrates into automated workflows.
- Composer Integration: Easily installable via Composer, fitting seamlessly into Laravel’s package management system.
- Lightweight and Fast: No unnecessary dependencies, making it a quick solution.
- Customizable Rules: You can define your own rules according to project needs.
- Customizable Presets: Supports various coding standards, including PSR-12 and PER, allowing developers to choose or define their preferred style.
Installing Laravel Pint Package
Laravel Pint requires PHP 8.1 or higher. You can install it globally or on a per-project basis.
Global Installation
Run the following command to install Pint globally:
composer global require laravel/pint
After installation, you can use it in any Laravel project by running:
pint
Project-Based Installation
To install Pint only for a specific Laravel project, run:
composer require laravel/pint --dev
The --dev
flag ensures that Pint is added as a development dependency.
Using Laravel Pint Package
Once installed, running Pint is straightforward. Navigate to your project’s root directory and execute:
./vendor/bin/pint
This command will analyze your codebase and automatically fix any style issues according to the default Laravel preset. For a dry run, where you can see the changes without applying them, use:
./vendor/bin/pint –test
To view detailed information about the changes Pint would make, add the verbose flag:
./vendor/bin/pint --test –v
To format a specific file or directory, use:
pint path/to/file.php
Configuring Laravel Pint Package
While Pint operates effectively with its default settings, you can customize its behavior by creating a pint.json configuration file in your project’s root directory. This JSON file allows you to specify presets and individual rules.
Using Presets
Presets define a set of rules that Pint will apply. To use the PSR-12 coding standard, for example, your pint.json would look like:
{
"preset": "psr12"
}
Laravel Pint supports various presets, including:
- laravel: The default Laravel coding style.
- psr12: The PSR-12 coding standard.
- per: The PHP Extended Ruleset, which builds upon PSR-12.
Creating a Custom Configuration
Generate a pint.json file in the root of your project:
pint --generate
This will create a file like this:
{
"preset": "laravel",
"rules": {
"array_syntax": {"syntax": "short"}
}
}
You can modify this file to customize the rules based on your project needs.
Customizing Rules
To tailor specific rules within a preset, you can define them in the rules section of your pint.json. For instance, to disable the braces rule:
{
"preset": "psr12",
"rules": {
"braces": false
}
}
This flexibility allows you to enforce coding standards that align with your project’s requirements.
Integrating Laravel Pint Package with PhpStorm
For developers using PhpStorm, integrating Laravel Pint enhances the development experience by providing real-time code style feedback.
Installation in PhpStorm
- Ensure Composer is Installed: PhpStorm relies on Composer for managing PHP dependencies. Ensure its installed and initialized in your project.
- Install Laravel Pint: If not already installed, add Pint to your project using Composer as described earlier.
- Configure PhpStorm:
- Navigate to
Settings (Ctrl+Alt+S) > PHP > Quality Tools > Laravel Pint
. - PhpStorm should detect the Pint executable automatically. If not, specify the path manually
(./vendor/bin/pint)
. - Configure additional options, such as the path to your
pint.json
file and desired presets.
- Navigate to
Enabling Laravel Pint Inspection
- Go to
Settings > Editor > Inspections
. - Expand the
PHP > Quality Tools section
. - Check the box for
Laravel Pint validation
.
With these settings, PhpStorm will highlight code style issues in real-time, allowing for immediate corrections.
Integrating Laravel Pint Package with CI/CD
To enforce code styling in CI/CD pipelines, you can add Pint to your GitHub Actions workflow or other CI tools.
Example GitHub Actions setup:
name: Pint Code Style Check
on: [push, pull_request]
jobs:
pint:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Install Dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader
- name: Run Laravel Pint
run: ./vendor/bin/pint --test
This will ensure all commits and pull requests adhere to the coding standards.
Laravel Pint vs PHP-CS-Fixer
Laravel Pint is built on PHP-CS-Fixer, but it provides a simpler and more Laravel-friendly approach:
Feature | Laravel Pint | PHP-CS-Fixer |
Default Configuration | Yes | No |
Laravel Preset | Yes | No |
Customization | Yes | Yes |
Complexity | Low | Medium |
For developers working in Laravel, Pint is a hassle-free alternative with minimal configuration.
Conclusion
Laravel Pint is an invaluable tool for maintaining code quality and consistency in Laravel applications. With its zero-configuration setup, customizable presets, and seamless integration with tools like PhpStorm, it simplifies the process of enforcing coding standards. Whether you’re working solo or in a team, incorporating Pint into your workflow can significantly improve code readability and maintainability.
By leveraging Laravel Pint, you can focus more on writing quality code rather than spending time on manual style corrections. If you haven’t already, give it a try and experience a more streamlined coding process!
For more details and the latest updates, visit the official Laravel Pint repository on GitHub: Laravel Pint GitHub.