Consistent code style improves readability and reduces code review friction. Laravel Pint wraps PHP-CS-Fixer with sensible defaults. At ZIRA Software, Pint standardizes code style across all PHP projects.
Installation
# Install via Composer
composer require laravel/pint --dev
# Run Pint
./vendor/bin/pint
# Preview changes without applying
./vendor/bin/pint --test
# Verbose output
./vendor/bin/pint -v
Basic Usage
# Fix all files
./vendor/bin/pint
# Fix specific files
./vendor/bin/pint app/Models/User.php
# Fix specific directory
./vendor/bin/pint app/Http/Controllers
# Show what would change
./vendor/bin/pint --test --preset laravel
Before and After
// Before Pint
class UserController extends Controller{
public function index(Request $request){
$users=User::query()
->where('active',true)
->orderBy('name')
->get();
return view('users.index',compact('users'));
}
}
// After Pint
class UserController extends Controller
{
public function index(Request $request)
{
$users = User::query()
->where('active', true)
->orderBy('name')
->get();
return view('users.index', compact('users'));
}
}
Configuration
// pint.json
{
"preset": "laravel",
"rules": {
"simplified_null_return": true,
"braces": {
"allow_single_line_closure": true
},
"new_with_braces": {
"anonymous_class": false,
"named_class": true
}
},
"exclude": [
"vendor",
"storage",
"bootstrap/cache"
]
}
Available Presets
# Laravel style (default)
./vendor/bin/pint --preset laravel
# PSR-12
./vendor/bin/pint --preset psr12
# Symfony
./vendor/bin/pint --preset symfony
# Per-project (PER)
./vendor/bin/pint --preset per
Custom Rules
{
"preset": "laravel",
"rules": {
"binary_operator_spaces": {
"operators": {
"=>": "align_single_space_minimal"
}
},
"concat_space": {
"spacing": "one"
},
"ordered_imports": {
"sort_algorithm": "alpha"
},
"single_quote": true,
"trailing_comma_in_multiline": true,
"no_unused_imports": true
}
}
Git Pre-Commit Hook
#!/bin/sh
# .git/hooks/pre-commit
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".php$")
if [ -n "$STAGED_FILES" ]; then
echo "Running Pint on staged PHP files..."
./vendor/bin/pint $STAGED_FILES
git add $STAGED_FILES
fi
exit 0
Composer Scripts
{
"scripts": {
"pint": "./vendor/bin/pint",
"pint:test": "./vendor/bin/pint --test",
"pint:dirty": "./vendor/bin/pint --dirty",
"format": [
"@pint"
],
"lint": [
"@pint:test",
"./vendor/bin/phpstan analyse"
]
}
}
GitHub Actions Integration
# .github/workflows/code-style.yml
name: Code Style
on: [push, pull_request]
jobs:
pint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
- name: Install Dependencies
run: composer install --no-interaction
- name: Run Pint
run: ./vendor/bin/pint --test
IDE Integration
// VS Code settings.json
{
"editor.formatOnSave": true,
"[php]": {
"editor.defaultFormatter": "open-southeners.laravel-pint"
}
}
// PhpStorm: External Tools
// Program: $ProjectFileDir$/vendor/bin/pint
// Arguments: $FilePath$
// Working directory: $ProjectFileDir$
Team Workflow
# package.json or composer.json scripts
{
"scripts": {
"pre-commit": [
"@pint:dirty"
],
"ci": [
"@pint:test",
"@phpstan",
"@test"
]
}
}
# Developer workflow
git add .
composer pint:dirty # Fix only changed files
git commit -m "Feature: Add user export"
Conclusion
Laravel Pint eliminates code style debates with sensible defaults. Zero-configuration setup with easy customization makes it essential for PHP projects.
Need code quality tooling? Contact ZIRA Software for development workflow optimization.