PHP_CodeSniffer で CodeIgniter のコードをチェックする

CodeIgniter-for-PHP_CodeSniffer が更新されましたので、新しい記事を書きました。

PHP_CodeSniffer のインストール

$ sudo pear install PHP_CodeSniffer

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

https://github.com/thomas-ernest/CodeIgniter-for-PHP_CodeSniffer
より CodeIgniter-for-PHP_CodeSniffer のソースをダウンロードします。

CodeSniffer の Standards フォルダに CodeIgniter というフォルダ名でコピーします。

$ cd CodeIgniter-for-PHP_CodeSniffer
$ sudo cp -Rv src /opt/lampp/lib/php/PHP/CodeSniffer/Standards/CodeIgniter

正しくインストールできたか確認します。

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

coding standards に「CodeIgniter」が表示されれば OK です。

使い方

$ phpcs -v --standard=CodeIgniter --encoding=utf-8 *.php
Registering sniffs in CodeIgniter standard... DONE (32 sniffs registered)
Creating file list... DONE (1 files in queue)
Processing blog.php [787 tokens in 113 lines]... DONE in 1 second (18 errors, 0 warnings)

FILE: ...home/kenji/workspace/blog/application/controllers/blog.php
--------------------------------------------------------------------------------
FOUND 18 ERROR(S) AFFECTING 17 LINE(S)
--------------------------------------------------------------------------------
   1 | ERROR | Missing file doc comment
   9 | ERROR | Missing class doc comment
  11 | ERROR | Missing function doc comment
  19 | ERROR | You must use "/**" style comments for a function comment
  28 | ERROR | You must use "/**" style comments for a function comment
  36 | ERROR | You must use "/**" style comments for a function comment
  36 | ERROR | Very short (i.e. less than 4 chars), non-word variables like
     |       | "id" should only be used as iterators in for() loops.
  38 | ERROR | Very short (i.e. less than 4 chars), non-word variables like
     |       | "id" should only be used as iterators in for() loops.
  39 | ERROR | Very short (i.e. less than 4 chars), non-word variables like
     |       | "id" should only be used as iterators in for() loops.
  40 | ERROR | Very short (i.e. less than 4 chars), non-word variables like
     |       | "id" should only be used as iterators in for() loops.
  41 | ERROR | Very short (i.e. less than 4 chars), non-word variables like
     |       | "id" should only be used as iterators in for() loops.
  53 | ERROR | Very short (i.e. less than 4 chars), non-word variables like
     |       | "row" should only be used as iterators in for() loops.
  56 | ERROR | Very short (i.e. less than 4 chars), non-word variables like
     |       | "row" should only be used as iterators in for() loops.
  62 | ERROR | No space found after comma in function call
  74 | ERROR | Very short (i.e. less than 4 chars), non-word variables like
     |       | "id" should only be used as iterators in for() loops.
  83 | ERROR | You must use "/**" style comments for a function comment
 109 | ERROR | No comment block marks the end of file instead of the closing
     |       | PHP tag. Please add a comment block containing only "Location:
     |       | ./controllers/blog.php".
 112 | ERROR | Multi lines comments are not allowed; use "// Comment" DocBlock
     |       | comments instead
--------------------------------------------------------------------------------

Time: 1 second, Memory: 4.00Mb