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

(2011/10/19) 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 のソースをダウンロードします。

https://github.com/thomas-ernest/CodeIgniter-for-PHP_CodeSniffer/pull/9
の pull request を摘要します。

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

$ sudo cp -R CodeIgniter-for-PHP_CodeSniffer /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 (2 files in queue)
Processing blog.php [778 tokens in 111 lines]... DONE in < 1 second (35 errors, 0 warnings)
Processing welcome.php [80 tokens in 27 lines]... DONE in < 1 second (6 errors, 0 warnings)

FILE: ...home/kenji/workspace/blog/application/controllers/blog.php
--------------------------------------------------------------------------------
FOUND 35 ERROR(S) AFFECTING 29 LINE(S)
--------------------------------------------------------------------------------
   1 | ERROR | End of line character is invalid; expected "\n" but found "\r\n"
   1 | ERROR | Missing file doc comment
   9 | ERROR | Missing class doc comment
  11 | ERROR | PHP5 style constructors are not allowed; use "Blog" instead
  11 | ERROR | Missing function doc comment
  13 | ERROR | PHP5 style calls to parent constructors are not allowed. Please
     |       | use "parent::CI_Controller" instead.
  19 | ERROR | You must use "/**" style comments for a function comment
  22 | ERROR | Single-quoted strings should be used unless it contains
     |       | variables, special chars like \n or single quotes.
  25 | ERROR | No space found after comma in function call
  28 | ERROR | You must use "/**" style comments for a function comment
  31 | ERROR | Single-quoted strings should be used unless it contains
     |       | variables, special chars like \n or single quotes.
  32 | ERROR | No space found after comma in function call
  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.
  51 | 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
     |       | "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.
  59 | ERROR | "==" and "!=" are prohibited; use "===" instead of "==".
  62 | ERROR | No space found after comma in function call
  72 | ERROR | No space found after comma in function call
  72 | ERROR | No space found after comma in function call
  72 | 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.
  81 | ERROR | You must use "/**" style comments for a function comment
  84 | ERROR | Single-quoted strings should be used unless it contains
     |       | variables, special chars like \n or single quotes.
  93 | ERROR | "==" and "!=" are prohibited; use "===" instead of "==".
  96 | ERROR | No space found after comma in function call
 103 | ERROR | No space found after comma in function call
 107 | 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".
 110 | ERROR | Multi lines comments are not allowed; use "// Comment" DocBlock
     |       | comments instead
--------------------------------------------------------------------------------


FILE: ...e/kenji/workspace/blog/application/controllers/welcome.php
--------------------------------------------------------------------------------
FOUND 6 ERROR(S) AFFECTING 6 LINE(S)
--------------------------------------------------------------------------------
  1 | ERROR | Missing file doc comment
  3 | ERROR | Missing class doc comment
 18 | ERROR | There must be exactly one blank line before the tags in function
    |       | comment
 19 | ERROR | Missing @return tag in function comment
 23 | ERROR | No comment block marks the end of file instead of the closing PHP
    |       | tag. Please add a comment block containing only "Location:
    |       | ./controllers/welcome.php".
 26 | ERROR | Multi lines comments are not allowed; use "// Comment" DocBlock
    |       | comments instead
--------------------------------------------------------------------------------

Time: 1 second, Memory: 4.00Mb

この CodeIgniter-for-PHP_CodeSniffer ですが、規約が古いようです。

PHP5 style constructors are not allowed; use "Blog" instead