WordPress

VCCWでphpmdを使う

phpmdといえばWordBench京都12月号での公開処刑に使われた因縁のあるツールオープンソースの静的解析ツールで、使われていない変数や神クラスといったコード上の問題を発見することができるものです。 使い方はPHPの […]

広告ここから
広告ここまで

phpmdといえばWordBench京都12月号での公開処刑に使われた因縁のあるツールオープンソースの静的解析ツールで、使われていない変数や神クラスといったコード上の問題を発見することができるものです。

使い方はPHPの静的コード解析ツール『PHPMD』を使ってみた。 – Qiitaなどが参考になるかなと思います。

で、これをVCCWに入れて、プラグインとかテーマを公開処刑されにくいメンテしやすいコードにしてみようと思います。

VCCWに入れる場合の注意点

VCCWはChefで構成管理されていますので、vagrant sshしてからwgetcomposer installするというやり方だとvagrant provisionなどでプロビジョニングされた時に吹っ飛んでしまいます。

じゃあどうすればいいかというと、chefのレシピに書いてそっちでインストールさせます。

触った箇所

ちなみにこのやり方、composerでインストールする系のものであれば「phpmd」の部分を書き換えると大体いけるっぽいので、他のツールを入れたい時などにも使えるかと思います。

使ってみる

VMの中にphpmdを入れているので、vagrant sshしてから操作します。

$ vagrant ssh
$ cd /var/www/wordpress/wp-content/plugins
$ git clone git@github.com:torounit/hello-kushimoto.git
$ cd hello-kushimoto
$ phpmd ../ text unusedcode

/var/www/wordpress/wp-content/plugins/hello-kushimoto/src/class-hello-kushimoto-option-page.php:26	Avoid unused private fields such as '$option_seeker'.
/var/www/wordpress/wp-content/plugins/hello-kushimoto/src/class-hello-kushimoto-option-page.php:113	Avoid unused private methods such as 'create_option'.
/var/www/wordpress/wp-content/plugins/hello-kushimoto/src/class-hello-kushimoto-option-page.php:125	Avoid unused private methods such as 'get_speaker_name'.
/var/www/wordpress/wp-content/plugins/hello-kushimoto/src/class-hello-kushimoto-speaker-seeker.php:61	Avoid unused private methods such as 'convert_to_class_name'.

例えば最後の行だと、
https://github.com/torounit/hello-kushimoto/blob/master/src/class-hello-kushimoto-speaker-seeker.php#L61
の関数が使われていないんじゃないか?という解析結果ですね。実際には
https://github.com/torounit/hello-kushimoto/blob/master/src/class-hello-kushimoto-speaker-seeker.php#L50
でコールされているのですが、WordPress独自の呼び出し方array( $this, 'convert_to_class_name' )がデフォルトでは解析できていないのかもしれません(要検証)。

特にWPの場合は、フックの仕様上unusedな変数がどうしても出てくる場合などもありますので、「絶対に全部解決しなければならない」と思いつめる必要はないかと思います。

ちなみにコマンドは

$ phpmd
Mandatory arguments:
1) A php source code filename or directory. Can be a comma-separated string
2) A report format
3) A ruleset filename or a comma-separated string of rulesetfilenames

Available formats: xml, text, html.
Available rulesets: cleancode, codesize, controversial, design, naming, unusedcode.

Optional arguments that may be put after the mandatory arguments:
--minimumpriority: rule priority threshold; rules with lower priority than this will not be used
--reportfile: send report output to a file; default to STDOUT
--suffixes: comma-separated string of valid source code filename extensions, e.g. php,phtml
--exclude: comma-separated string of patterns that are used to ignore directories
--strict: also report those nodes with a @SuppressWarnings annotation

ということで、

$ phpmd [解析するファイル名・ディレクトリ名] [レポートフォーマット|xml/text/html] [解析内容|cleancode, codesize, controversial, design, naming, unusedcode]

という書き方をすれば良さそうです。

取り急ぎ以上です。

余談

ということで、早速PR出してます。(マージ済)

https://github.com/vccw-team/vccw/pull/113

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts