I stand up for children in need. Please join me in helping this family.

Skip to content
Steven Roland

Streamlining Code Style with Laravel Pint

Laravel Pint is an opinionated PHP code style fixer that simplifies the process of maintaining consistent code style across your Laravel projects. Built on top of PHP-CS-Fixer, Pint offers a zero-configuration approach to code formatting, making it an essential tool for Laravel developers.

Getting Started with Laravel Pint

Laravel Pint is included by default in recent Laravel releases. For older projects, you can install it via Composer:

composer require laravel/pint --dev

Basic Usage

To run Pint on your entire project, simply execute:

./vendor/bin/pint

This command will automatically fix code style issues across your Laravel application.

Targeted Formatting

You can also run Pint on specific files or directories:

./vendor/bin/pint app/Models

./vendor/bin/pint app/Http/Controllers/UserController.php

Inspecting Changes

To see a detailed list of changes without actually modifying files, use the --test option:

./vendor/bin/pint --test

Suggested Usages

  • Pre-commit Hook: Ensure code style consistency before each commit:

    Create a .git/hooks/pre-commit file with:

    #!/bin/sh
    ./vendor/bin/pint
  • CI/CD Pipeline: Integrate Pint into your continuous integration workflow:

    For GitHub Actions, create .github/workflows/pint.yml:

    name: Laravel Pint
    on: [push]
    jobs:
      laravel-pint:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - name: "laravel-pint"
            uses: aglipanci/[email protected]
  • Editor Integration: Configure your IDE to run Pint on save. For VS Code, add to settings.json:

    "editor.formatOnSave": true,
    "[php]": {
      "editor.defaultFormatter": "open-southeners.laravel-pint"
    }
  • Custom Configuration: Create a pint.json file in your project root to customize rules:

    {
      "preset": "laravel",
      "rules": {
        "class_attributes_separation": {
          "elements": {
            "method": "one"
          }
        },
        "not_operator_with_successor_space": true
      }
    }
  • Exclude Files: Prevent Pint from formatting certain files by adding a .pintignore file:

    *.blade.php
    config/*.php

Best Practices

  • Regular Use: Run Pint regularly to maintain consistent code style.

  • Team Adoption: Ensure all team members use Pint to avoid style-related conflicts.

  • Version Control: Include your pint.json in version control for team-wide consistency.

  • Gradual Implementation: For large existing projects, consider implementing Pint gradually, focusing on new or modified files.

Laravel Pint simplifies the process of maintaining clean, consistent code across your Laravel projects. By integrating it into your development workflow, you can focus more on writing great code and less on style debates. Whether you're working solo or in a team, Pint helps ensure your codebase remains clean and professional.

Remember, while Pint is opinionated, it's also flexible. Don't hesitate to customize its rules to fit your team's specific needs and coding standards. Happy coding!

Support My Work

If you enjoy my content, consider supporting me through Buy Me a Coffee or GitHub Sponsors.

Buy Me A Coffee
or

More posts

Simplifying Routing with Laravel Folio: A Comprehensive Guide

Laravel Folio simplifies routing by using Blade templates to generate routes automatically. Install Folio, create pages in the 'pages' directory, and enjoy automatic routing. It supports dynamic routes, model binding, and middleware. Ideal for static pages, blogs, documentation sites, and admin panels.

Laravel's unique Validation Rule: Advanced Usage and Exceptions

This post explains Laravel's advanced unique validation rule, focusing on the unique:table,column,except,idColumn syntax. It provides real-world examples for updating user profiles, product SKUs, and organization subdomains, along with best practices and considerations.