最初に
既存のソースコードをPSR-2に準拠した形に置き換えるのはコストがかかりますし、なかなか実施しにくいと思います。
また、PSR-2をエンジニア全員に覚えさせて守らせるのも苦労するし、レビューで突っ込むのも面倒ってところの解決策になればな幸いです。
インストール
PHP_CodeSnifferを使って楽に一括で変換を行います。
https://github.com/squizlabs/PHP_CodeSniffer
README.mdではいくつかインストール方法が書いてありますが、今回はcomposerを使います。
phpcs:チェック
phpcbf:整形
$ composer global require "squizlabs/php_codesniffer=*"
$ ls ~/.composer/vendor/bin/
phpcbf phpcs
準備
まず、ディレクトリとファイルを準備します。
$ tree php_codesniffer
php_codesniffer
├── test
│ ├── test3.php
│ └── test4.php
├── test1.php
└── test2.php
中身は全部これです。
<?php
function hoge(){
if(TRUE)
{
echo "hoge";
}
}
phpcsでチェックするとエラーがたくさん出てます。
FILE: /path_to/php_codesniffer/test1.php
----------------------------------------------------------------------
FOUND 14 ERRORS AFFECTING 5 LINES
----------------------------------------------------------------------
2 | ERROR | [ ] Missing file doc comment
2 | ERROR | [ ] Missing function doc comment
2 | ERROR | [x] Opening brace should be on a new line
3 | ERROR | [x] Spaces must be used to indent lines; tabs are not
| | allowed
3 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found
| | 1
3 | ERROR | [ ] Expected "if (...) {\n"; found "if(...)\n\t{\n"
3 | ERROR | [x] TRUE, FALSE and NULL must be lowercase; expected
| | "true" but found "TRUE"
3 | ERROR | [x] There must be a single space between the closing
| | parenthesis and the opening brace of a multi-line IF
| | statement; found newline
4 | ERROR | [x] Spaces must be used to indent lines; tabs are not
| | allowed
4 | ERROR | [x] Line indented incorrectly; expected at least 4
| | spaces, found 1
5 | ERROR | [x] Spaces must be used to indent lines; tabs are not
| | allowed
5 | ERROR | [x] Line indented incorrectly; expected at least 8
| | spaces, found 2
6 | ERROR | [x] Spaces must be used to indent lines; tabs are not
| | allowed
6 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found
| | 1
----------------------------------------------------------------------
PHPCBF CAN FIX THE 11 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
Time: 17ms; Memory: 3.25Mb
整形
準備した4ファイル全てが整形されます。
$ ~/.composer/vendor/bin/phpcbf php_codesniffer --standard=PSR2
Changing into directory /path_to/php_codesniffer/test
Processing test4.php [PHP => 30 tokens in 8 lines]... DONE in 2ms (9 fixable violations)
=> Fixing file: 0/9 violations remaining [made 3 passes]... DONE in 5ms
Processing test3.php [PHP => 30 tokens in 8 lines]... DONE in 1ms (9 fixable violations)
=> Fixing file: 0/9 violations remaining [made 3 passes]... DONE in 5ms
Changing into directory /path_to/php_codesniffer
Processing test2.php [PHP => 30 tokens in 8 lines]... DONE in 1ms (9 fixable violations)
=> Fixing file: 0/9 violations remaining [made 3 passes]... DONE in 5ms
Processing test1.php [PHP => 30 tokens in 8 lines]... DONE in 1ms (9 fixable violations)
=> Fixing file: 0/9 violations remaining [made 3 passes]... DONE in 5ms
Patched 4 files
Time: 58ms; Memory: 3.5Mb
整形後のファイルの内容はこちらです。
<?php
function hoge()
{
if(true) {
echo "hoge";
}
}
phpcsを実行すると、ドキュメント書けってエラー以外は消えてます。
(なぜかifの後のスペースが入らない・・・)
$ ~/.composer/vendor/bin/phpcs php_codesniffer --standard=PSR2
FILE: /path_to/php_codesniffer/test/test4.php
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
----------------------------------------------------------------------
2 | ERROR | Missing file doc comment
2 | ERROR | Missing function doc comment
4 | ERROR | Expected "if (...) {\n"; found "if(...) {\n"
----------------------------------------------------------------------
最後に
ちゃんと活用すればレビュー時にコーディング規約に沿ってるかとか確認しなくてもよくなるので、非常に便利です。
vimでの保存時に自動で整形したり、フレームワークに組み込んだり、IDEのプラグインとして入れたり、Jenkinsでチェックしたりなんてことも出来るので、導入しない手はないですね。
参考サイト
phpcs & phpcbf コマンドについてメモ
PHP CodeSniffer を試す
PHP CodeSniffer ルールセットのカスタマイズ方法
PHP_CodeSniffer のインストール(composerで編)