私らの開発環境が最近やっとphp 8.0となりましたので、Laravel Pint(ピント)なるものを使ってみました。正式にLaravelの開発チームから登場したコードスタイル整形のツールですが、なんのことはない、Larajapanでも以前から数回紹介したphp-cs-fixerを土台にしたLaravel版です。しかし、より簡単になりました。

以前に紹介したのは、「コードスタイルを統一するツール」です。ここでも、同様な醜い(Ugly Class)のコードを用意して、Laravel Pintを紹介しましょう。

インストール

Laravelのプロジェクトのルートのディレクトリにおいて、以下を実行します。

$ composer require laravel/pint --dev

開発環境でしか必要ないので、最後の引数–devは忘れないように。

実行

実行もいたって簡単です。php-cs-fixerと違って、コード整形のためのルールの設定はまったく要りません。完全にLaravel用にデフォルト設定になっています。

$ ./vendor/bin/pint
 ......................................................

 ───────────────────────────────────────────────────────────────── Laravel
 PASS   ....................................................... 54 files

もちろん、何も変えてないのでエラーはゼロでPASSです。

今度は、前回作成したUglyクラスのファイルを追加して、

namespace App\Models;

use Log;
use Illuminate\Database\Eloquent\Model;
use DB;

class Ugly extends Model
{
	public function test ($x, $y)
	{

if ($x == 1)
{
	//
} else if ($y == 2) { /* */ }

	}
}

再度実行すると、

$ ./vendor/bin/pint
  .....................................................✓.

  ───────────────────────────────────────────────────────────────── Laravel
    FIXED   ................................. 55 files, 1 style issue fixed
  ✓ app/Models/Ugly.php indentation_type, elseif, braces, function_declara…

しっかり、修正(FIXED)されて、

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Ugly extends Model
{
    public function test($x, $y)
    {
        if ($x == 1) {
    //
        } elseif ($y == 2) { /* */
        }
    }
}

綺麗に整形されました。

カスタム設定

Laravel Pintは、デフォルトで完全にLaravelのコードスタイルとしてくれます。しかし、ちょっとだけ私がカスタマイズしたい部分があります。

コードにおけるオブジェクトの表示です。私は以下のように、=>で項目を揃えたいのです。

	protected $rules = [
		'cost'       => 'required|integer|min:0',
		'cost_ship'  => 'required|integer|min:0',
		'cost_other' => 'required|integer|min:0',
		'commission' => 'required|numeric|min:0',
	];

しかし、これをpintで実行すると、

    protected $rules = [
        'cost' => 'required|integer|min:0',
        'cost_ship' => 'required|integer|min:0',
        'cost_other' => 'required|integer|min:0',
        'commission' => 'required|numeric|min:0',
    ];

となってしまいます。もちろんそれがLaravelのデフォルトのスタイルなのですが。

これを強制しないようにするには、設定ファイルが必要となります。以下のファイル(pint.json)をプロジェクトのルートディレクトリに置きます。

{
    "preset": "laravel",
    "rules": {
        "binary_operator_spaces":{"default":"single_space","operators":{"=>":null}}
    },
    "exclude": [
        "bootstrap",
        "src"
    ]
}

上のrulesのルールは、以下のphp-cs-fixerで使用するものと同じです。
https://mlocati.github.io/php-cs-fixer-configurator/#version:3.11|fixer:binary_operator_spaces

しかし、そこでの設定はphpの配列となります。以下のように。

    'binary_operator_spaces' => [
        'default' => 'single_space',
        'operators' => ['=>' => null],
    ],

pintツールは、phpなのになぜか設定ファイルはjsonフォーマットです。ということで、tinkerで以下のように変換して、pint.jsonに入れる必要あります。

>>> json_encode(['binary_operator_spaces' => [
        'default' => 'single_space',
        'operators' => ['=>' => null],
    ]]);
=> "{"binary_operator_spaces":{"default":"single_space","operators":{"=>":null}}}"

By khino