Code Quality with Laravel Pint

Code Quality with Laravel Pint

By on Tue Nov 7 in code quality, Laravel, PHP


0
(0)

Introduction

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.

Installation

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

For more information, you can refer to the README file on GitHub or the official Laravel documentation.

Configuration

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:

{
    "preset": "laravel"
}

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:

./vendor/bin/pint

You can customize Pint’s behavior using various options, such as:

  1. To run Pint in verbose mode, use the following command:
   ./vendor/bin/pint -v
  1. Run Pint on a specific folder (e.g., “app”):
   ./vendor/bin/pint app/
  1. Limit Pint to only modify files with uncommitted changes according to Git:
   ./vendor/bin/pint --dirty

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:

  1. Create a GitHub Actions workflow that includes running Pint.
  2. 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

Conclusion

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.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.