Laravel Pint for code formatting

Laravel Pint is a modern PHP code formatting tool specifically designed for Laravel applications. It helps maintain consistent and clean coding standards, improving readability and collaboration across your development team.

What is Laravel Pint?

Laravel Pint simplifies code formatting by automatically enforcing coding standards without requiring complex configuration. Built upon PHP-CS-Fixer, it provides a hassle-free solution to keep your Laravel project well-organized and aligned with best practices.

Why Use Laravel Pint?

  • Consistency: Standardizes code styling across your project, reducing friction and promoting uniformity.
  • Enhanced Readability: Clearly formatted code makes it easier to understand, review, and debug.
  • Automation: Eliminates repetitive manual formatting tasks, significantly saving developer time.
  • Ease of Integration: Simple setup process for immediate integration into any existing or new Laravel project.
  • Better Collaboration: Improves team workflow by reducing code conflicts and misunderstandings related to coding styles.

How to Set Up Laravel Pint

Laravel Pint coms

Follow these simple steps to integrate Laravel Pint into your project:

Step 1: Install Laravel Pint

Using Composer, install Pint into your Laravel application:

Bash
composer require laravel/pint --dev

Step 2: Running Laravel Pint

After installation, you can immediately use Pint to format your code. Run the following command from your project root directory:

./vendor/bin/pint

Pint will scan your PHP files and automatically fix formatting issues according to the default Laravel standards.

Step 3: Customize Laravel Pint (Optional)

If you prefer custom coding standards, you can create a configuration file named pint.json in your project root:

{
    "preset": "laravel",
    "rules": {
        "binary_operator_spaces": {
            "operators": {
                "=>": "align_single_space"
            }
        }
    }
}

Adjust rules as per your project’s coding standards.

Setting up Laravel Pint in VsCode or Cursor

https://devinthewild.com/article/laravel-pint-formatting-vscode-phpstorm

Integrating Laravel Pint into Git Hooks (Optional)

To automate formatting before every commit, add Pint to your pre-commit hooks:

composer require captainhook/captainhook --dev
./vendor/bin/captainhook install

Then, add the following to your captainhook.json file:

{
    "pre-commit": {
        "actions": [
            {
                "action": "./vendor/bin/pint --test",
                "options": []
            }
        ]
    }
}

Conclusion

Laravel Pint is a powerful tool for keeping your Laravel applications clean and professional. Integrating Pint into your workflow ensures your codebase remains consistent, saving your team time and effort. Try it today to experience smoother development and collaboration!