Memo

メモ > サーバ > 各論: プログラミング > PHP CS Fixer(コード整形)を使う

■PHP CS Fixer(コード整形)を使う
GitHub - FriendsOfPHP/PHP-CS-Fixer: A tool to automatically fix PHP coding standards issues https://github.com/FriendsOfPHP/PHP-CS-Fixer からファイルを入手する。具体的には http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar からpharファイルを取得し、「php-cs-fixer.phar」に名前を変更して保存する インストールはこれで完了。以下のように、バージョンが表示されることを確認する
$ php php-cs-fixer.phar --version PHP CS Fixer 2.10.0 Bowling Bear by Fabien Potencier and Dariusz Ruminski (513a376)
変換対象プログラム(テスト)を用意する これはPSR-2の「名前空間の宣言直後に空行を入れる」「ブレス({ 〜 })は改行する」という2点に違反している状態
$ vi test/sample.php
<?php namespace Sample; class Dummy { }
以下のコマンドでコード整形ができる
$ php php-cs-fixer.phar fix ./test You are running PHP CS Fixer with xdebug enabled. This has a major impact on runtime performance. If you need help while solving warnings, ask at https://gitter.im/PHP-CS-Fixer, we will help you! Loaded config default. 1) test\\sample.php Fixed all files in 0.449 seconds, 9.750 MB memory used
以下のコマンドで「PSR-2に整形する」と明示できる(デフォルトでPSR-2なので、この場合は結果は同じ)
$ php php-cs-fixer.phar --rules=@PSR2 fix ./test
PHP CS Fixerで快適PHPライフ - FLOG SPLASH http://fivestar.hatenablog.com/entry/2014/12/08/033345 PHP CS Fixer v2 でもっと快適PHPライフ - FLOG SPLASH http://fivestar.hatenablog.com/entry/2017/03/30/233744 コーディング規約自動調整ツールCodeSniffer2とphp-cs-fixer - Qiita https://qiita.com/dozo/items/ed4baa58a0131945ad6f GitHub - FriendsOfPHP/PHP-CS-Fixer: A tool to automatically fix PHP coding standards issues https://github.com/FriendsOfPHP/PHP-CS-Fixer

Advertisement