Laravel Pint is an opinionated PHP code style fixer for your Laravel projects. It helps ensure your codebase adheres to a consistent coding style. Here’s how you can configure Laravel Pint in a Laravel 11 project:
Step-by-Step Guide
1. Install Laravel Pint
First, add Laravel Pint as a development dependency in your project:
composer require laravel/pint --dev
2. Run Laravel Pint
To check the current code style in your project, you can run Pint with the following command:
./vendor/bin/pint
By default, this command will analyze and fix the code style issues based on the preset rules.
3. Create a Configuration File
You can create a .pint.json
configuration file in the root of your project to customize Pint’s behavior.
touch .pint.json
4. Configure Pint
Edit the .pint.json
file to configure Pint according to your preferences. Here is an example configuration:
{
"preset": "laravel",
"rules": {
"binary_operator_spaces": {
"default": "align_single_space"
},
"array_syntax": {
"syntax": "short"
},
"trailing_comma_in_multiline": {
"elements": ["arrays"]
}
},
"exclude": [
"vendor",
"storage",
"bootstrap/cache"
]
}
- preset: Defines the coding style preset to use (e.g., "laravel", "psr2", etc.).
- rules: Custom rules to override the preset rules.
- exclude: Directories or files to exclude from Pint's analysis.
5. Running Pint with Custom Configuration
After creating the configuration file, run Pint to apply the custom rules:
./vendor/bin/pint
6. Integrate Pint into Your Workflow
You can add Pint to your continuous integration (CI) pipeline to ensure code style consistency across your team. Here is an example for GitHub Actions:
# .github/workflows/pint.yml
name: Laravel Pint
on: [push, pull_request]
jobs:
pint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
- name: Install dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader
- name: Run Laravel Pint
run: ./vendor/bin/pint
7. Fixing Code Style Issues
If Pint finds code style issues, it will automatically fix them if possible. You can manually review and fix any remaining issues as needed.
8. Additional Pint Commands
Check code without fixing:
./vendor/bin/pint --test
Fix specific file or directory:
./vendor/bin/pint path/to/file.php
Conclusion
By configuring Laravel Pint in your Laravel 11 project, you can ensure that your code adheres to a consistent style, making it easier to read and maintain. This setup process includes installing Pint, creating a custom configuration file, and integrating Pint into your development workflow. With these steps, you can maintain a high standard of code quality in your Laravel projects.
0 Comments