Skip to content

Laravel Pint

Giới thiệu (Introduction)

Laravel Pintcode 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 --dev

Chạ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 --dirty

Cấu hình (Configuration)

Tạo file pint.json tại root project:

json
{
    "preset": "laravel"
}

Presets (Bộ quy tắc)

PresetMô tả
laravel(Mặc định) Laravel code style
perPER Coding Style
psr12PSR-12 standard
symfonySymfony 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 --test

Exit code 0 = code style OK, 1 = cần sửa. Phù hợp cho GitHub Actions, GitLab CI, v.v.