PHPコーディングスタイルチェックツール

PHP_CodeSniffer

PHP_CodeSnifferとはコードのレイアウトを自動的に修正してくれるツール、コマンドライン上で実行することもあるが、エディタのプラグインとして動作させることもできる。

インストール方法

wgetでダウンロード

$ wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
$ wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

ダウンロードしたファイルのテスト

$ php phpcs.phar -h
$ php phpcbf.phar -h

パスが通っている場所へインストール

$ sudo mv phpcs.phar /usr/local/bin/phpcs
$ sudo mv phpcbf.phar /usr/local/bin/phpcbf

使い方

テスト元PHPファイル test.php
中身はこんな感じでインデントとスペースを適当に記述

<?php
/**
 * test.php
 */

class
{
function hoge()
{
    return "";
}

if( !empty(  hoge()))
{
    echo "ok\n";
}

    echo 'hoge';
    echo "abc";
    }

修正が必要なところの表示

$ phpcs -sw --standard=PSR2 test.php

FILE: /Users/xxxxxxxx/test.php
------------------------------------------------------------------------------------------------------------------------------
FOUND 14 ERRORS AFFECTING 9 LINES
------------------------------------------------------------------------------------------------------------------------------
  8 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 0 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
  9 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0 (Generic.WhiteSpace.ScopeIndent.Incorrect)
 10 | ERROR | [x] Line indented incorrectly; expected at least 8 spaces, found 4 (Generic.WhiteSpace.ScopeIndent.Incorrect)
 11 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 0 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
 13 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 0 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
 13 | ERROR | [x] Expected 1 space(s) after IF keyword; 0 found (Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword)
 13 | ERROR | [x] Expected 0 spaces after opening bracket; 1 found
    |       |     (PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace)
 13 | ERROR | [x] Space after opening parenthesis of function call prohibited
    |       |     (PSR2.Methods.FunctionCallSignature.SpaceAfterOpenBracket)
 13 | ERROR | [x] Expected 1 space(s) after closing parenthesis; found newline
    |       |     (Squiz.ControlStructures.ControlSignature.SpaceAfterCloseParenthesis)
 14 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0 (Generic.WhiteSpace.ScopeIndent.Incorrect)
 15 | ERROR | [x] Line indented incorrectly; expected at least 8 spaces, found 4 (Generic.WhiteSpace.ScopeIndent.Incorrect)
 16 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 0 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
 20 | ERROR | [x] Line indented incorrectly; expected 0 spaces, found 4 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
 20 | ERROR | [x] Closing brace indented incorrectly; expected 0 spaces, found 4 (Squiz.WhiteSpace.ScopeClosingBrace.Indent)
------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 14 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------------

Time: 115ms; Memory: 5.25Mb

実際にコマンドで修正

$ phpcbf -w --standard=PSR2 test.php

PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE                                                  FIXED  REMAINING
----------------------------------------------------------------------
/Users/xxxxxxxxxxx/test.php                            14     0
----------------------------------------------------------------------
A TOTAL OF 14 ERRORS WERE FIXED IN 1 FILE
---------------------------------------------------------------------

修正後のソース

<?php
/**
 * test.php
 */

class
{
    function hoge()
    {
        return "";
    }

    if (!empty(hoge())) {
        echo "ok\n";
    }

    echo 'hoge';
    echo "abc";
}

修正後のソースにphpcsをもう一回かけてみると修正が必要ないため何事もなく終了する

$ phpcs -sw --standard=PSR2 test.php