hagetak's blog

どうも、はげたかです。

CakePHP のプロジェクトでコーディング規約に沿っているか自動チェックをする

CakePHP のプロジェクトでコーディング規約に沿っているか自動チェックをする

概要

よくプルリクエストで、コーディング規約に沿っていない箇所をご指摘頂く。コーディング規約はあらかじめ決まっているため、事前に気づくことができる。

プルリク => レビュー => 修正(コーディング規約に沿っていない箇所) => レビュー => ・・・

1サイクル(レビュー => 修正)が増えるので、当然、自分やレビュー者にも負担がかかり、時間も無駄にしてしまう。(そしてコミットログを汚すことにもなる)

ということで、コーディング規約チェッカー(PHP_CodeSniffer 2.0.0)を導入してみては?と提案いただいたので導入してみる。

ココで得られること

  • PHP_CodeSniffer のインストール
  • CakePHP 用のルールをセット
  • 簡単なカスタマイズができる(インデントに Tabを許可する)
    • Tabs must be used to indent lines; spaces are not allowed

コーディング規約一覧

PSR-0

http://www.infiniteloop.co.jp/docs/psr/psr-0.html

PSR-1

http://www.infiniteloop.co.jp/docs/psr/psr-1-basic-coding-standard.html

PSR-2

http://www.infiniteloop.co.jp/docs/psr/psr-2-coding-style-guide.html

CakePHP2

https://book.cakephp.org/2.0/ja/contributing/cakephp-coding-conventions.html

PHP_CodeSniffer 2.0.0

Composer のインストール

Composer とは、PHPのパッケージ管理ツールである。詳しくは、以下を参照すること。

http://qiita.com/atwata/items/d6f1cf95ce96ebe58010

# composer のインストール ※ 既にインストール済みの場合はSKIPすること
curl http://getcomposer.org/installer | php 

# 利用している shell の設定ファイルに環境設定を書き込み
vim ~/.zshrc

################ 以下を記述 #####################

# For Composer, Uses vendor's bin.
export PATH="$PATH:$HOME/.composer/vendor/bin"

################ ここまで #######################

# 変更した設定ファイルを読みこむ
source ~/.zshrc

CodeSniffer をインストール

composer global require "squizlabs/php_codesniffer"
composer global show --installed | grep php_codesniffer

Changed current directory to /Users/{{USER_NAME}_/.composer
You are using the deprecated option "installed". Only installed packages are shown by default now. The --all option can be used to show all packages.
squizlabs/php_codesniffer    2.8.0   PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined s...

# 使えるかチェック
phpcbf --version
PHP_CodeSniffer version 2.6.1 (stable) by Squiz (http://www.squiz.net)

CakePHP2 用のルールを取得

上記のインストールの他に、CakePHP用のコーディング規約ルールを取得しなければいけない。

https://github.com/cakephp/cakephp-codesniffer/

CakePHP2 のルールを取得するには、 update-fxer-2 を取得する。

master は CakePHP3用でPSR-2 を準拠しているため全く異なる

# 好みの場所にインストールする
# 僕の場合は、 ~/.sniffs/{{Intalled Coding Rules}}
cd ~/
curl -LO https://github.com/cakephp/cakephp-codesniffer/archive/update-fxer-2.zip
unzip update-fxer-2.zip -d sniffs
cd ~/.sniffs/

# ~/.sniffs/{{NAME}} になるので正しい名前を命名する
# ※ ここでバージョン指定{{CakePHP2}} したいが、エラーになる.><;
mv cakephp-codesniffer-update-fxer-2 CakePHP

# 削除忘れずに
rm ~/update-fxer-2.zip

取得したルールを反映させる

# 取得したルールセットを反映させる
phpcs --config-set installed_paths ~/.sniffs/                           

# 反映を確認する
phpcs -i
The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zend and CakePHP2

phpcbf -i
The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zend and CakePHP2

実際に使ってみよう。

phpcs --standard=CakePHP /PATH/TO/CAKEPHP_APP/app/Controller/AppController.php

----------------------------------------------------------------------
FOUND 7 ERRORS AND 1 WARNING AFFECTING 5 LINES
----------------------------------------------------------------------
 33 | ERROR   | [ ] Expected 1 space before curly opening bracket
 36 | ERROR   | [x] Tabs must be used to indent lines; spaces are not
    |         |     allowed
 36 | WARNING | [ ] Possible useless method overriding detected
 36 | ERROR   | [ ] Missing function doc comment
 37 | ERROR   | [x] Tabs must be used to indent lines; spaces are not
    |         |     allowed
 37 | ERROR   | [x] Opening brace should be on the same line as the
    |         |     declaration
 38 | ERROR   | [x] Tabs must be used to indent lines; spaces are not
    |         |     allowed
 39 | ERROR   | [x] Tabs must be used to indent lines; spaces are not
    |         |     allowed
----------------------------------------------------------------------
PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

インデントのタブの指摘が多すぎる。うあああああ。

インデントにタブ利用を許可するカスタマイズする

cd ~/.sniffs/CakePHP 
cp ruleset.xml ruleset.xml.org                                                      

# 62行目の'<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>'をコメントアウト
vim ruleset.xml

### こうする
 <!-- <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/> -->

diff ruleset.xml ruleset.xml.org 
62c62
<  <!-- <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/> -->
---
>  <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
phpcs --standard=CakePHP /PATH/TO/CAKEPHP_APP/app/Controller/AppController.php

----------------------------------------------------------------------
FOUND 3 ERRORS AND 1 WARNING AFFECTING 3 LINES
----------------------------------------------------------------------
 33 | ERROR   | [ ] Expected 1 space before curly opening bracket
 36 | WARNING | [ ] Possible useless method overriding detected
 36 | ERROR   | [ ] Missing function doc comment
 37 | ERROR   | [x] Opening brace should be on the same line as the
    |         |     declaration
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

これで、インデントのエラーが消え、本質的なコーディング規約を見ることができる。

CakePHP2 実践入門 (WEB+DB PRESS plus)

CakePHP2 実践入門 (WEB+DB PRESS plus)

CakePHPで学ぶ継続的インテグレーション

CakePHPで学ぶ継続的インテグレーション

PHPフレームワーク CakePHP 3入門

PHPフレームワーク CakePHP 3入門