sublime text3 でPSR-2 formatを適用する

自分はsublime textは使ってないけども、調べる必要があったのでメモ。

  • fabpot/php-cs-fixer をインストール
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer
$ composer global require fabpot/php-cs-fixer
  • sublime text で Tools -> Build System -> New Build Systemを選択し以下の内容で、ファイル名php.sublime-buildで保存する。
{
    "path": "$HOME/.composer/vendor/bin",
    "shell_cmd": "php-cs-fixer fix '$file' --level=psr2"
}
  • sublime textでphpコードを書いて、cmd + bを押せばフォーマットが実行される

※もしcommand not found エラーが出た場合はshell_cmd部分を以下のようにすると動くかも

"shell_cmd": "php-cs-fixer fix \"$file\" --level=psr2"

以下参照元

https://gist.github.com/mikefrancis/63436f1cb94ec31ce31fgist.github.com