コーディング規約の自動チェック(PHPMD)
概要
静的コード解析( PHPMD )のチェックできるようにする。
※ 今回のチェック対象は、 Laravel5.4のアプリ です
ファイル構成
下記のようにphpcsフォルダにファイルをまとめて設置します。
├ laravel # ソースコード格納フォルダ
└ phpmd
├ .gitignore
├ composer.json
├ composer.lock
├ composer.phar
└ phpmd.xml
まず、フォルダを作成
$ mkdir -p ./phpmd/
$ cd ./phpmd/
.gitignore追加
gitの除外条件を記載します。
vendor/
*.log
composer追加
下記コマンドにてPHPのCompoerをインストールする。
$ curl -sS https://getcomposer.org/installer | php
composer.json追加
下記のjsonファイルを作成します。
{
"require-dev": {
"phpmd/phpmd": "*",
"squizlabs/php_codesniffer": "2.*"
},
"scripts": {
"report": [
"./vendor/bin/phpmd ../laravel text phpmd.xml --suffixes php --exclude vendor/*,tests/*"
]
}
}
※ エラーにしたくない場合は、下記パラメータを付けることもできる
--ignore-violations-on-exit
※ あまりにもエラーが多い場合は、ファイルに出力することもできます
--reportfile ./report.log
PHPMDのコマンド
ヘルプでオプションや設定パラメータを見てみるといろいろあるようです。
$ ./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
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
--ignore-violations-on-exit: will exit with a zero code, even if any violations are found
PHPMD 設定
下記xmlファイルを作成して、設定を記載します。
<?xml version="1.0"?>
<!-- PHPコードの静的コード解析ツール -->
<ruleset name="My first PHPMD rule set"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="
http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>
My custom rule set that checks my code...
</description>
<!-- コードサイズが大きすぎないかチェック (9/10) -->
<rule ref="rulesets/codesize.xml">
<exclude name="ExcessiveClassComplexity" />
</rule>
<!-- 命名規則など議論の余地がある部分を検出するチェック (2/6) -->
<rule ref="rulesets/controversial.xml">
<exclude name="Superglobals" />
<exclude name="CamelCasePropertyName" />
<exclude name="CamelCaseParameterName" />
<exclude name="CamelCaseVariableName" />
</rule>
<!-- 設計上の関連のチェック (7/7) -->
<rule ref="rulesets/design.xml" />
<!-- 変数名など名前関連のチェック (5/6) -->
<rule ref="rulesets/naming.xml">
<exclude name="ShortVariable" />
<exclude name="ShortMethodName" />
</rule>
<!-- 使われていないコードのチェック (3/3) -->
<rule ref="rulesets/unusedcode.xml" />
<!-- 綺麗なコードかチェック (1/3) -->
<rule ref="rulesets/cleancode.xml">
<exclude name="ElseExpression" />
<exclude name="StaticAccess" />
</rule>
</ruleset>
※ ルールを定義したxmlファイルは以下のフォルダに存在します。
「 ./vendor/phpmd/phpmd/src/main/resources/rulesets/... 」
各種ライブラリをインストール
$ php composer.phar install
※「 vendor 」フォルダにcomposer.jsonで指定した各種ライブラリがインストールされる
PHPMD
静的コード解析を実行してみる。
$ php composer.phar report
参考サイト
Author And Source
この問題について(コーディング規約の自動チェック(PHPMD)), 我々は、より多くの情報をここで見つけました https://qiita.com/reflet/items/86fd6c58200da7c8be6b著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .