LoginSignup
3
1

More than 1 year has passed since last update.

VSCodeでphp-cs-fixerのトラブル

Last updated at Posted at 2021-07-02

環境構築

VSCodeでPHPのコード自動成型のExtensionが色々あるが、いつもphp cs fixerを使っている。

このExtentionにはphp-cs-fixer.pharが同梱されているが公式ではパフォーマンスが低いので別途インストールすることを推奨している。

Install php-cs-fixer
1. this extension has included php-cs-fixer.phar for beginner, maybe performance lower.
2. if you want to install php-cs-fixer by yourself, see: php-cs-fixer Installation guide

なので、Installation guideに則ってインストールした。
Windows 10 on WSL2(Ubuntu-20.04)なので手順通りターミナルより下記を入力。

$ mkdir --parents tools/php-cs-fixer
$ composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer

次にSettingsより Extentions > PHPCSFixer > Executable Pathを変更する。
php-cs-fixer(自分のホームディレクトリ)/tools/php-cs-fixer/vendor/bin/php-cs-fixer
※Windowsで直接開発している場合は Extentions > PHPCSFixer > Executable Path Windows にパスを指定する。

これで、フォーマットができるようになるはず!

実行!あれっ!?

マニュアル通りにVSCode上でF1 -> php-cs-fixer: fix this file をするが無反応......
OUTPUT を見てみる。

[2021-07-02 07:21:18.812] [exthost] [error] [junstyle.php-cs-fixer] provider FAILED
[2021-07-02 07:21:18.812] [exthost] [error] undefined

ん~、なんか失敗している。

調べるとこんなIssueが junstyle.php-cs-fixer provider failed
ここに書かれている内容はデフォルトのphp-cs-fixer.pharのパスを解決する(${extensionPath}/php-cs-fixer.phar)話なので、ちょっと今回の件とは違うが、VSCode自体にDeveloper Toolsが存在することを知る。
image.png
VSCodeはElectronベースなコードエディタなので、ブラウザデバッグできるのだ。
F1 -> Developer: Toggle Developer Toolsを選択すると、右ペインにおなじみの画面が
image.png
この状態でもう一度F1 -> php-cs-fixer: fix this fileを実行するとエラーの詳細が表示された。
image.png

Configuration file .php_cs.dist is outdated, rename to .php-cs-fixer.dist.php.

設定ファイル名の形式が古いからリネームしろとのこと。
どうやらphp-cs-fixer:3.0でルールが変わったらしい。

どうするか迷ったが、チーム開発をしているので勝手にファイル名をリネームするとほかの人に迷惑がかかる。
今回はphp-cs-fixerを2系に落として対応とした。

$ composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer:^2.0.0

設定ファイルリネームの件はCHANGELOG.mdを見ても特に言及がないように見える......
ネットで調べてみてもあまり情報が載ってなさそうだったので、記事にしてみた。

2021/09/13追記

UPGRADE-v3.mdにしっかり載ってた。

3
1
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
3
1