Atomでphpmd+Atom-beautify(php-cs-fixer)の静的解析とコード整形を簡単実行
ソース開発の最後に必ずあるソースレビュー。ソースレビューの時はできる限り処理が正しいか、性能はでるかなど本質的なレビューをしたい。使用していない変数とか、ネスト深すぎるとか、インデントが、、、とかどうでもいいコメントはできる限りしたくない!レビュー項目に残すのも嫌だし、直ったら確認するもの嫌!
そういう方にオススメしたいプラグインが、phpmdとAtom-beautify(php-cs-fixer)です。
というわけでAtomで簡単にPHPの静的解析とコード整形を実行できるパッケージのインストール手順を紹介します。
目次
Atomにphpmd+php-cs-fixer+Atom-beautifyをインストールする
前提条件
コマンドプロンプトよりパッケージをインストールする
C:\Users\user>composer global require phpmd/phpmd=* Changed current directory to C:/Users/<ユーザ名>/AppData/Roaming/Composer ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) - Installing symfony/filesystem (v3.1.2) Downloading: 100% - Installing symfony/dependency-injection (v3.1.2) Downloading: 100% - Installing symfony/config (v3.1.2) Downloading: 100% - Installing pdepend/pdepend (2.2.4) Downloading: 100% - Installing phpmd/phpmd (2.4.3) Downloading: 100%
・コマンドプロンプトよりphp-cs-fixerをインストールをする。
C:\Users\user>>composer global require friendsofphp/php-cs-fixer Changed current directory to C:/Users/morik/AppData/Roaming/Composer Using version ^1.11 for friendsofphp/php-cs-fixer ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) - Installing sebastian/diff (1.4.1) Loading from cache - Installing symfony/stopwatch (v3.1.3) Loading from cache - Installing symfony/process (v3.1.3) Loading from cache - Installing symfony/finder (v3.1.3) Loading from cache - Installing symfony/event-dispatcher (v3.1.3) Loading from cache - Installing symfony/polyfill-mbstring (v1.2.0) Loading from cache - Installing symfony/console (v3.1.3) Loading from cache - Installing friendsofphp/php-cs-fixer (v1.11.6) Downloading: 100% symfony/event-dispatcher suggests installing symfony/http-kernel () symfony/console suggests installing psr/log (For using the console logger) Writing lock file Generating autoload files
・phpmd,php-cs-fixerのバージョンを確認する。
C:\Users\user>phpmd --version PHPMD 2.4.3 C:\Users\user>php-cs-fixer --version PHP CS Fixer version 1.11.6 by Fabien Potencier
・atomにlinter、linter-phpmdなどをインストールする。
パッケージ名が分かっているのでコマンドプロンプトからインストールするのが楽です。
C:\Users\user>apm install linter Installing linter to C:\Users\<ユーザ名>\.atom\packages done C:\Users\user>apm install linter-phpmd Installing linter-phpmd to C:\Users\<ユーザ名>\.atom\packages done C:\Users\user>apm install php-cs-fixer Installing php-cs-fixer to C:\Users\<ユーザ名>\.atom\packages done C:\Users\user>apm install atom-beautify Installing atom-beautify to C:\Users\<ユーザ名>\.atom\packages done
※ATOMから直接インストールする場合はファイル⇒環境設定⇒インストール⇒search packagesの入力欄に以下を入力して以下4つをインストールする。
linter
linter-phpmd
php-cs-fixer
atom-beautify
パッケージの設定
上部メニューファイル⇒環境設定⇒パッケージのコミュニティパッケージにインストールしたパッケージ名が表示される。それぞれの設定より設定を行う。
・linter-phpmdの設定
PHPMD Executable Path:C:\Users\<ユーザ名>\AppData\Roaming\Composer\vendor\bin\phpmd
PHPMD Rulesets:cleancode,codesize,controversial,design,unusedcode
・php-cs-fixerの設定
PHP-CS-fixer executable path:C:\Users\<ユーザ名>\AppData\Roaming\Composer\vendor\bin\php-cs-fixer
Ctrl+Sで保存するとPHP-CSとPHP-MDがソースを解析してくれます。
以下画像のように該当箇所にフォーカスさせると原因が表示されます。
またCtrl+Alt+Bか、ソース上で右クリック⇒「Beautify editor contents」を実行することでソースの自動整形が実施されます。