php の code metrics は、上記のentry で PhpMetricsによるものを記載していますが、 PhpMetrics や、PHP Depend では、計測できないことがありました。
計測できない原因は不明でしたが、PHPMD では計測できましたので、 以下では PHPMD の使用法をメモしておきます。
install by Composer
https://pdepend.org/download/index.html
上記urlによれば、PHPMDのインストール方法には Composer や git clone 等、複数ありますが、 以前の PhpMetrics が Composer でしたので、今回も利用します。
まぁ、以下のインストール方法は、上記urlの通りです。
また、PHPMDは、別のmetrics ツールである PHP Depend に依存しているらしく これも併せて、vender以下にインストールされます。
$ vi composer.json
{"require": { "pdepend/pdepend" : "@stable" }}
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install
$ ./vendor/bin/phpmd --help
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
Example: phpmd /path/to/source format ruleset
Available formats: ansi, html, json, text, xml.
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.
Use asterisks to exclude by pattern. For example *src/foo/*.php or *src/foo/*
--strict: also report those nodes with a @SuppressWarnings annotation
--ignore-violations-on-exit: will exit with a zero code, even if any violations are found
code metrics を計測してみる
「phpmd --help」で分かるように PHPMDは 「$ phpmd /path/to/source format ruleset」のように実行します。
ちなみに、ruleset には、以下があるようです。
| RULE | NOTE |
|---|---|
| cleancode | 整形ルール |
| codesize | コードサイズと複雑度 |
| controversial | 賛否両論のあるものの為、検証対象から除外も可 |
| design | ソフトウェアデザインというより、コーディングルール |
| naming | 命名規則 |
| unusedcode | 未使用コードチェック |
で、以下の通りです。
$ ./vendor/bin/phpmd ~/tmp/form/cgi-bin2 text cleancode,codesize
cgi-bin2/form_api.php:122 The function main_confirm_to_complete() has a Cyclomatic Complexity of 15.
The configured cyclomatic complexity threshold is 10.
cgi-bin2/form_api.php:122 The function main_confirm_to_complete() has an NPath complexity of 440.
The configured NPath complexity threshold is 200.
cgi-bin2/form_api.php:122 The function main_confirm_to_complete() has 238 lines of code.
Current threshold is set to 100. Avoid really long methods.
cgi-bin2/form_api.php:223 Avoid using undefined variables such as '$db_name' which will lead to PHP notices.
cgi-bin2/form_api.php:223 Avoid using undefined variables such as '$form_id' which will lead to PHP notices.
cgi-bin2/form_api.php:228 Avoid using undefined variables such as '$save_path' which will lead to PHP notices.
cgi-bin2/form_api.php:233 Avoid using undefined variables such as '$mailfromaddress' which will lead to PHP notices.
cgi-bin2/form_api.php:235 Avoid using undefined variables such as '$mailfromaddress' which will lead to PHP notices.
cgi-bin2/form_api.php:236 Avoid using undefined variables such as '$systemmail' which will lead to PHP notices.
: :