LoginSignup
7
5

More than 3 years have passed since last update.

PHP_CodeSniffierのインストールと使い方

Last updated at Posted at 2019-06-25

環境

CentOS 7.5
composer 1.7.2
PHP 7.2
Laravel 5.7.11

インストール

$ composer require --dev "squizlabs/php_codesniffer=*"

devオプションで開発環境でのみインストールするようにしている。packagistの記載ではglobalオプションをつけているが、composerのホームディレクトリにインストールしない場合は不要。

インストールが完了すると、codesnifferを使用できるようになるので、ヘルプを表示してみる。

$ cd プロジェクトフォルダパス
$ ./vendor/bin/phpcs -h
Usage: phpcs [-nwlsaepqvi] [-d key[=value]] [--colors] [--no-colors]
  [--cache[=<cacheFile>]] [--no-cache] [--tab-width=<tabWidth>]
  [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>]

以下省略

使い方

デフォルトで使用可能なコーディング規約を確認

$ ./vendor/bin/phpcs -i
The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz and Zend

プロジェクトのルートディレクトリ直下にphpcs.xmlを作成(これがルールファイルになる)。以下はPSR-1とPSR-2の規約をチェックするルールファイル。プロジェクト直下のnode_modules, storage, vendorディレクトリはチェック対象外とする。

<?xml version="1.0"?>
<ruleset name="Custom Standard">
    <rule ref="PSR2">
        <!-- "PSR2" の中で除外するルールがあれば記載 -->
        <!--<exclude name="Generic.Files.LineLength"/>-->
        <!--<exclude name="PSR1.Classes.ClassDeclaration"/>-->
    </rule>
    <!-- 追加するルールがあれば記載 -->
    <!--<rule ref="PEAR.WhiteSpace.ObjectOperatorIndent"/>-->
    <!-- 除外するファイル・ディレクトリ -->
    <exclude-pattern>node_modules/</exclude-pattern>
    <exclude-pattern>storage/</exclude-pattern>
    <exclude-pattern>vendor/</exclude-pattern>
</ruleset>

設定したチェックルールの一覧を確認する。

$ ./vendor/bin/phpcs -e
The PSR2 Rule Set standard contains 42 sniffs

Generic (12 sniffs)
-------------------
  Generic.ControlStructures.InlineControlStructure
  Generic.Files.ByteOrderMark
  Generic.Files.LineEndings

以下省略

各ルールとチェックしている規約の内容はこちらを参考にさせていただきました。
PHP CodeSnifferにおけるPSR1,2の検知箇所と対応するSniffer

phpcs.xmlではルールとしてPSR2のみを指定したが、チェックするルール自体は以下ファイルに記載されており、この中にPSR-1が記載されているので、PSR-1とPSR-2の両方がチェックされる事が分かる。
src/Standards/PSR2/ruleset.xml

ファイル別に結果を出力

$ ./vendor/bin/phpcs --report=summary .
PHP CODE SNIFFER REPORT SUMMARY
---------------------------------------------------------------------------------------
FILE                                                             ERRORS  WARNINGS
---------------------------------------------------------------------------------------
/var/www/html/app/Http/Controllers/Admin/Test1Controller.php     0       2
/var/www/html/app/Http/Controllers/Admin/Test2Controller.php     6       2
/var/www/html/app/Http/Controllers/Admin/Test3Controller.php     0       2
/var/www/html/app/Http/Controllers/Admin/Test4Controller.php     7       7
/var/www/html/app/Http/Controllers/Admin/Test5Controller.php     0       6
---------------------------------------------------------------------------------------
A TOTAL OF 13 ERRORS AND 19 WARNINGS WERE FOUND IN 5 FILES
---------------------------------------------------------------------------------------
PHPCBF CAN FIX 13 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------------------

ルール別に結果を出力

$ ./vendor/bin/phpcs --report=source /path/to/code
PHP CODE SNIFFER VIOLATION SOURCE SUMMARY
-------------------------------------------------------------------------------------------
    STANDARD  CATEGORY            SNIFF                                               COUNT
-------------------------------------------------------------------------------------------
[ ] Generic   Files               Line length too long                                19
[x] PSR2      Methods             Function call signature multiple arguments          4
[x] PSR2      Methods             Function call signature content after open bracket  3
[x] PSR2      Methods             Function call signature close bracket line          2
[x] PSR2      Methods             Function call signature indent                      2
[x] Squiz     Control structures  Control signature space after close parenthesis     1
[x] Squiz     Control structures  Control signature space after keyword               1
-------------------------------------------------------------------------------------------
A TOTAL OF 32 SNIFF VIOLATIONS WERE FOUND IN 7 SOURCES
-------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 6 MARKED SOURCES AUTOMATICALLY (13 VIOLATIONS IN TOTAL)
-------------------------------------------------------------------------------------------

結果レポートをファイルとして出力。
プロジェクトフォルダ直下のresult以下にtest_phpcs.xmlファイルが作成される。

$ ./vendor/bin/phpcs --report=checkstyle --report-file=result/test_phpcs.xml .

■参考にさせていただきました
新標準PSRに学ぶきれいなPHP
https://www.slideshare.net/yandod/psrphp

PSR-1 基本コーディング規約(日本語)
http://www.infiniteloop.co.jp/docs/psr/psr-1-basic-coding-standard.html

PSR-2 コーディングガイド(日本語)
http://www.infiniteloop.co.jp/docs/psr/psr-2-coding-style-guide.html

PHP PSR一覧 2017年版
https://qiita.com/rana_kualu/items/f41d8f657df7709bda0f

PHP CodeSnifferにおけるPSR1,2の検知箇所と対応するSniffer
https://qiita.com/piotzkhider/items/c90dd9253e9822fab3a2

7
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
7
5