Over a million developers have joined DZone.

Installing Phan on OS X

Phan is a static analyzer for PHP 7. This post describes how to make it work with Homebrew, Composer, and AST.

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

I use Homebrew for my local PHP installation on OS X and am currently running PHP 7.0.0 RC8.

Phan is a static analyser for PHP 7 which was written by Rasmus and then rewritten by Andrew Morrison. As it benefits from PHP 7's abstract syntax tree it can find all kinds of subtle errors, so I wanted to install it locally to have a play with it.

I started by trying to install Phan into my global composer install so that it's available on my path. As it's not released on Packagist, I edited ~/.composer/composer.json and added the following to the top:

"repositories": [
            "type": "vcs",
            "url": "https://github.com/etsy/phan"

I could then install via the standard composer method:

$ composer global require etsy/phan:dev-master

This immediately failed as I don't have the Nikita's ast extension installed:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for etsy/phan dev-master -> satisfiable by etsy/phan[dev-master].
    - etsy/phan dev-master requires ext-ast * -> the requested PHP extension ast is missing from your system.

Which is necessary to install this extension:

$ git clone git@github.com:nikic/php-ast.git
$ cd php-ast
$ phpize
$ ./configure --enable-ast
$ make install

Now edit your php.ini file (/usr/local/etc/php/7.0/php.ini for Homebrew installations) and add extension=ast.so at the bottom.

Having installed the ast PHP 7 extension, Phan will now install:

$ composer global require etsy/phan:dev-master
Changed current directory to /Users/rob/.composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)         
  - Installing etsy/phan (dev-master 66dd303)
    Cloning 66dd303fc17aafe968fa5ff393b920ba5c222b6f

Writing lock file
Generating autoload files
Changelogs summary:

 - etsy/phan installed in version dev-master

Now, Phan works from my command line!

$ phan -h
Usage: /Users/rob/.composer/vendor/bin/phan [options] [files...]
  -f <filename>   A file containing a list of PHP files to be analyzed
  -q              Quick mode - doesn't recurse into all function calls
  -b              Check for potential PHP 5 -> PHP 7 BC issues
  -i              Ignore undeclared functions and classes
  -c              Comma-separated list of classes that require parent::__construct() to be called
  -m <mode>       Output mode: verbose, short, json, csv
  -o <filename>   Output filename
  -p              Show progress bar
  -t              Emit trace IDs on messages (for grouping error types)
  -s <filename>   Save state to the given file and read from it to speed up
                  future executions
  -r              Force a re-analysis of any files passed in even if they haven't
                  changed since the last analysis
  -h              This help

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

php,os x,homebrew

Published at DZone with permission of Rob Allen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}