Phan Daemon mode 試用してみた
etsy/phan というPHPの静的解析ツールが最近話題です。
最新の0.9.2(PHP7.0の場合は0.8.4)で、 Phan Daemon Mode
という仕組みが搭載されました。
クラス依存情報を持つ必要があるため、これまでは解析に関わるvendorを含む全てのファイルを対象とした解析の実行しか行えませんでした。
これは、CIなどのプロセスにおいては有効ですが、エディタによる編集作業中にエラーを指示してくれないことになり、フィードバックが遅れるという問題があります。
- (Linux/Unix only) Add Experimental Phan Daemon mode (PR #563 for Issue #22), which allows phan to run in the background, and accept TCP requests to analyze single files. (The implementation currently requires the
pcntl
extension, which does not in Windows) Server usage:path/to/phan --daemonize-tcp-port 4846
(In the root directory of the project being analyzed) Client usage:path/to/phan_client --daemonize-tcp-port 4846 -l src/file1.php [ -l src/file2.php ]
新しい仕組みでは、バックグラウンドで常に全ファイルの解析を行ったものを保持して、エディタ等でファイル変更が生じた際にそのファイルだけを解析にかけることが出来るというものです。
実際に実行するのは簡単で、
# プロジェクト外にphanを置く $ git clone https://github.com/etsy/phan.git /opt/phan $ cd /opt/phan # PHP7.1の場合はmaster、PHP7.0の場合は0.8ブランチで $ git checkout -b 0.8 origin/0.8 $ composer install -o --no-dev $ cd /path/to/project $ /opt/phan/phan --daemonize-tcp-port 4846 --quick # `/.phan/config.php` を事前に置いておくの推奨 Warning: This daemon is slower when xdebug is installedListening for Phan analysis requests at tcp://127.0.0.1:4846 # 警告の通り、xdebug付きで実行すると遅くなるそうです。
これでデーモンプロセスが立ち上がるので、後はコマンドなりなんなりで
$ /opt/phan/phan_client -l src/TargetFile.php Phan error: UndefError: PhanUndeclaredClassMethod: Call to method get from undeclared class \Config in src/TargetFile.php on line 10 ...
のように単体ファイルで高速に解析が出来ます。
…とのことでしたが、xdebugをONにしているのが悪いのか、1ファイル解析するのに1分くらいかかったりしてました。
CPUとメモリもそれなりにムシャムシャするみたいなので、まだ安定運用は出来なさそうな雰囲気です。
クライアントをエディタに反映するプラグインも emacs用 と vimスニペット くらいしか出ていません。
今後は Microsoft Language Server Protocol に従った出力をしようぜ、などの提案がなされています。今後に期待ですね。