laravel/pint
Laravel Pint is an opinionated PHP code style fixer for minimalists. Built on PHP-CS-Fixer, it makes it easy to keep your Laravel and PHP projects clean and consistent with a simple, standardized formatting workflow.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require --dev laravel/pint
Pint is automatically registered as a dev dependency and adds a pint command to your project.
First Run:
php artisan pint
This runs Pint with the default Laravel preset, formatting all PHP files in your project.
Key Files to Know:
vendor/laravel/pint/src/Preset.php (Laravel preset).pint.json in your project root to override defaults (see Laravel Docs).First Use Case: Run Pint in a CI pipeline (e.g., GitHub Actions) to enforce consistent code style before merging:
# .github/workflows/pint.yml
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: php artisan pint --test
Pre-Commit Hook:
Integrate Pint with pre-commit (e.g., using Laravel Git Hooks) to auto-format staged files:
php artisan pint --dirty
--dirty: Only formats files modified in the current Git working directory.VS Code Integration: Add Pint to your editor for real-time feedback:
// .vscode/settings.json
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "bmewburn.vscode-intelephense",
"intelephense.format.enable": false,
"[php]": {
"editor.formatOnSave": false
}
}
Use the PHP Intelephense extension with Pint via a custom task.
Parallel Processing: Speed up large codebases with parallel execution (Windows/Linux/macOS):
php artisan pint --parallel --jobs=4 # 4 parallel processes
php artisan pint -p --jobs=4.Custom Presets:
Extend the default preset in pint.json:
{
"preset": "laravel",
"rules": {
"@PER-CS": true,
"fully_qualified_strict_types": true,
"no_unneeded_import_alias": true
}
}
CI Feedback:
Use --test to exit with non-zero status if changes are needed:
php artisan pint --test
--diff to show changes without applying them:
php artisan pint --diff
Composer Scripts:
Add Pint to composer.json scripts for one-liners:
"scripts": {
"lint": "pint",
"lint:fix": "pint --diff"
}
Run with:
composer lint
GitHub Actions: Cache Pint’s PHAR to avoid re-downloading:
- name: Cache Pint
uses: actions/cache@v3
with:
path: ~/.cache/pint
key: ${{ runner.os }}-pint
Monorepos:
Use --path-mode=relative to format files outside the project root:
php artisan pint --path-mode=relative --path=../monorepo/packages/*
Performance:
--jobs is set to 1 or omit --parallel if encountering hangs (see #272)..pintignore:
# .pintignore
storage/logs/*.log
Configuration Conflicts:
preset: "empty", Pint applies no rules. Override explicitly:
{
"preset": "empty",
"rules": {
"phpdoc_align": false,
"single_line_comment_spacing": true
}
}
Rule Interactions:
fully_qualified_strict_types: May conflict with no_unused_imports. Test with:
php artisan pint --dry-run
yoda_style: Disables when null comparisons are involved (see #213).Edge Cases:
Verbose Output:
Use -v or --verbose to diagnose issues:
php artisan pint -v
Cache Issues: Clear Pint’s cache if rules seem ignored:
php artisan cache:clear
rm -rf ~/.cache/pint
Rule Validation:
Validate your pint.json with:
php artisan pint --validate
Custom Rules: Extend Pint’s rules by modifying the underlying PHP-CS-Fixer config. Example:
{
"rules": {
"Pint/phpdoc_type_annotations_only": true,
"custom_rule": {
"path": "./CustomRule.php",
"parameters": {}
}
}
}
Preset Inheritance: Extend existing presets (e.g., Laravel + PER-CS):
{
"extends": ["laravel", "@PER-CS"],
"rules": {
"cast_spaces": false
}
}
Agent Format:
Use the agent format for IDE integration (auto-detected in Claude Code/OpenCode):
php artisan pint --format=agent
Exit Status:
Use --with-exit-status to fail builds when changes are needed:
php artisan pint --test --with-exit-status
php artisan pint app/Http/Controllers/
php artisan pint --dry-run
php artisan pint --summary-file=pint-summary.txt
--bail:
php artisan pint --bail
--dirty checks with --ignore-no-changes:
php artisan pint --dirty --ignore-no-changes
```markdown
### Laravel-Specific Tips
1. **Laravel Preset Quirks**:
- The Laravel preset enforces `snake_case` for PHPUnit test methods. Override in `pint.json` if needed:
```json
{
"rules": {
"phpunit_method_casing": false
}
}
```
2. **Artisan Command Alias**:
Add an alias to your `~/.bashrc` or `~/.zshrc`:
```bash
alias pint='php artisan pint'
Laravel Mix/Webpack: Integrate Pint with Laravel Mix for frontend + backend formatting:
// webpack.mix.js
mix.postCss('resources/css/app.css', 'public/css', [
// ...
])
.then(() => {
require('child_process').execSync('php artisan pint', { stdio: 'inherit' });
});
Laravel Forge/Envoyer: Add Pint to deployment scripts:
# envoyer/deploy.php
$tasks->add('composer
How can I help you explore Laravel packages today?