Giao diện
Laravel Pint
Giới thiệu (Introduction)
Laravel Pint là code style fixer không cần cấu hình (opinionated) cho PHP. Dựa trên PHP-CS-Fixer, Pint đảm bảo code style của bạn luôn sạch sẽ và nhất quán.
Pint được cài đặt sẵn trong mọi Laravel project mới.
Cài đặt
bash
composer require laravel/pint --devChạy Pint
bash
# Chạy trên toàn bộ project
./vendor/bin/pint
# Chạy trên files/thư mục cụ thể
./vendor/bin/pint app/Models
./vendor/bin/pint app/Models/User.php
# Chỉ hiển thị thay đổi (không sửa)
./vendor/bin/pint --test
# Verbose output
./vendor/bin/pint -v
# Dirty files only (uncommitted changes)
./vendor/bin/pint --dirtyCấu hình (Configuration)
Tạo file pint.json tại root project:
json
{
"preset": "laravel"
}Presets (Bộ quy tắc)
| Preset | Mô tả |
|---|---|
laravel | (Mặc định) Laravel code style |
per | PER Coding Style |
psr12 | PSR-12 standard |
symfony | Symfony code style |
Tùy chỉnh Rules
json
{
"preset": "laravel",
"rules": {
"simplified_null_return": true,
"braces": {
"position_after_functions_and_oop_constructs": "next"
},
"new_with_braces": {
"anonymous_class": false,
"named_class": false
}
}
}Exclude (Loại trừ)
json
{
"exclude": [
"my-specific/folder"
]
}Chỉ định files
json
{
"finder": {
"notPath": ["storage", "vendor"],
"notName": ["*.blade.php"]
}
}CI/CD Integration
Tích hợp vào CI pipeline:
bash
./vendor/bin/pint --testExit code 0 = code style OK, 1 = cần sửa. Phù hợp cho GitHub Actions, GitLab CI, v.v.