概要
ここでは、PHPStormでDrupalのコーディング規約を自動チェックできるようにする方法を紹介します。
関連記事
環境情報
- Mac Big Sur:11.6.2
- Apple M1
- composer:2.1.14
- HomeBrewでインストール
- 以下のパスが通っていること
PhpStorm 2021.3
``` export PATH=$HOME/.composer/vendor/bin:$PATH ```
手順
PHP Code Snifferと Drupal code standard
まずはリンターの
PHP Code Sniffer
(以下 phpcsと記述)と、Drupalのコーディング規約をインストールします。composer global require "squizlabs/php_codesniffer=*" composer global require drupal/coder
phpcsがインストールできたことを確認しましょう。
phpcs --version PHP_CodeSniffer version 3.6.2 (stable) by Squiz (http://www.squiz.net)
コーディング規約がインストールできていることを確認しましょう。
composer global show -- drupal/coder
パッケージ情報が表示されます。
phpcsの設定
phpcsにDrupalのコーディング規約を読み込むように設定します。
設定前
phpcs -i The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1 and PSR12
設定コマンドを実行
phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
設定後、DrupalとDrupalPracticeが増えていることを確認します。
phpcs -i The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1, PSR12, Drupal and DrupalPractice
PHPStorm側の操作
PHPStormの設定画面を開きます(
⌘ + ,
)。品質ツール > PHP_CodeSniffer > 「...」をクリックします。
「PHP_CodeSnifferのパス:」に
phpcs
までのパスを入力または選択します。/ABSOLUTE/PATH/TO/.composer/vendor/bin/phpcs
ついでに、「phpcbfのパス:」に
phpcbf
までのパスを入力または選択します。/ABSOLUTE/PATH/TO/.composer/vendor/bin/phpcbf
「検証」をクリックして、下の方に「OK, PHP_CodeSniffer version ・・・省略・・・」と表示されればOKです。
「PHP_CodeSniffer インスペクション」をクリックします。
「PHP_CodeSniffer 検証」のチェックをONにします。
右の側の「次の拡張子を持つファイルをチェック」に以下の内容を入力します。
php,module,inc,install,test,profile,theme,css,info,txt,md,yml
コーディング規約読み込み用の設定ファイルを作成します。ファイル名も任意のものでOKです。
- 筆者は
~/.compser
ディレクトリの下に作成してあります。
ファイル名:phpcs-custom-standards.xml
<?xml version="1.0"?> <ruleset name="Custom Drupal code standards"> <!-- Combine these rulesets. --> <rule ref="Drupal"/> <rule ref="DrupalPractice"/> <!-- Ignore any files in these paths. --> <exclude-pattern>*/.git/*</exclude-pattern> <exclude-pattern>*/config/*</exclude-pattern> <exclude-pattern>*/css/*</exclude-pattern> <exclude-pattern>*/js/*</exclude-pattern> <exclude-pattern>*/icons/*</exclude-pattern> <exclude-pattern>*/vendor/*</exclude-pattern> <exclude-pattern>*/node_modules/*</exclude-pattern> <exclude-pattern>*rules_export.txt</exclude-pattern> <exclude-pattern>*/features/*</exclude-pattern> <!-- For colored cli output --> <arg name="colors"/> <!-- To show rule names. Equivalent to "phpcs -s" --> <arg value="sp"/> <!-- Depending on your project, you may need to ignore specific rules until they can be fixed one at a time. Here is an example of how to ignore a specific rule. --> <!-- Class name must be prefixed with the module name. --> <!-- <rule ref="DrupalPractice.General.ClassName.ClassPrefix"> <severity>0</severity> </rule> --> </ruleset>
- 筆者は
設定画面に戻って、「コーディング標準:」に
Custom
を選択します。「コーディング標準:」の右にある「...」をクリックして、「ルールセットのパス:」に上で作成したファイルまでのパスを入力します。パスは絶対パスです。
設定画面が消えるまで「OK」をクリックします。
完了
サンプルソースで確認
- サンプルソース:helloworld 8.x-1.3 | Drupal.orgを適当に持ってきます。
helloworld/src/Controller/DefaultController.php
を開きます。あえて規約を破るようにいじって自動チェックが走ることを確認します。
参考
- 閲覧数 248
コメントを追加