69
72

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPのコーディングスタイルをPHP_CodeSnifferで修正する

Posted at

PHP_CodeSnifferはPHPのコーディングスタイルをチェックするツールとして有名ですが、現在開発中の2.0系ではチェックだけでなく修正もできるようです。

Fixing Errors Automatically · squizlabs/PHP_CodeSniffer Wiki

インストール

2.0系ではpharも提供されているので、 https://github.com/squizlabs/PHP_CodeSniffer/releases からphpcs.pharとphpcbf.pharをダウンロードしましょう。

$ curl -LO https://github.com/squizlabs/PHP_CodeSniffer/releases/download/2.0.0RC1/phpcs.phar

$ curl -LO https://github.com/squizlabs/PHP_CodeSniffer/releases/download/2.0.0RC1/phpcbf.phar

使い方

チェックはphpcs.pharで行います。

$ cat test.php
<?php
if($a==2)
    echo $a;
else
    echo '3';


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

FILE: C:\Users\ishida\src\test.php
--------------------------------------------------------------------------------

FOUND 7 ERRORS AFFECTING 4 LINES
--------------------------------------------------------------------------------

 1 | ERROR | [x] Expected 1 space after closing brace; 0 found
 2 | ERROR | [x] Expected 1 space after IF keyword; 0 found
 2 | ERROR | [x] Inline control structures are not allowed
 2 | ERROR | [x] Whitespace found at end of line
 4 | ERROR | [x] Expected 1 space after ELSE keyword; newline found
 4 | ERROR | [x] Inline control structures are not allowed
 5 | ERROR | [x] Expected 1 blank line at end of file; 2 found
--------------------------------------------------------------------------------

PHPCBF CAN FIX THE 7 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------


Time: 90ms; Memory: 2.5Mb

$ php phpcs.phar --standard=PSR2 --report=diff test.php
--- test.php
+++ PHP_CodeSniffer
@@ -1,6 +1,6 @@
 <?php
-if($a==2)
+if ($a==2) {
     echo $a;
-else
+} else {
     echo '3';
-
+}

修正はphpcbf.pharで行います。

$ php phpcbf.phar --standard=PSR2 test.php
Patched 1 file
Time: 182ms; Memory: 2.5Mb

$ cat test.php
<?php
if ($a==2) {
    echo $a;
} else {
    echo '3';
}

面白い機能としては、誰が問題を混入したかを調べるgitblameという(またはsvnblame、hgblame)レポート形式があります。

$ php phpcs.phar --report=gitblame src

PHP CODE SNIFFER GIT BLAME SUMMARY
--------------------------------------------------------------------------------

AUTHOR                                              (Author %) (Overall %) COUNT

--------------------------------------------------------------------------------

ISHIDA Akio                                            (16.18)       (100)    11

--------------------------------------------------------------------------------

A TOTAL OF 11 SNIFF VIOLATIONS WERE COMMITTED BY 1 AUTHOR
--------------------------------------------------------------------------------


Time: 164ms; Memory: 2.75Mb

はい。本当に申し訳ありませんでした!

なお似たようなことを行うツールとして、PHP Coding Standards Fixerもあります。

69
72
0

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
69
72

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?