PHPのコーディングスタイルをチェックするツールとしてPHP_CodeSniffer
があるが、これのCodeIgniter
用のもの__「CodeIgniter-for-PHP_CodeSniffer」__があることが分かり、早速インストールした(環境はCentOS)。その手順をまとめる。
※他のフレームワーク(例: FuelPHP
)も同様の手順でインストール可
PHP_CodeSnifferのインストール
これはたくさん記事が存在するので、インストールコマンドだけ記述。
※pear
がインストールされている想定。
# pearでインストール
$ sudo pear install PHP_CodeSniffer
以上でインストール完了、確認。
$ phpcs -i
The installed coding standards are PEAR, Squiz, PSR2, PHPCS, PSR1, Zend and MySource
上記の文言が表示されていたらOK。
CodeIgniter-for-PHP_CodeSnifferのインストール
# ディレクトリ移動
$ cd /tmp/
# ソースをダウンロード
$ git clone https://github.com/thomas-ernest/CodeIgniter-for-PHP_CodeSniffer.git
# CodeSniffer/Standardsディレクトリに移動
$ cd /path_to_CodeSniffer_directory/Standards/
# ディレクトリ作成
$ sudo mkdir CodeIgniter
# ソースをコピー
$ sudo cp -R /tmp/CodeIgniter-for-PHP_CodeSniffer/src/* CodeIgniter/
上記手順でインストール完了!
私の環境では、CodeSniffer/Standardsディレクトリは/usr/share/pear/PHP/CodeSniffer/Standards/
だった。
あとは、正しくインストールできているか確認する。
$ phpcs -i
The installed coding standards are PEAR, Squiz, PSR2, PHPCS, CodeIgniter, PSR1, Zend and MySource
一覧にCodeIgniter
が表示されていればOK。
使い方
以下のコマンドをたたく。(ファイル名は適当に作成したHoge.php
)
$ phpcs --standard=CodeIgniter Hoge.php
FILE: /path_to_php_directory/Hoge.php
----------------------------------------------------------------------
FOUND 17 ERRORS AFFECTING 9 LINES
----------------------------------------------------------------------
1 | ERROR | [x] End of line character is invalid; expected "\n" but
| | found "\r\n"
1 | ERROR | [ ] Filename "Hoge" doesn't match the name of the class
| | that it contains "Hoge" in lower case. "Hoge" was
| | expected.
2 | ERROR | [ ] Missing file doc comment
4 | ERROR | [ ] Missing class doc comment
6 | ERROR | [ ] Tabs must be used to indent lines; spaces are not
| | allowed for code indentation
6 | ERROR | [ ] Missing function doc comment
6 | ERROR | [x] Opening brace should be on a new line
7 | ERROR | [ ] Tabs must be used to indent lines; spaces are not
| | allowed for code indentation
8 | ERROR | [ ] Tabs must be used to indent lines; spaces are not
| | allowed for code indentation
10 | ERROR | [ ] Tabs must be used to indent lines; spaces are not
| | allowed for code indentation
10 | ERROR | [ ] Missing function doc comment
10 | ERROR | [x] Opening brace should be on a new line
11 | ERROR | [ ] Tabs must be used to indent lines; spaces are not
| | allowed for code indentation
11 | ERROR | [ ] Single-quoted strings should be used unless it
| | contains variables, special chars like \n or single
| | quotes.
12 | ERROR | [ ] Tabs must be used to indent lines; spaces are not
| | allowed for code indentation
12 | ERROR | [ ] No comment block marks the end of file instead of
| | the closing PHP tag. Please add a comment block
| | containing only "Location: ./controllers/Hoge.php".
12 | ERROR | [ ] No comment block marks the end of file instead of
| | the closing PHP tag. Please add a comment block
| | containing only "End of file Hoge.php".
----------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
こんな感じで普通のCodeSnifferと同じような結果が表示される。
直し方は以下のコマンドをたたく。
$ phpcbf --standard=CodeIgniter Hoge.php
Changing into directory /path_to_php_directory
Processing Hoge.php [PHP => 74 tokens in 13 lines]... DONE in 23ms (4 fixable violations)
=> Fixing file: 0/4 violations remaining [made 4 passes]... DONE in 27ms
Patched 1 file
Time: 241ms; Memory: 2Mb
以上! Let's enjoy CodeIgniter life!
追記(2016/06/28)
pear
のバージョンが古いとPHP_CodeSniffer
のインストールでコケる可能性があるので、自分はこちらの記事を参考にpear
をアップグレードするとうまくいった。(根本解決ではない、と記載があるのでその点に注意)