Help us understand the problem. What is going on with this article?

Symplify\CodingStandardを素のPHP-CS-Fixerで動かす方法

More than 1 year has passed since last update.

Symplify\CodingStandardは、クリーンアーキテクチャに注力するPHPプロジェクトのための、30以上のコード標準検査ルールを束ねたリポジトリだ。

検査ルールは、PHP CodeSnifferのSniffやPHP-CS-FixerのFixerを実装する形で作られているものの、姉妹ツールのEasyCodingStandardで実行することが推奨されている。

Sniff系の検査ルールはPHP CodeSnifferに問題なく組み込むことができる。しかし、Fixer系の検査ルールはPHP-CS-Fixerに組み込むことができない。何故かと言うと、PHP-CS-Fixerが期待する形の命名規則でCodingStandardが作られていないためだ。

もうすでにPHP-CS-Fixerを使っていて、EasyCodingStandardを入れるのは面倒な場合がある。そんなときなsymplify-cs-fixerをインストールするといい。これはSymplify\CodingStandardのFixer系ルールをPHP-CS-Fixerで動くようにしてくれるライブラリだ。

なお、symplify-cs-fixerをインストールすれば、symplify/easy-coding-standardもsymplify/coding-standardも入れる必要はない。

symplify-cs-fixerのインストール

composerでいれる:

composer require --dev suin/symplify-cs-fixer

.php_cs.distを設定する

ちょっと書き方がどうしても特殊になってしまうが、$symplifyFixers = new SymplifyFixers()して、$symplifyFixers()にSymplify/CodingStandardのルール名を渡すと、PHP-CS-Fixerが認識してくれるようになる。

.php_cs.dist
<?php

declare(strict_types=1);

use SymplifyCsFixer\SymplifyFixers;

$symplifyFixers = new SymplifyFixers();

return PhpCsFixer\Config::create()
    ->registerCustomFixers(
        [
            $symplifyFixers('standalone_line_in_multiline_array'),
            $symplifyFixers('block_property_comment'),
            $symplifyFixers('remove_empty_doc_block'),
            $symplifyFixers('remove_superfluous_doc_block_whitespace'),
            $symplifyFixers('remove_useless_doc_block'),
            $symplifyFixers('require_followed_by_absolute_path'),
            $symplifyFixers('line_length'),
            $symplifyFixers('property_name_matching_type'),
            $symplifyFixers('method_order_by_type'),
            $symplifyFixers('class_string_to_class_constant'),
            $symplifyFixers('array_property_default_value'),
            $symplifyFixers('final_interface'),
            $symplifyFixers('blank_line_after_strict_types'),
        ]
    )
    ->setRules(
        [
            ... 他のルール ...
            'Symplify/standalone_line_in_multiline_array' => true,
            'Symplify/block_property_comment' => true,
            'Symplify/remove_empty_doc_block' => true,
            'Symplify/remove_superfluous_doc_block_whitespace' => true,
            'Symplify/remove_useless_doc_block' => true,
            'Symplify/require_followed_by_absolute_path' => true,
            'Symplify/line_length' => [
                'line_length' => 80,
                'break_long_lines' => true,
                'inline_short_lines' => true,
            ],
            'Symplify/property_name_matching_type' => true,
            'Symplify/method_order_by_type' => true,
            'Symplify/class_string_to_class_constant' => true,
            'Symplify/array_property_default_value' => true,
            'Symplify/final_interface' => true,
            'Symplify/blank_line_after_strict_types' => true,
        ]
    )
    ->setFinder(
        ...
    );

あとは、php-cs-fixerの普通の使い方どおり:

vendor/bin/php-cs-fixer fix --dry-run --diff --diff-format=udiff --verbose
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away