Laravel Pint: The Secret Weapon for Clean Code!

May 16, 2024

Laravel Pint: PHP Code Style Fixer

 

Laravel Pint is an opinionated PHP code style fixer for Laravel applications.

 

What does opinionated mean?

  • It enforces a specific coding style
  • Minimal configuration needed
  • Encourages consistency in Laravel projects

 

In other words Laravel Pint comes with strong defaults and expect you to follow a specific set of best practices.

 

It is built on top of PHP-CS-Fixer.

 

It is designed to automatically format PHP code according to Laravel’s coding standards.

 

Key Features of Laravel Pint

  1. Zero configuration required
  2. Opinionated
  3. Fast and minimal
  4. Customizable
  5. PSR Compliance - PSR-1, PSR-2, and PSR-12 standards.

 

How to Use Laravel Pint

 

1. Install Laravel Pint

 

composer require laravel/pint --dev

 

 

2. Run Laravel Pint

 

vendor/bin/pint

 

3. Custom Configuration (Optional)

 

Create  a pint.json file in the root of your project (sample config file): 

 

{
   "preset": "laravel",
   "rules": {
       "array_syntax": {"syntax": "short"}
   },
   "exclude": ["vendor"]
}

 

 

Go to the docs for more.

 

4. Dry Run (Check Without Fixing)

 

vendor/bin/pint --test

 

5. Running Pint on Specific Files

 

vendor/bin/pint app/Http/Controllers

 

Why Use Laravel Pint?

  1. Saves Time – No manual formatting needed.
  2. Maintains Consistency – Ensures all developers in a project follow the same style.
  3. Improves Readability
  4. Integrates with CI/CD – Can be used in GitHub Actions or pipelines.

     

Reach out to me! Find me on linkedin!

 

Want to stay updated? Join my newsletter and get a weekly report on the most exciting industry news! πŸš€