翻譯進度
46.67% 已翻譯
更新時間:
2024年6月30日 上午8:27:00 [世界標準時間]
翻譯人員:
幫我們翻譯此頁

Laravel Pint

簡介

Laravel Pint 是一款專為極簡主義者涉及的主導性 (Opinionated) PHP Code Style Fixer(程式碼風格修正程式)。Pint 以 PHP-CS-Fixer 為基礎,並讓其保持簡單,以確保你的 Code Style 保持乾淨與統一。

在所有新建立的 Laravel 專案中,會自動安裝 Pint,因此你可以馬上開始使用。預設情況下,Pint 並不需要任何設定,會自動使用 Laravel 的主導性 Coding Style 來修正程式碼中的 Coding Style 問題。

安裝

Pint 已包含在最近釋出的 Laravel 框架中,因此通常不需要進行安裝。不過,在舊的專案中,可以使用 Composer 來安裝 Laravel Pint:

1composer require laravel/pint --dev
1composer require laravel/pint --dev

執行 Pint

只要呼叫專案 vendor/bin 目錄中的 pint 二進位檔案,就可以讓 Pint 修正 Coding Style 問題:

1./vendor/bin/pint
1./vendor/bin/pint

也可以針對特定檔案或目錄來執行 Pint:

1./vendor/bin/pint app/Models
2 
3./vendor/bin/pint app/Models/User.php
1./vendor/bin/pint app/Models
2 
3./vendor/bin/pint app/Models/User.php

Pint 會列出其更新的檔案列表。只要在呼叫 Pint 時提供 -v 選項,就可以檢視更多關於 Pint 所做出更改的詳情:

1./vendor/bin/pint -v
1./vendor/bin/pint -v

If you would like Pint to simply inspect your code for style errors without actually changing the files, you may use the --test option. Pint will return a non-zero exit code if any code style errors are found:

1./vendor/bin/pint --test
1./vendor/bin/pint --test

若要根據 Git 來讓 Pint 只修改包含未 Commit 更改的檔案,可使用 --dirty 選項:

1./vendor/bin/pint --dirty
1./vendor/bin/pint --dirty

If you would like Pint to fix any files with code style errors but also exit with a non-zero exit code if any errors were fixed, you may use the --repair option:

1./vendor/bin/pint --repair
1./vendor/bin/pint --repair

設定 Pint

剛才也提到過,Pint 不需要任何設定檔。不過,若有需要更改預設、規則、或是要檢查的資料夾,可在專案根目錄建立一個 pint.json 檔:

1{
2 "preset": "laravel"
3}
1{
2 "preset": "laravel"
3}

此外,若要使用特定資料夾中的 pint.json 檔,可在呼叫 Pint 時提供 --config 選項:

1./vendor/bin/pint --config vendor/my-company/coding-style/pint.json
1./vendor/bin/pint --config vendor/my-company/coding-style/pint.json

預設

Presets define a set of rules that can be used to fix code style issues in your code. By default, Pint uses the laravel preset, which fixes issues by following the opinionated coding style of Laravel. However, you may specify a different preset by providing the --preset option to Pint:

1./vendor/bin/pint --preset psr12
1./vendor/bin/pint --preset psr12

若有需要,也可以在專案的 pint.json 檔案中設定預設:

1{
2 "preset": "psr12"
3}
1{
2 "preset": "psr12"
3}

Pint's currently supported presets are: laravel, per, psr12, symfony, and empty.

規則

「規則 (Rule)」是 Pint 要用來修正程式碼 Coding Style 問題的樣式準則。剛才提到過,「預設」預先定義了一組規則,這些規則應該適用於大多數的 PHP 專案,因此你通常不需要擔心個別的規則。

However, if you wish, you may enable or disable specific rules in your pint.json file or use the empty preset and define the rules from scratch:

1{
2 "preset": "laravel",
3 "rules": {
4 "simplified_null_return": true,
5 "array_indentation": false,
6 "new_with_parentheses": {
7 "anonymous_class": true,
8 "named_class": true
9 }
10 }
11}
1{
2 "preset": "laravel",
3 "rules": {
4 "simplified_null_return": true,
5 "array_indentation": false,
6 "new_with_parentheses": {
7 "anonymous_class": true,
8 "named_class": true
9 }
10 }
11}

Pint 以 PHP-CS-Fixer 為基礎製作,因此,你可以使用 PHP-CS-Fixer 的規則來修正專案中的 Coding Style 問題: PHP-CS-Fixer Configurator

排除檔案或資料夾

預設情況下,Pint 會檢查專案中除了 vendor 目錄以外的所有 .php 檔案。若有需要排除其他目錄,可使用 exclude 設定選項:

1{
2 "exclude": [
3 "my-specific/folder"
4 ]
5}
1{
2 "exclude": [
3 "my-specific/folder"
4 ]
5}

若有需要排除所有符合特定檔名規則的檔案,可使用 notName 選項:

1{
2 "notName": [
3 "*-my-file.php"
4 ]
5}
1{
2 "notName": [
3 "*-my-file.php"
4 ]
5}

若有需要排除特定路徑的檔案,可使用 notPath 設定選項:

1{
2 "notPath": [
3 "path/to/excluded-file.php"
4 ]
5}
1{
2 "notPath": [
3 "path/to/excluded-file.php"
4 ]
5}

Continuous Integration

GitHub Actions

To automate linting your project with Laravel Pint, you can configure GitHub Actions to run Pint whenever new code is pushed to GitHub. First, be sure to grant "Read and write permissions" to workflows within GitHub at Settings > Actions > General > Workflow permissions. Then, create a .github/workflows/lint.yml file with the following content:

1name: Fix Code Style
2 
3on: [push]
4 
5jobs:
6 lint:
7 runs-on: ubuntu-latest
8 strategy:
9 fail-fast: true
10 matrix:
11 php: [8.3]
12 
13 steps:
14 - name: Checkout code
15 uses: actions/checkout@v4
16 
17 - name: Setup PHP
18 uses: shivammathur/setup-php@v2
19 with:
20 php-version: ${{ matrix.php }}
21 extensions: json, dom, curl, libxml, mbstring
22 coverage: none
23 
24 - name: Install Pint
25 run: composer global require laravel/pint
26 
27 - name: Run Pint
28 run: pint
29 
30 - name: Commit linted files
31 uses: stefanzweifel/git-auto-commit-action@v5
32 with:
33 commit_message: "Fixes coding style"
1name: Fix Code Style
2 
3on: [push]
4 
5jobs:
6 lint:
7 runs-on: ubuntu-latest
8 strategy:
9 fail-fast: true
10 matrix:
11 php: [8.3]
12 
13 steps:
14 - name: Checkout code
15 uses: actions/checkout@v4
16 
17 - name: Setup PHP
18 uses: shivammathur/setup-php@v2
19 with:
20 php-version: ${{ matrix.php }}
21 extensions: json, dom, curl, libxml, mbstring
22 coverage: none
23 
24 - name: Install Pint
25 run: composer global require laravel/pint
26 
27 - name: Run Pint
28 run: pint
29 
30 - name: Commit linted files
31 uses: stefanzweifel/git-auto-commit-action@v5
32 with:
33 commit_message: "Fixes coding style"
翻譯進度
46.67% 已翻譯
更新時間:
2024年6月30日 上午8:27:00 [世界標準時間]
翻譯人員:
幫我們翻譯此頁

留言

尚無留言

“Laravel” is a Trademark of Taylor Otwell.
The source documentation is released under MIT license. See laravel/docs on GitHub for details.
The translated documentations are released under MIT license. See cornch/laravel-docs-l10n on GitHub for details.