[PHP] phpcsをグローバルインストールしてPSR-12チェックする

カテゴリ: phpcs

phpではPSR-12というコーディング規約があります。作成したphpプログラムがPSR-12の規約に合致しているかをチェックしたい場合、phpcsコマンドを使うと便利です。

この記事では、composerでグローバルインストールすることで、コマンドラインからphpcsコマンドをインストールして、PHPのコードがpsr-2のコーディング規約に準拠しているかチェックする方法を説明します。

作業手順

composerコマンドに対してglobalオプションを指定してphpcsをグローバルインストールします。

composer global require squizlabs/php_codesniffer

composerでグローバルインストールしたコマンドは、デフォルトでは~/.composer/vendor/binの下に用意されます。
phpcsコマンドをかんたんに実行できるようパスを通しておきます。

echo 'export PATH=$HOME/.composer/vendor/bin:$PATH' >> ~/.bash_profile
source ~/.bash_profile

オプション--versionを指定してphpcsを実行し、コマンドが使えるか確認と、バージョンの確認をします。

phpcs --version

下記のコマンドで、PSR-12のコーディング規約に沿っているか、チェックすることができます。

phpcs foo.php --standard=PSR12

オプション-iを利用すると、--standardで指定できる規約の一覧を確認できます。

phpcs -i
The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1 and PSR12

phpcsのオプション一覧

コマンドphpcs --helpでコマンドのヘルプを確認できます

phpcs --help

Usage: phpcs [-nwlsaepqvi] [-d key[=value]] [--colors] [--no-colors]
  [--cache[=<cacheFile>]] [--no-cache] [--tab-width=<tabWidth>]
  [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>]
  [--report-width=<reportWidth>] [--basepath=<basepath>] [--bootstrap=<bootstrap>]
  [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]
  [--runtime-set key value] [--config-set key value] [--config-delete key] [--config-show]
  [--standard=<standard>] [--sniffs=<sniffs>] [--exclude=<sniffs>]
  [--encoding=<encoding>] [--parallel=<processes>] [--generator=<generator>]
  [--extensions=<extensions>] [--ignore=<patterns>] [--ignore-annotations]
  [--stdin-path=<stdinPath>] [--file-list=<fileList>] [--filter=<filter>] <file> - ...

 - ローカルファイルやディレクトリではなく、標準入力から指定された文字列をチェックする
 -n 警告を表示しない (--warning-severity=0のショートカットです)
 -w 警告とエラーの両方を表示する(これがデフォルト)
 -l サブディレクトリを再帰せず、ローカルディレクトリのみチェックする
 -s すべてのレポートにsniff codeを表示する
 -a 対話的に実行する
 -e どの規約が適用されたかsniffを表示する
 -p 実行中の進捗を表示する
 -q Quiet mode:進捗状況や冗長な出力を無効にする
 -m エラーメッセージを記録しないようにする
    (多くのメモリを節約できますが、多くのレポートが使用されない)
 -v 処理済みファイルの表示する
 -vvv ルールセットとトークンの出力を表示する
 -vvv sniffの処理情報を表示する
 -i インストールされているコーディング規約の一覧を表示する
 -d php.iniの[key]を[value]に設定する 値が省略された場合は[true]とする

 --help このヘルプメッセージを表示する
 --version バージョン情報を表示する
 --colors 出力に色をつける
 --no-colors 出力に色を使わない(デフォルト)
 --cache 実行間の結果をキャッシュする
 --no-cache 実行間の結果をキャッシュしない(デフォルト)
 --ignore-annotations コードコメント中の phpcs: 注釈をすべて無視する

 <cacheFile> キャッシュに特定のファイルを使用 (デフォルトでは一時ファイルを使用)
 <basepath> レポート内のファイルパスの先頭から削除するパス
 <bootstrap> 処理を開始する前に実行するファイルのカンマ区切りリスト
 <エンコーディング> チェックするファイルのエンコーディング(デフォルトはutf-8)
 <extensions> チェックするファイル拡張子のカンマ区切りリスト
                ファイルの種類は、ext/typeで指定できる
                例:module/php,es/js
 <file> チェックする1つ以上のファイルおよび/またはディレクトリ
 <fileList> チェックするファイルやディレクトリのリストを含むファイル (1行に1つ)
 <filter> "gitmodified "または "gitstaged "フィルタのいずれかを使用する
                または、カスタムフィルタクラスのパスを指定する
 <ジェネレータ> "HTML"、"Markdown"、"Text "ジェネレータのいずれかを使用します
                (チェックではなく、ドキュメント生成を強制する)
 <patterns> ファイルやディレクトリを無視するためのパターンをカンマで区切ったリスト
 <processes> 同時にチェックするファイルの数 (デフォルトは1)
 <report> "full"、"xml"、"checkstyle"、"csv "のいずれかを出力します
                "json"、"junit"、"emacs"、"source"、"summary"、"diff"
                「svnblame", "gitblame", "hgblame" または "notifysend" レポート。
                または、カスタムレポートクラスへのパスを指定する
                (デフォルトでは "full"レポートが出力される)
 <reportFile> 指定されたファイルパスにレポートを書き込む
 <reportWidth> 何列のワイドスクリーンのレポートを印刷するか
                サポートされている場合は、現在の画面幅を使用するために "auto" に設定する
 <severity> エラーまたは警告を表示するために必要な最小の重要度
 <sniffs> チェックに含める、または除外するスニッフコードをカンマで区切ったリスト
                (すべてのsniffは指定された規格の一部である必要がある)
 <standard> 使用するコーディングスタンダードの名前またはパス
 <stdinPath> STDINを処理する場合、STDINが処理されるファイルパス
 <tabWidth> タブのスペース幅
