LoginSignup
28
24

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-10-28

概要

コーディング規約( PSR2 )が守られているかチェックを行うコマンドを設置する。

※ 今回のチェック対象は、 Laravel5.4のアプリ です
※ Laravel以外のフレームワークであれば、CodeSnifferの設定を調整する

ファイル構成

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

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

まず、フォルダを作成

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

.gitignore追加

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

.gitignore
vendor/
*.log

composer追加

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

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

composer.json追加

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

cmposer.json
{
    "require-dev": {
        "squizlabs/php_codesniffer": "2.*"
    },
    "scripts": {
        "report": [
            "./vendor/bin/phpcs -sq --no-colors --report=source --report-diff=./report-diff.log --report-full=./report-full.log --standard=phpcs.xml"
        ],
        "convert": [
            "./vendor/bin/phpcbf --standard=./phpcs.xml"
        ]
    }
}

※ エラーにしたくない場合は、下記パラメータを付けることもできる
   --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1

PHP CodeSnifferのコマンド

ヘルプでオプションや設定パラメータを見てみるといろいろあるようです。

ターミナル
$ ./vendor/bin/phpcs --help
Usage: phpcs [--colors] [--no-colors] [--report=<report>] [--report-<report>=<reportFile>] ...

         Set runtime value (see --config-set) 
    -n   Do not print warnings (shortcut for --warning-severity=0)
    -s   Show sniff codes in all reports
    -q   Quiet mode; disables progress and verbose output

PHP CodeSniffer 設定

下記xmlファイルを作成して、設定を記載します。

phpcs.xml
<?xml version="1.0"?>
<ruleset name="Laravel Standards">
    <description>The Laravel Coding Standards</description>

    <!-- 対象フォルダ -->
    <file>../laravel/app</file>
    <file>../laravel/config</file>
    <file>../laravel/resources</file>
    <file>../laravel/routes</file>
    <file>../laravel/tests</file>

    <!-- 除外したいファイル、ディレクトリ -->
    <exclude-pattern>*/database/*</exclude-pattern>
    <exclude-pattern>*/cache/*</exclude-pattern>
    <exclude-pattern>*/*.js</exclude-pattern>
    <exclude-pattern>*/*.css</exclude-pattern>
    <exclude-pattern>*/*.xml</exclude-pattern>
    <exclude-pattern>*/*.blade.php</exclude-pattern>
    <exclude-pattern>*/autoload.php</exclude-pattern>
    <exclude-pattern>*/storage/*</exclude-pattern>
    <exclude-pattern>*/docs/*</exclude-pattern>
    <exclude-pattern>*/vendor/*</exclude-pattern>
    <exclude-pattern>*/migrations/*</exclude-pattern>

    <!-- PSR2をベースとする -->
    <rule ref="PSR2">
        <!-- 除外したい項目 -->
        <exclude name="Generic.Files.LineLength.TooLong"/>
    </rule>

    <arg name="colors"/>
    <arg value="p"/>

    <ini name="memory_limit" value="128M"/>
    <rule ref="PSR2"/>
</ruleset>

各種ライブラリをインストール

ターミナル
$ php composer.phar install

コマンド実行

レポート出力

コーディング規約(PSR2)に準拠しているかチェックしてみる。

ターミナル
$ php composer.phar report

※細かい結果は、下記ファイルに出力されます。
 ・ ./phpcs/report-diff.log
 ・ ./phpcs/report-full.log

自動変換

コミット前にどのように修正されているかは確認した方がいいかもしれません。

ターミナル
$ php composer.phar convert

参考サイト

28
24
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
28
24