Hướng dẫn laravel pint

  • December 6, 2022
  • 1057

Một dự án sẽ có rất nhiều người code với nhau, mỗi người có một code style khác nhau, làm sao để thống nhất một convetion code chung để mọi người cùng code theo? Hôm nay chúng ta cùng tìm hiểu laravel pint  để giải quyết vấn đề này.

Laravel pint

Laravel pint được tích hợp sẵn với laravel phiên bản 9, công cụ giúp check code, fomat code chuẩn convention. Ở các phiên bản thấp hơn chúng ta cần cài đặt với composer. Ngoài ra chúng ta cũng có thể check convention code với phpstanphp CodeSniffer.

Install laravel pint

Nếu bạn đang dùng laravel 9 thì sẽ không cần cài đặt laravel pint, nó đã được tích hợp sẵn rồi. Ở các phiên bản thấp hơn có thể cài đặt thông qua composer


composer require laravel/pint --dev

Config laravel pint

Tạo file pint.json để thiết lập các rule convention code trong dự án của bạn. Chúng ta tạo một file như sau


{
    "preset": "laravel",
    "rules": {
        "simplified_null_return": true,
        "align_multiline_comment": true,
        "braces": true,
        "binary_operator_spaces": true,
        "blank_line_after_namespace": true,
        "function_typehint_space": true,
        "blank_line_after_opening_tag": true,
        "compact_nullable_typehint": true,
        "unary_operator_spaces": false,
        "space_after_semicolon": true,
        "new_with_braces": {
            "anonymous_class": false,
            "named_class": false
        },
        "phpdoc_align": {
            "align": "left"
        },
        "concat_space": {
            "spacing": "one"
        },
        "clean_namespace": true,
        "not_operator_with_successor_space": false
    }
}    

Chúng ta có thể tự custom các rule cho dự án của mình, các rule chúng ta thảm khảo PHP-CS-Fixer Configurator.

Excluding Files / Folders

Chúng ta cũng có thể config bỏ qua file, folder không muốn check convention bằng cách thêm config sau vào file pint.json


// Bỏ qua check folder
{
    "exclude": [
        "my-specific/folder"
    ]
}

// Bỏ qua check file
{
    "notPath": [
        "path/to/excluded-file.php"
    ]
}

How to use laravel pint

Để check code có đúng convetion mà chúng ta thiết lập không chỉ cần chạy command


./vendor/bin/pint --test

Để check code và fixed luôn convention code chúng ta chạy command


./vendor/bin/pint -v

Thiết lập short script trong file composer.json


"scripts": {
    "pint-fixed": "./vendor/bin/pint -v",
     "pint": "./vendor/bin/pint --test"
}

Giờ muốn test convention code chúng ta sẽ run command


composer pint

Muốn fix convention code run command


composer pint-fixed

Đây là command khi chúng ta run thành công


root@9cdd7ac94b31:/var/www# composer pint
> ./vendor/bin/pint --test

  .............................................................................

  ───────────────────────────────────────────────────────────────────────────────────────────────────────── Laravel  
    PASS   ............................................................................................... 77 files  

root@9cdd7ac94b31:/var/www# composer pint-fixed
> ./vendor/bin/pint -v

  .............................................................................

  ───────────────────────────────────────────────────────────────────────────────────────────────────────── Laravel  
    PASS   ............................................................................................... 77 files  

root@9cdd7ac94b31:/var/www# 

Tổng kết

Như vậy là chúng ta đã định nghĩa được convention code cho dự án. Trước mỗi lần merge code chúng ta sẽ check và fix convention code, giúp code dự án clean hơn. Các bạn sẽ thiết lập rule như thế nào? Hãy comment bên dưới. Thanks for reading.