[PHP]phpcsでPSR-2構文チェックを1秒で行う方法(右クリックメニュー使用)

カテゴリ: phpcs

PHPでは、phpcsを使用するとコーディング規約のチェックを行えます。
今回は、単一のphpファイルを、右クリックから簡単にチェックする方法を紹介します。

phpcsを使った構文チェックといえば、以下のような使われ方が多いかと思います。

  • IDEやエディタプラグインとして実行させ、その場でチェック
  • コミット時のフックスクリプトに仕掛けておいてコミット時チェック
  • jenkinsなどのCIに組み込んで定期的にチェック

ですが、上記のような準備を行わず、気楽にphpファイル1つだけを構文チェックしたい場合があります。右クリックメニューの送るからチェックできるようにします。

phpcsのインストール

composerコマンドでインストールを行います。
今回は、c:\phpcsにインストール行った場合の例を示しますが、どこにインストールして大丈夫です。

> mkdir c:\phpcs

> cd /d c:\phpcs

> composer require squizlabs/php_codesniffer

Using version ^3.0 for squizlabs/php_codesniffer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing squizlabs/php_codesniffer (3.0.0): Downloading (100%)
Writing lock file
Generating autoload files

phpcsの動作確認

まず、phpcsをオプションなしで実行して、ヘルプが表示されることを確認します。

> c:\phpcs\vendor\bin\phpcs.bat

ERROR: You must supply at least one file or directory to process.

Usage: phpcs [-nwlsaepqvi] [-d key[=value]] [--cache[=<cacheFile>]] [--no-cache] [--colors] [--no-colors]
  [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>] ...
  [--report-width=<reportWidth>] [--basepath=<basepath>] [--tab-width=<tabWidth>]
  [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]
  [--runtime-set key value] [--config-set key value] [--config-delete key] [--config-show]
  [--standard=<standard>] [--sniffs=<sniffs>] [--exclude=<sniffs>] 
  [--encoding=<encoding>] [--parallel=<processes>] [--generator=<generator>]
  [--extensions=<extensions>] [--ignore=<patterns>] [--ignore-annotations] <file> - ...

 -     Check STDIN instead of local files and directories
 -n    Do not print warnings (shortcut for --warning-severity=0)
 -w    Print both warnings and errors (this is the default)
 -l    Local directory only, no recursion
 -s    Show sniff codes in all reports
 -a    Run interactively
 -e    Explain a standard by showing the sniffs it includes
...

次に適当なファイルを作成して、下記のコマンドでチェックできることを確認します。
(下記の例では、--standard=PSR2オプションで、PSR-2のコーディング規約に合致しているかチェックします)

<?php
// c:work/hello.php

function say_hello() {
    print("hello\n");
}

say_hello();
  • チェック対象のプログラム
> c:\phpcs\vendor\bin\phpcs.bat --report-width=90 --standard=PSR2 c:\work\hello.php

FILE: C:\work\hello.php
------------------------------------------------------------------------------------------
FOUND 2 ERRORS AND 1 WARNING AFFECTING 3 LINES
------------------------------------------------------------------------------------------
 1 | WARNING | [ ] A file should declare new symbols (classes, functions, constants,
   |         |     etc.) and cause no other side effects, or it should execute logic with
   |         |     side effects, but should not do both. The first symbol is defined on
   |         |     line 4 and the first side effect is on line 8.
 4 | ERROR   | [x] Opening brace should be on a new line
 8 | ERROR   | [x] Expected 1 newline at end of file; 0 found
------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------

Time: 176ms; Memory: 6Mb
  • コマンドラインからのチェック結果

バッチファイルの作成

次に上記の処理を右クリックメニューに組み込みます。まずは下記のバッチを作成します。

rem PhpcsChecker.bat

call c:\phpcs\vendor\bin\phpcs.bat --report-width=140 --standard=PSR2 %1
pause

作ったバッチファイルを、右クリックの送るメニューに追加します。
送るメニューの一覧を管理しているフォルダは、以下のようにExplorerにsend:sendtoと入力すると、移動できます。

チェックしたいファイルを選択し、右クリック->送る->作成したバッチを選択するとチェックできます。

開いたウィンドウはEnterキーを入力すると閉じます。

何度も繰り返してチェックしたい場合

コーディング規約のチェックは、修正->確認->修正...の繰り返しを複数回繰り返すことも多いです。この場合、バッチを下記のように無限ループにしておくと、Enterキーを押すたびに再チェックを行えます。
終了したい場合は、右上の[x]で閉じてください。

LOOP:

call c:\phpcs\vendor\bin\phpcs.bat --report-width=140 --standard=PSR2 %1
pause

cls
goto LOOP:

Amazonでおトクに買い物する方法
AmazonチャージでポイントGET


Amazonは買いもの前にAmazonギフト券をチャージしてポイントをゲットしないと損!

こちらもおススメ

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です