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!