phpcs --help

Usage: phpcs [-nwlsaepqvi] [-d key[=value]] [--colors] [--no-colors]
  [--cache[=<cacheFile>]] [--no-cache] [--tab-width=<tabWidth>]
  [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>]
  [--report-width=<reportWidth>] [--basepath=<basepath>] [--bootstrap=<bootstrap>]
  [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]
  [--runtime-set key value] [--config-set key value] [--config-delete key] [--config-show]
  [--standard=<standard>] [--sniffs=<sniffs>] [--exclude=<sniffs>]
  [--encoding=<encoding>] [--parallel=<processes>] [--generator=<generator>]
  [--extensions=<extensions>] [--ignore=<patterns>] [--ignore-annotations]
  [--stdin-path=<stdinPath>] [--file-list=<fileList>] [--filter=<filter>] <file> - ...

 -     Check STDIN instead of local files and directories
 -n    Do not print warnings (shortcut for --warning-severity=0)
 -w    Print both warnings and errors (this is the default)
 -l    Local directory only, no recursion
 -s    Show sniff codes in all reports
 -a    Run interactively
 -e    Explain a standard by showing the sniffs it includes
 -p    Show progress of the run
 -q    Quiet mode; disables progress and verbose output
 -m    Stop error messages from being recorded
       (saves a lot of memory, but stops many reports from being used)
 -v    Print processed files
 -vv   Print ruleset and token output
 -vvv  Print sniff processing information
 -i    Show a list of installed coding standards
 -d    Set the [key] php.ini value to [value] or [true] if value is omitted

 --help                Print this help message
 --version             Print version information
 --colors              Use colors in output
 --no-colors           Do not use colors in output (this is the default)
 --cache               Cache results between runs
 --no-cache            Do not cache results between runs (this is the default)
 --ignore-annotations  Ignore all phpcs: annotations in code comments

 <cacheFile>    Use a specific file for caching (uses a temporary file by default)
 <basepath>     A path to strip from the front of file paths inside reports
 <bootstrap>    A comma separated list of files to run before processing begins
 <encoding>     The encoding of the files being checked (default is utf-8)
 <extensions>   A comma separated list of file extensions to check
                The type of the file can be specified using: ext/type
                e.g., module/php,es/js
 <file>         One or more files and/or directories to check
 <fileList>     A file containing a list of files and/or directories to check (one per line)
 <filter>       Use either the "gitmodified" or "gitstaged" filter,
                or specify the path to a custom filter class
 <generator>    Use either the "HTML", "Markdown" or "Text" generator
                (forces documentation generation instead of checking)
 <patterns>     A comma separated list of patterns to ignore files and directories
 <processes>    How many files should be checked simultaneously (default is 1)
 <report>       Print either the "full", "xml", "checkstyle", "csv"
                "json", "junit", "emacs", "source", "summary", "diff"
                "svnblame", "gitblame", "hgblame" or "notifysend" report,
                or specify the path to a custom report class
                (the "full" report is printed by default)
 <reportFile>   Write the report to the specified file path
 <reportWidth>  How many columns wide screen reports should be printed
                or set to "auto" to use current screen width, where supported
 <severity>     The minimum severity required to display an error or warning
 <sniffs>       A comma separated list of sniff codes to include or exclude from checking
                (all sniffs must be part of the specified standard)
 <standard>     The name or path of the coding standard to use
 <stdinPath>    If processing STDIN, the file path that STDIN will be processed as
 <tabWidth>     The number of spaces each tab represents

もっと詳しく知りたい

phpcsでPSR-2コーディング規約チェックを行う


Amazonでおトクに買い物する方法
AmazonチャージでポイントGET


Amazonは買いもの前にAmazonギフト券をチャージしてポイントをゲットしないと損!

こちらもおススメ

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です