Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away