LoginSignup
9
10

More than 5 years have passed since last update.

CodeIgniter用のPHP_CodeSnifferをインストールする手順

Last updated at Posted at 2016-01-27

PHPのコーディングスタイルをチェックするツールとしてPHP_CodeSnifferがあるが、これのCodeIgniter用のもの「CodeIgniter-for-PHP_CodeSniffer」があることが分かり、早速インストールした(環境はCentOS)。その手順をまとめる。

※他のフレームワーク(例: FuelPHP)も同様の手順でインストール可

PHP_CodeSnifferのインストール

これはたくさん記事が存在するので、インストールコマンドだけ記述。
pearがインストールされている想定。

# pearでインストール
$ sudo pear install PHP_CodeSniffer

以上でインストール完了、確認。

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

上記の文言が表示されていたらOK。

CodeIgniter-for-PHP_CodeSnifferのインストール

# ディレクトリ移動
$ cd /tmp/

# ソースをダウンロード
$ git clone https://github.com/thomas-ernest/CodeIgniter-for-PHP_CodeSniffer.git

# CodeSniffer/Standardsディレクトリに移動
$ cd /path_to_CodeSniffer_directory/Standards/

# ディレクトリ作成
$ sudo mkdir CodeIgniter

# ソースをコピー
$ sudo cp -R /tmp/CodeIgniter-for-PHP_CodeSniffer/src/* CodeIgniter/

上記手順でインストール完了!
私の環境では、CodeSniffer/Standardsディレクトリは/usr/share/pear/PHP/CodeSniffer/Standards/だった。
あとは、正しくインストールできているか確認する。

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

一覧にCodeIgniterが表示されていればOK。

使い方

以下のコマンドをたたく。(ファイル名は適当に作成したHoge.php

$ phpcs --standard=CodeIgniter Hoge.php

FILE: /path_to_php_directory/Hoge.php
----------------------------------------------------------------------
FOUND 17 ERRORS AFFECTING 9 LINES
----------------------------------------------------------------------
  1 | ERROR | [x] End of line character is invalid; expected "\n" but
    |       |     found "\r\n"
  1 | ERROR | [ ] Filename "Hoge" doesn't match the name of the class
    |       |     that it contains "Hoge" in lower case. "Hoge" was
    |       |     expected.
  2 | ERROR | [ ] Missing file doc comment
  4 | ERROR | [ ] Missing class doc comment
  6 | ERROR | [ ] Tabs must be used to indent lines; spaces are not
    |       |     allowed for code indentation
  6 | ERROR | [ ] Missing function doc comment
  6 | ERROR | [x] Opening brace should be on a new line
  7 | ERROR | [ ] Tabs must be used to indent lines; spaces are not
    |       |     allowed for code indentation
  8 | ERROR | [ ] Tabs must be used to indent lines; spaces are not
    |       |     allowed for code indentation
 10 | ERROR | [ ] Tabs must be used to indent lines; spaces are not
    |       |     allowed for code indentation
 10 | ERROR | [ ] Missing function doc comment
 10 | ERROR | [x] Opening brace should be on a new line
 11 | ERROR | [ ] Tabs must be used to indent lines; spaces are not
    |       |     allowed for code indentation
 11 | ERROR | [ ] Single-quoted strings should be used unless it
    |       |     contains variables, special chars like \n or single
    |       |     quotes.
 12 | ERROR | [ ] Tabs must be used to indent lines; spaces are not
    |       |     allowed for code indentation
 12 | ERROR | [ ] No comment block marks the end of file instead of
    |       |     the closing PHP tag. Please add a comment block
    |       |     containing only "Location: ./controllers/Hoge.php".
 12 | ERROR | [ ] No comment block marks the end of file instead of
    |       |     the closing PHP tag. Please add a comment block
    |       |     containing only "End of file Hoge.php".
----------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

こんな感じで普通のCodeSnifferと同じような結果が表示される。
直し方は以下のコマンドをたたく。

$ phpcbf --standard=CodeIgniter Hoge.php

Changing into directory /path_to_php_directory
Processing Hoge.php [PHP => 74 tokens in 13 lines]... DONE in 23ms (4 fixable violations)
        => Fixing file: 0/4 violations remaining [made 4 passes]... DONE in 27ms
Patched 1 file
Time: 241ms; Memory: 2Mb

以上! Let's enjoy CodeIgniter life!

追記(2016/06/28)

pearのバージョンが古いとPHP_CodeSnifferのインストールでコケる可能性があるので、自分はこちらの記事を参考にpearをアップグレードするとうまくいった。(根本解決ではない、と記載があるのでその点に注意)

PEAR:but it is not a valid package archiveとなってしまう時の対処

9
10
1

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
9
10