Laravel Pint & VS Code: Automate Your Code Formatting

Harish Kumar · · 6208 Views

Laravel Pint is an opinionated PHP code style fixer built on top of PHP-CS-Fixer, designed to simplify the process of ensuring clean and consistent code style in Laravel projects.

In this comprehensive guide, we will explore how to integrate Laravel Pint with VS Code, one of the most popular code editors among Laravel developers. We will delve into the installation process, configuration options, and best practices for using Laravel Pint in your Laravel projects. So let's dive in and automate your code style with Laravel Pint!

Table of Contents

  1. What is Laravel Pint?

  2. Installation

  3. Running Laravel Pint

  4. Configuring Laravel Pint

  5. Integrating Laravel Pint with VS Code

  6. Setting Up VS Code Tasks

  7. Running Laravel Pint on Save

  8. Customizing Pint Rules

  9. Using Laravel Pint in PhpStorm

  10. Code Quality with Laravel Pint

  11. Laravel Pint and the Laravel Community

  12. Conclusion

What is Laravel Pint?

Laravel Pint is an opinionated PHP code style fixer for minimalists. It is built on top of PHP-CS-Fixer and makes it simple to ensure that your code style stays clean and consistent. Laravel Pint comes pre-installed with new Laravel installations, making it readily available for use in your Laravel projects. It eliminates the need for manual code formatting and enforces code style standards in your codebase.

Installation

To start using Laravel Pint, you need to install it as a dependency in your Laravel project. The installation process is straightforward and can be done via Composer. Open your terminal and navigate to your Laravel project directory, then run the following command:

composer require laravel/pint --dev

This command will install Laravel Pint as a development dependency in your project. The --dev flag ensures that it is only installed for development purposes.

Running Laravel Pint

Once Laravel Pint is installed, you can run it using the following command:

./vendor/bin/pint

This command will execute Laravel Pint and analyze your project's codebase for any code style violations. Laravel Pint will provide a detailed list of files that require fixes, allowing you to review and apply the necessary changes.

Configuring Laravel Pint

By default, Laravel Pint does not require any configuration and follows the PSR-12 coding style guide. However, if you wish to customize the code style rules, you can create a pint.json file in the root of your Laravel project. This file allows you to define presets and rules for Laravel Pint to follow.

For example, if you want to use the Laravel preset, your pint.json file can look like this:

{
  "preset": "laravel"
}

You can also define custom rules within the rules section of the pint.json file. Laravel Pint provides a wide range of rules that you can customize to fit your project's specific requirements.

Integrating Laravel Pint with VS Code

To integrate Laravel Pint with VS Code, you can use the Laravel Pint extension available in the VS Code marketplace. Search for "Laravel Pint" in the Extensions tab, and install the extension.

Once installed, you can set Laravel Pint as the default formatter for your PHP files in VS Code. Open the VS Code settings (either user or workspace preferences) and add the following configuration:

{
  "[php]": {
    "editor.defaultFormatter": "open-southeners.laravel-pint"
  }
}

This configuration ensures that Laravel Pint is used to format PHP files when you trigger the format command in VS Code.

Setting Up VS Code Tasks

To further automate the code formatting process, you can set up a VS Code task to run Laravel Pint. Create a .vscode directory in the root of your Laravel project if it doesn't already exist. Inside the .vscode directory, create a new file named tasks.json. Add the following content to the tasks.json file:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Pint Formatter",
      "type": "shell",
      "command": "./vendor/bin/pint",
      "problemMatcher": [],
      "presentation": {
        "reveal": "silent"
      },
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

This task configuration defines a task named "Pint Formatter" that runs the pint binary command. It is set as the default build task, which means it will be triggered when you run the build command in VS Code.

Running Laravel Pint on Save

To automate the code formatting process further, you can configure VS Code to run Laravel Pint whenever you save a PHP file. To achieve this, you can use the "Run on Save" extension available in the VS Code marketplace. Install the extension, and it will automatically trigger Laravel Pint whenever you save a PHP file in your Laravel project.

