GANCHIKU.com

PHP_CodeSnifferいいお。

2006年8月30日
PHP

今、PEARの call for voteのフェーズにいて、どうやら通りそうなのだが、同じくcall for voteにいるPHP_CodeSnifferが私の好み。
こっちが開発者のサイト。使いかたとかはこっち。MySource Matrix Open Source CMS – PHP_CodeSniffer
PEARのプロポーザルはこっち。PEAR::PEPr::Details::PHP_CodeSniffer
デフォルトでは、指定したソースコード(ファイル、ディレクトリなど)をPEARのコーディング基準にマッチしているかをチェックしているのだ。
試しに私のServices_YouTubeをかますと。。。まず、summaryで。

shin@muse -> $ phpcs --report=summary --standard=PEAR YouTube.php YouTube/Exception.php

PHP CODE SNIFFER REPORT SUMMARY
--------------------------------------------------------------------------------
FILE ERRORS WARNINGS
--------------------------------------------------------------------------------
YouTube.php 5 19
--------------------------------------------------------------------------------
A TOTAL OF 5 ERROR(S) AND 19 WARNING(S) WERE FOUND IN 1 FILE(S)
--------------------------------------------------------------------------------

プギャー!Errorとか出た!Warningとか出た!ムムム!ちなみにmuseと言うのはマイ開発マシンの名前。
で、どんなErrorやらWarningかなーなんて思って、今度は、–reportingをfullにしてみる。つーか、デフォルトが–reporting=fullなので、書く必要はないけども。。

shin@muse -> phpcs --report=full --standard=PEAR YouTube.php YouTube/Exception.php

FILE: YouTube.php
--------------------------------------------------------------------------------
FOUND 5 ERROR(S) AND 19 WARNING(S) AFFECTING 24 LINE(S)
--------------------------------------------------------------------------------
[LINE 113] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 121] ERROR: Line indented incorrectly. Expected at least 4 spaces, but found 0.
[LINE 122] ERROR: Line indented incorrectly. Expected at least 4 spaces, but found 3.
[LINE 135] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 152] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 172] ERROR: Line indented incorrectly. Expected at least 4 spaces, but found 0.
[LINE 177] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 191] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 205] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 221] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 236] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 237] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 246] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 271] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 297] ERROR: File is being conditionally included. Use "include_once" instead.
[LINE 299] WARNING: Equals sign not aligned with surrounding assignments. Expected 3 spaces, but found 1 space.
[LINE 311] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 347] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 350] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 367] ERROR: File is being conditionally included. Use "include_once" instead.
[LINE 370] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 373] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 391] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 418] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
--------------------------------------------------------------------------------

うごぉ。大ダメージ!!!で、Errorのところは、インデントを揃えろってこと!条件付きのincludeは、inclure_once使えって!ムムム!
インデント揃えろ!って言われても。。。知らなかったがこんな感じか。

$hoge = "hogehoge";
$hogehog = "hoge";

つーか、ブログに書けねぇ。 使えばいいけど、めんどくさ。要は変数に代入している文が続いていたら、一番長い変数名に合わせてスペースで埋めろってこと。これってPEARコーディング基準だったっけ。。

まぁ、いいや。次は、Warning
これは、全部、文が85文字越えているので改行しろ!ってこと。まぁね。。。ちょいと苦しい言い訳をしてもらうと、Exceptionクラスを作ったりすると例えば、Services_YouTube_Exceptionってなるわけじゃん。そして、それをthrowして、そのServices_YouTube_Exceptionの引数にどんな問題だったかなんて書くともう余裕で85文字越えちゃうの。。さらに、throwするときって、いくつか入れ子になった後だよね。つまり、メソッドの中。なので、それだけでスペース8個分、つまり、最低8文字持っていかれる。で、if hogehoge {throw}とかするんだけど、これで、12文字。85文字を守りたいのは山々なのだが。。。

てな感じで使えるのだ!たのすぃー!

*デフォルトでは、–reportingは、fullになっていて、–standardはPEARになっている。PEAR用しかないから。しかし、コード基準ファイルを自分で定義して入れることも可能。ここのインストレーションのページでは、Squizというコード基準ファイルが置いてあり、pearコマンドでインストールができる。ただ、他のコード基準ファイルを入れると–standardのオプションは必須になり、–standard=PEARとか入れなくちゃいけない。ヲレ流コード基準がある人以外はPEARのコード基準がいいと思うので、特にコード基準ファイルを作る必要無しだと思ふ。

PHP_CodeSnifferいいお。 への2件のコメント

  1. sakamoto より:

    同意、マジいいです。エディッタのコマンド機能に実装して使いまくっています。
    PHP/CodeSniffer.php の 471行目を
      echo $filename.’[LINE '.$line.'] ‘.$error.”\n”;
    に変更すると正規表現でのパースが楽になるのでオススメです。

  2. shin より:

    どもー。試してみるっす。

コメントをどうぞ

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

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Shin Ohno 2003-2012