LoginSignup
51
55

More than 5 years have passed since last update.

PHPコードをコマンドで自動整形! Condig Standards Fixer と PHP_CodeSniffer

Last updated at Posted at 2014-10-17

The PHP Coding Standards Fixer

PSR-1 and PSR-2 の形やオプションでコーディング規約に整形してくれるコマンド

The PHP Coding Standards Fixer for PSR-1 and PSR-2

インストール

以下からダウンロードして、chmod。で終わり。
http://get.sensiolabs.org/php-cs-fixer.phar

以下コピペでOK

sudo wget http://get.sensiolabs.org/php-cs-fixer.phar -O /usr/local/bin/php-cs-fixer
sudo chmod a+x /usr/local/bin/php-cs-fixer

使い方

ディレクトリごとか、ファイルの場合

php php-cs-fixer.phar fix /path/to/dir
php php-cs-fixer.phar fix /path/to/file

規約を指定して実行

php php-cs-fixer.phar fix /path/to/project --level=psr0
php php-cs-fixer.phar fix /path/to/project --level=psr1
php php-cs-fixer.phar fix /path/to/project --level=psr2
php php-cs-fixer.phar fix /path/to/project --level=symfony

エディター用プラグイン

  • Vim
  • Sublime Text
  • NetBeans
  • PhpStorm

参考

規約違反を発見! PHP_CodeSniffer

PHP_CodeSnifferは、古いです。

インストールとチェック

% pear install PHP_CodeSniffer

使い方

どんなコーディング規約があるかチェック

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

ディレクトリ指定して、実行

% phpcs /Users/beginner/spaghetti/code/ --standard=PSR2
FILE: /Users/beginer/spaghetti/code/index.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AND 1 WARNING(S) AFFECTING 5 LINE(S)
--------------------------------------------------------------------------------
  1 | WARNING | A file should declare new symbols (classes, functions,
    |         | constants, etc.) and cause no other side effects, or it should
    |         | execute logic with side effects, but should not do both. The
    |         | first symbol is defined on line 11 and the first side effect is
    |         | on line 7.
 16 | ERROR   | Expected 1 space after comma in function call; 5 found
 18 | ERROR   | Expected 1 space after comma in function call; 8 found
 19 | ERROR   | Expected 1 space after comma in function call; 6 found
 20 | ERROR   | Expected 1 space after comma in function call; 5 found
--------------------------------------------------------------------------------

参考

PSRの規約でコーディングをするために必要な調査 : 実験ぶろぐ(仮)試供品
http://needtec.exblog.jp/19108536

51
55
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
51
55