Skip to content

Instantly share code, notes, and snippets.

@er-pkumar
Last active May 30, 2024 10:51
Show Gist options
  • Save er-pkumar/fd521b193fcec72942510943126be8fc to your computer and use it in GitHub Desktop.
Save er-pkumar/fd521b193fcec72942510943126be8fc to your computer and use it in GitHub Desktop.
Laravel Pint is a tool built on top of PHP-CS-Fixer for PHP code that helps you style your code in a specific way, especially if you prefer a minimalist approach.

What?

Laravel Pint is a tool built on top of PHP-CS-Fixer for PHP code that helps you style your code in a specific way, especially if you prefer a minimalist approach. By default install by installing fresh laravel application, Pint does not require any configuration and will fix code style issues in your code by following the opinionated coding style of Laravel.

Why Pint?

  • Consistency: Pint enforces a consistent code style across your Laravel projects, making it easier for developers to read and understand each other's code.

  • Best Practices: Pint follows Laravel's best practices and coding standards, ensuring that your code adheres to recommended conventions.

  • Productivity: By automatically fixing code style issues, Pint saves developers time and effort, allowing them to focus on writing quality code rather than worrying about formatting.

  • Maintainability: Consistent code style improves code maintainability, making it easier to update and extend your Laravel applications in the future.

  • Community Support: Pint is widely used in the Laravel community, which means you can find resources, tutorials, and support from other developers familiar with this tool.

Installation

composer require laravel/pint --dev

How?

You can instruct Pint to fix code style issues by invoking the pint binary that is available in your project's vendor/bin directory:

./vendor/bin/pint

Running for specific directory/file:

./vendor/bin/pint app/Models

./vendor/bin/pint app/Models/User.php

If you want to see details along with fixes, try -v option when running pint:

./vendor/bin/pint app/Models/SampleTest.php -v

Output:


  ✓

  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Laravel  
    FIXED   .......................................................................................................................... 1 file, 1 style issue fixed  
  ✓ app/Models/SampleTest.php single_quote, trailing_comma_in_multiline, no_multiple_statements_per_line, statement_indentation, single_line_after_imports, binary  
  @@ -1,17 +1,20 @@
   <?php
   
  -use App\Models\Role;use App\Models\User;use Illuminate\Foundation\Testing\RefreshDatabase;
  +use App\Models\Role;
  +use App\Models\User;
  +use Illuminate\Foundation\Testing\RefreshDatabase;
   use Illuminate\Foundation\Testing\WithFaker;
   use Laravel\Sanctum\Sanctum;
   use Symfony\Component\HttpFoundation\Response;
  +
   uses(WithFaker::class, RefreshDatabase::class);

If you only want to inspect that can be achieved using --test

./vendor/bin/pint app/Models/SampleTest.php --test

Output:


  ⨯

  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Laravel  
    FAIL   ................................................................................................................................. 1 file, 1 style issue  
  ⨯ app/Models/SampleTest.php single_quote, trailing_comma_in_multiline, no_multiple_statements_per_line, statement_indentation, single_line_after_imports, binar…  

Fix only those files that have uncommitted changes according to Git, use the --dirty option:

./vendor/bin/pint --dirty

Configuration Options

Although Pint does not required any additionla configuration. However, if you wish to customize the presets, rules, or inspected folders, you may do so by creating a pint.json file in your project's root directory:

{
    "preset": "laravel"
}

Note: If you wish to use a pint.json from a specific directory, you may provide the --config option when invoking Pint:

pint --config vendor/my-company/coding-style/pint.json

Preset

Presets defines a set of rules that can be used to fix code style issues in your code. You can also specify a different preset by providing the --preset option to Pint:

pint --preset psr12

You can also specify preset in pint.json

Supported presets: laravel, per, psr12 and symfony.

Excluding Folder:

{
    "exclude": [
        "my-specific/folder"
    ]
}

Excluding File:

{
    "notName": [
        "*-my-file.php"
    ]
}

Pint will inspect all .php files in your project except those in the vendor directory.

If you want to ignore any specific path that can be achieved by adding following config

{
    "notPath": [
        "path/to/excluded-file.php"
    ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment