6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

コーディング規約の自動チェック(PHPMD)

Last updated at Posted at 2018-01-29

概要

静的コード解析( PHPMD )のチェックできるようにする。

※ 今回のチェック対象は、 Laravel5.4のアプリ です

ファイル構成

下記のようにphpcsフォルダにファイルをまとめて設置します。

ファイル構成
 ├ laravel          # ソースコード格納フォルダ
 └ phpmd
    ├ .gitignore
    ├ composer.json
    ├ composer.lock
    ├ composer.phar
    └ phpmd.xml

まず、フォルダを作成

ターミナル
$ mkdir -p ./phpmd/
$ cd ./phpmd/

.gitignore追加

gitの除外条件を記載します。

.gitignore
vendor/
*.log

composer追加

下記コマンドにてPHPのCompoerをインストールする。

ターミナル
$ curl -sS https://getcomposer.org/installer | php

composer.json追加

下記のjsonファイルを作成します。

cmposer.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ファイルを作成して、設定を記載します。

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

参考サイト

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?