Setup vim with phan

Posted on Categories programming

In last post, we explained how to set up a PHP IDE with vim, we can do syntax checking, GOTO definition, and auto fix, but the syntax checking is just a basic check according to the PHP grammar, it does not consider the undefined variable, unused import and all the other extra check, fortunately, we have phan and static PHP analyzer that can do this for us, let’s see how can we set it up

Installing Phan

phan is a static analyzer for PHP, to install it , you can run composer global require phan/phan

notes: you may encounter the issue that phan need microsoft/tolerant-php-parser:0.0.20 but the lastest version is microsoft/tolerant-php-parser:0.0.22, so composer think 0.0.20 is too old, it vialate the minimun-stability rule, you have to install microsoft/tolerant-php-parser by typing the version alson, so the total command should be composer global require phan/phan microsoft/tolerant-php-parser:0.0.20

After installing phan, you will have two executables : ~/.composer/vendor/bin/phan and ~/.composer/vendor/bin/phan_client . the phan_client is used when phan is run with daemon mode. when phan is run daemon mode, phan_client can communicate with it and ask for specific file analysis results and display it to the user.

let phan analysis your project files

for phan to analyze your project PHP file and figure out the file/class/package relationships, you need to create an .phan/config.php file, in this file you will specify which files should be included when doing the analysis, for an example you can check here, but the minimum file can be :

return [
    'directory_list' => ['.'],

here we can telling phan to analysis all the files under the current directory

now to start the analysis, run the command :~/.composer/vendor/bin/phan

after phan finish analysing the project PHP files, you can start it in daemon mode and use phan_client to query the result

nohup ~/.composer/vendor/bin/phan --daemonize-tcp-host= &

~/.composer/vendor/bin/phan_client -l apps/files_checksum/lib/Controller/FilesChecksumApiController.php

~/.composer/vendor/bin/phan_client -l apps/files_checksum/lib/Controller/FilesChecksumApiController.php 
Phan error: NOOPError: PhanUnreferencedUseNormal: Possibly zero references to use statement for classlike/namespace IQueryBuilder (\OCP\DB\QueryBuilder\IQueryBuilder) in apps/files_checksum/lib/Controller/FilesChecksumApiController.php on line 12
Phan error: TypeError: PhanTypeArraySuspicious: Suspicious array access to $this->session of type \OCP\ISession in apps/files_checksum/lib/Controller/FilesChecksumApiController.php on line 53
Phan error: TypeError: PhanTypeMismatchPropertyProbablyReal: Assigning (tempnam(sys_get_temp_dir(), ('nextcloud_proccesss_output_temp_file-' . $this->userId)) as a field) of type array<string,fals .....

Setup vim

When using vim with phan, the process is the same

  • create an .phan/config.php file in your project directory
  • run phan in daemon mode (it will automatically start analyzing)
  • tell vim to use phan_client as a linter and tell it to use client mode
let g:ale_linters = {'php': ['php','langserver','phan']}
let g:ale_php_phan_executable='/root/.composer/vendor/bin/phan_client'
let g:ale_php_phan_use_client = 1 

now every time you open any PHP file with vim, vim will automatically run the phan_client on the current file to display the result, you can verify this by running ALEInfo

Leave a Reply

Your email address will not be published. Required fields are marked *