Skip to content

Laravel Pint

Nguồn gốc: Bản dịch từ Laravel Pint

Giới thiệu (Introduction)

Laravel Pint là code style fixer opinionated cho PHP, được xây dựng trên PHP-CS-Fixer. Pint tự động đảm bảo code style sạch sẽ, nhất quán.

Pint được cài sẵn trong mọi ứng dụng Laravel mới — bạn có thể bắt đầu dùng ngay!

Cài đặt (Installation)

bash
composer require laravel/pint --dev

Chạy Pint (Running Pint)

bash
./vendor/bin/pint

Chạy trên file/thư mục cụ thể:

bash
./vendor/bin/pint app/Models
./vendor/bin/pint app/Models/User.php

Xem thay đổi mà không áp dụng:

bash
./vendor/bin/pint --test

Chỉ hiện file có lỗi:

bash
./vendor/bin/pint --dirty

Cấu hình Pint (Configuring Pint)

Tạo file pint.json ở root project.

Presets

Preset định nghĩa tập quy tắc style. Mặc định Pint dùng preset laravel. Các preset: laravel, per, psr12, symfony, empty.

json
{
    "preset": "per"
}

Rules

Bật/tắt PHP-CS-Fixer rules cụ thể:

json
{
    "preset": "laravel",
    "rules": {
        "simplified_null_return": true,
        "braces": false,
        "new_with_braces": {
            "anonymous_class": false,
            "named_class": false
        }
    }
}

Excluding Files / Folders

json
{
    "exclude": [
        "my-specific/folder"
    ]
}

Continuous Integration

GitHub Actions

yaml
name: Fix Code Style

on: [push]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: shivammathur/setup-php@v2
        with:
          php-version: '8.3'
      - run: composer install
      - run: ./vendor/bin/pint --test