Code Quality with Laravel Pint 0 (0)
Maintaining a standard of code quality in a codebase particularly when collaborating with multiple developers can pose significant challenges. Each developer may have their coding style, standards, and skill levels. To ensure uniformity and organization across your codebase it is crucial to incorporate a tool that enforces coding standards enhances code readability and resolves issues such as indentation, missing spaces, and misplaced braces.
In this article, we will introduce you to a tool called Laravel Pint. We have seamlessly integrated Laravel Pint into our Laravel backend development workflow to achieve code quality and sustainability. This integration has become a part of our development process due to the absence of Laravel Pint, in Laravel 8.x by default; however, it has been incorporated as a core component starting from Laravel 9.x.
As per the official Laravel documentation, “Pint is automatically installed with all new Laravel applications, so you can start using it right away.” However, if you’re using an older version of Laravel, you can install Laravel Pint with Composer using the following command:
composer require laravel/pint --dev
While Pint doesn’t require any configuration, you can customize its behavior to suit your needs. To do this, create a
pint.json file in your project’s root folder. This file allows you to customize the preset (a set of rules) that Pint uses to fix code style issues. For example:
Another available preset is “psr12“. You can also enable or disable specific rules, exclude folders, or exclude specific files based on patterns or exact paths.
Additionally, you have the flexibility to define your own set of rules and customize how Pint fixes code issues. Since Pint is built on top of PHP-CS-Fixer, you can refer to the list of available rules on the PHP-CS-Fixer GitHub page.
Fix Your Code Quality with Pint
Running Pint on your project is straightforward. Simply use the following command:
You can customize Pint’s behavior using various options, such as:
- To run Pint in verbose mode, use the following command:
- Run Pint on a specific folder (e.g., “app”):
- Limit Pint to only modify files with uncommitted changes according to Git:
Execute these commands from your project’s root folder. Depending on your environment (e.g., virtual machine or container), you may need to retrieve the changed files.
For more details on the most commonly used options, consult the official Laravel documentation.
Automate Code Quality with GitHub Actions
One effective approach is to integrate Pint into your GitHub project using GitHub Actions. By doing this, you can ensure that Pint runs with every push to your repository. This automated process helps you catch and fix issues promptly.
To achieve this, follow these steps:
- Create a GitHub Actions workflow that includes running Pint.
- Configure the workflow to trigger on pushes to your repository.
For a detailed guide on setting up GitHub Actions for Laravel Pint, please refer to this article: https://laravelmagazine.com/run-laravel-pint-as-part-of-your-ci-pipeline-with-github-actions
Maintaining code quality is a crucial aspect of software development, especially when working with a team on a large codebase. Laravel Pint, with its easy installation, configuration options, and powerful code-fixing capabilities, is a valuable addition to your development workflow. By enforcing coding standards, enhancing code readability, and automating the code quality check process, Laravel Pint ensures that your codebase remains clean and consistent.
Whether you’re working on a Laravel project or any other PHP-based application, integrating Laravel Pint can significantly improve your coding experience and the overall quality of your software. Consider making Laravel Pint a part of your development toolkit to enjoy smoother collaboration, maintainable code, and higher code quality.