This ensures that your code is automatically formatted according to the Laravel Pint rules every time you save a file, eliminating the need for manual formatting.

Customizing Pint Rules

While Laravel Pint provides sensible defaults based on the Laravel coding standards, you may have specific code style requirements for your project. You can customize the Pint rules to meet your project's needs.

The Laravel Pint documentation provides a list of available rules that you can configure. Each rule has its own set of options and parameters that allow you to fine-tune the code style. You can modify rules such as indentation, line spacing, naming conventions, and more.

By customizing the Pint rules, you can ensure that your codebase adheres to your preferred coding standards and promotes code consistency.

Using Laravel Pint in PhpStorm

If you prefer using PhpStorm as your code editor, you can also integrate Laravel Pint into your workflow. Laravel News has published an article highlighting a recent update in PhpStorm that adds native support for Laravel Pint as a code formatter.

To enable Laravel Pint in PhpStorm, navigate to the quality tools settings in PhpStorm and configure Pint as the code formatter. This allows you to leverage Laravel Pint's automatic code formatting capabilities within the PhpStorm IDE.

Code Quality with Laravel Pint

Automated code formatting is just one aspect of maintaining code quality in Laravel projects. Laravel Pint helps enforce coding standards and ensures consistency in code style, but it is important to address other code quality aspects as well.

Other tools and practices that can complement Laravel Pint's code formatting include code linting, static analysis, and adhering to PHP coding best practices. By combining these tools and practices, you can enhance your code quality and productivity in Laravel development.

Laravel Pint and the Laravel Community

Laravel Pint is an open-source project maintained by the Laravel community. This means that it benefits from the collective expertise and contributions of Laravel developers worldwide. The Laravel community is known for its active support and engagement, making Laravel Pint a reliable tool for Laravel development.

By using Laravel Pint, you become part of the Laravel community and contribute to the improvement of Laravel code quality standards. You can participate in discussions, report issues, and contribute to the development of Laravel Pint on its GitHub repository.

Conclusion

Automating code style with Laravel Pint and integrating it with VS Code or PhpStorm can significantly boost your Laravel development workflow. By eliminating the manual effort of code formatting, you can focus on writing clean and maintainable code. Laravel Pint ensures that your code adheres to Laravel's coding standards, promoting code consistency and readability.

In this guide, we explored the installation and configuration steps for Laravel Pint, as well as its integration with VS Code and PhpStorm. We also discussed the importance of code quality and the role Laravel Pint plays in maintaining clean code.

So why wait? Start automating your code style with Laravel Pint and enjoy a more efficient and productive Laravel development experience!

0

Please login or create new account to add your comment.

0 comments
You may also like:

Exploring Asymmetric Property Visibility in PHP 8.4

The release of PHP 8.4 introduces a powerful new feature: Asymmetric Property Visibility, enabling developers to define separate visibility rules for reading and writing properties. (...)
Harish Kumar

What's New in PHP 8.4: Key Enhancements and Updates

As PHP 8.4's release on November 21, 2024, approaches, it's clear that PHP continues to evolve and delight its developer community. For those who have been coding with PHP since (...)
Harish Kumar

Introducing Tools to Supercharge PHP-FPM Efficiency and Monitoring

PHP-FPM stands for PHP FastCGI Process Manager. It’s an improved way to manage PHP processes that makes web applications faster and more efficient. Instead of running each PHP (...)
Harish Kumar

Building a Real-Time Chat App with Laravel Reverb and Nuxt 3

Building a real-time chat application is a great way to understand the power of WebSockets and real-time communication. In this tutorial, we will walk through creating a Real-Time (...)
Harish Kumar

How to Set Up Nuxt 3 Authentication with Laravel Sanctum (Step-by-Step Guide)

In modern web development, securing your application’s authentication process is a top priority. For developers building Single Page Applications (SPA) or Server-Side Rendered (...)
Harish Kumar

PHP 8.4 Property Hooks: The Ultimate Guide for Developers

PHP 8.4, coming in November 2024, introduces a new feature called property hooks. This feature makes it easier to work with class properties by allowing you to define custom behavior (...)
Harish Kumar