LoginSignup
20
13

Laravel Pint コードスタイル自動整形ツールのルール設定

Last updated at Posted at 2022-09-06

Laravel Pint

Laravel Pint(ピント)はコードスタイルの自動整形ツールPHP-CS-Fixerのラッパーライブラリです。

インストール

Laravel v9.3.0 からLaravelに標準搭載されています。

古いLaravelに導入する場合は下記のコマンドからインストールします。

$ composer require laravel/pint --dev

Pintの使い方

$ ./vendor/bin/pint

ルールに基づいてコードスタイルを適用します。

$ ./vendor/bin/pint -v

-v 詳細表示してくれます。

$ ./vendor/bin/pint --test
$ ./vendor/bin/pint --test -v

--test を指定するとルールは適用されずチェックのみ実施してくれます。

Pintの設定

デフォルトはlaravel プリセットが使用されるのですが、私の好みで少しルールを追加してます。
よければ参考にどうぞ。

pint.json
{
    "preset": "laravel",
    "cache-file": ".pint.cache",
    "exclude": [
        "database/migrations"
    ],
    "rules": {
        "concat_space": false,
        "date_time_immutable": true,
        "declare_parentheses": true,
        "declare_strict_types": true,
        "final_class": true,
        "global_namespace_import": {
            "import_classes": true,
            "import_constants": true,
            "import_functions": true
        },
        "mb_str_functions": true,
        "new_with_braces": true,
        "no_superfluous_phpdoc_tags": true,
        "not_operator_with_successor_space": true,
        "phpdoc_align": {
            "align": "left"
        },
        "phpdoc_separation": false,
        "php_unit_test_case_static_method_calls": {
            "call_type": "this"
        },
        "simplified_if_return": true,
        "simplified_null_return": true
    }
}
  • .pint.cache キャッシュファイルが作られるので .gitignore に追加してください。
  • final_class を設定してるので、app/Http/Controllers/Controller.phpabstract class に変更してください。

laravel プリセットの内容はPintのGitHubで確認できます。
https://github.com/laravel/pint/blob/main/resources/presets/laravel.php

laravel プリセットになく、好みで追加したいルールを設定してます。
個々のルールについてはこちらのドキュメントから内容を確認できます。

補足: 特定のディレクトリのみ適用するオプションはない

ディレクトリやファイル名、特定のファイルパスを除外したい場合は下記のように設定できます。

pint.json
{
    "exclude": [
        "my-specific/folder"
    ],
    "notName": [
        "*-my-file.php"
    ],
    "notPath": [
        "path/to/excluded-file.php"
    ]
}

ただし、現時点ではincludeのオプションは存在しないようです。

特定のディレクトリだけ実行させたい場合はコマンドの引数としてディレクトリを指定するしか今のところ方法なさそうです。

$ ./vendor/bin/pint app/Console

関連

20
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
13