Visual Studio CodeのためのPHP_CodeSnifferであるphpcsを使ってコーディング規約をチェック

Visual Studio CodeでPHPコードを書く上で、コーディング規約をチェックするのにphpcsという拡張機能を追加できます。ここではphpcsを追加する流れを自分の記録という意味合いも含め、記事として残しておこうと思います。

 

phpcsインストールの流れ

phpcsインストールにあたってVisual Studio Codeのphpcs拡張機能の詳細説明の箇所を参考にインストールを進めました。具体的には以下の箇所です。

Installation

Visual Studio Code must be installed in order to use this plugin. If Visual Studio Code is not installed, please follow the instructions here.

Linter Installation

Before using this plugin, you must ensure that phpcs is installed on your system. The preferred method is using composer for both system-wide and project-wide installations.

Once phpcs is installed, you can proceed to install the vscode-phpcs plugin if it is not yet installed.

NOTE: This plugin can detect whether your project has been set up to use phpcs via composer and use the project specific phpcs over the system-wide installation of phpcs automatically. This feature requires that both composer.json and composer.lock file exist in your workspace root or the phpcs.composerJsonPath in order to check for the composer dependency. If you wish to bypass this feature you can set the phpcs.executablePath configuration setting.

NOTE: You can also install phpcs on your system using pear or even manually but is beyond the scope of this plugin.

System-wide Installation

The phpcs linter can be installed globally using the Composer Dependency Manager for PHP.

Install composer.
Require phpcs package by typing the following in a terminal:

composer global require squizlabs/php_codesniffer
Project-wide Installation

The phpcs linter can be installed in your project using the Composer Dependency Manager for PHP.

Install composer.
Require phpcs package by typing the following at the root of your project in a terminal:

composer require –dev squizlabs/php_codesniffer
Plugin Installation

Open Visual Studio Code.
Press Ctrl+P on Windows or Cmd+P on Mac to open the Quick Open dialog.
Type ext install phpcs to find the extension.
Press Enter or click the cloud icon to install it.
Restart Visual Studio Code when prompted.

https://github.com/ikappas/vscode-phpcs/blob/develop/phpcs/README.md#installation

基本的に該当箇所のやり方を見れば、導入を終えることができますが、一応私の方でも説明してみます。

①Composerの導入

この文章を読むに、以下の箇所に

Before using this plugin, you must ensure that phpcs is installed on your system. The preferred method is using composer for both system-wide and project-wide installations.

https://github.com/ikappas/vscode-phpcs/blob/develop/phpcs/README.md#installation

とりあえずComposerを使ってphpcsをシステムにインストールするのが良いみたいなことが書いてあるので、Composer経由でインストールするようにします。つまりはComposerがインストール済みでなければ、先に進めないので、まずはComposerのインストールを終えられるようにします。Composerのインストールの仕方は以前書いたこちらをどうぞ。

【PHP】超初心者のためのComposerインストール方法

 

②Composerを使ってphpcsパッケージ(php_codesniffer)をインストール

Composerが使える環境を用意出来たら、コマンドからphpcsパッケージ(php_codesniffer)をインストールします。使うコマンドはこちらです。

composer global require squizlabs/php_codesniffer

このコマンドをターミナルに打ち込むと現在のディレクトリから自動で切り替わりphp_codesnifferがインストールされます。

なお、ややこしいですがPHP_CodeSnifferというPHPのコーディング規約をチェックする解析ツールがもとからあり、その解析ツールをVisual Studio Code上で使えるようにする拡張機能(Extensions)がphpcsという名前ということです。この記事内でも文字を省略しphpcsをインストールするということを何度も言っていますが、VS Codeの拡張機能のphpcsなのか、Composer経由でインストールするphp_codesnifferなのかわかりづらくなっているかもしれません。

Visual Studio CodeからPHP_CodeSnifferなる解析ツールを知った私のような初心者であるならば、少々こんがらがってしまいます。ちなみに初めはよく意味がわかりませんでした。

aipercent_visual_studio_code_extension_phpcs_01

 

 

 

 

 

 

 

 

Visual Studio Code上でコーディング規約の設定

拡張機能を追加後、Visual Studio Code上の設定でチェックするコーディング規約を設定することができます。

Visual Studio Code上でCtrl + ,(カンマ)を入力することでユーザ設定画面を開くことができます。

ここでの設定を変えることで、プロジェクトによって記述しなければならないコード内容が変わり、必要なコーディング規約が変更する場合にも、その都度規約を変更することができます。ここでは基本的な変更設定を紹介します。

phpcsのコーディングチェックを一時停止する

"phpcs.standard": null

 

phpcsのコーディング規約を変更したい場合

"phpcs.standard": "PSR2"

コーディングスタンダードの例として他にもMySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zendなどがあります。

 

WordPressなどのコーディング規約を適用したい場合

"phpcs.standard": "WordPress"

世界中のWebサイトのCMSとして使われているWordPressをはじめDrupalなどの規約も適用することができます。

 

カスタムコーディングスタンダード絶対パスの設定やカスタムルール絶対パスの設定

コーディングスタンダードの設定はパス表記でも設定することができます。たとえば絶対パス表記の場合以下のようです。

"phpcs.standard": "/path/to/coding/standard"

また、独自コーディング規約のチェックにphpcsを使いたい場合には、以下のように設定します。

"phpcs.standard": "/path/to/project/phpcs.xml"

 

カスタムコーディングスタンダードやカスタムルールを相対パスで設定

カスタムルールのパス表記設定は相対パス表記でも設定できます。

"phpcs.standard": "./vendor/path/to/coding/standard"

また、独自コーディング規約のチェックにphpcsを使いたい場合の相対パス表記は以下のように設定します。

"phpcs.standard": "./phpcs.xml"

 

コーディングチェックを無視したいファイルやフォルダがある場合の設定

"phpcs.ignorePatterns": [
        "*/ignored-file.php",
        "*/ignored-dir/*"
    ]

 

コメント

タイトルとURLをコピーしました