Getting phpmd to play nice in Composer
At the beginning of this month I joined the AOL Media Platform (AMP) team as a Senior Software Engineer. AMP is the engine behind sites like Engadget and TUAW, sites that get hit really hard whenever there is a big tech announcement like this week’s Apple event. Because of that it is extremely important that we have a high quality code base that performs really well. To that end, I have been working with the team to analyze our codebase and find ways that we can continue to improve it.
There is an entire suite of PHP QA Tools that you should check out (and even a recommended Jenkins configuration for PHP), but phpmd is the one that tripped me up today, so I’m going to focus on that.
[PHPMD] is a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly and easy to configure frontend for the raw metrics measured by PHP Depend.
Like all of the cool kids these days, phpmd supports composer. So, I added it to my composer.json like so.
"require-dev": {
"phploc/phploc": "*",
"phpmd/phpmd": "1.5.*",
"phpunit/phpunit": "3.7.*",
"sebastian/phpcpd": "*"
}
You can see several other developer tools installed here and I recommend taking the time to learn about each of them. Once they are installed they drop an executable into the vendor/bin directory. Unfortunately, I got a big ugly error message as soon as I ran a version check.
$ vendor/bin/phpmd -v
PHP Warning: require_once(PHP/Depend/Autoload.php): failed to open stream: No such file or directory in vendor/phpmd/phpmd/src/main/php/PHP/PMD/ParserFactory.php on line 48
I dug around a little bit to verify composer was pulling in PHP Depend (it was) and it was even in the PHP include path, but it just was not playing nice. Finally, I looked up PHP Depend and noticed that they only added composer support officially a few months ago. On a haunch, I added the latest version to my composer.json.
"require-dev": {
"pdepend/pdepend" : "1.1.1",
"phploc/phploc": "*",
"phpmd/phpmd": "1.5.*",
"phpunit/phpunit": "3.7.*",
"sebastian/phpcpd": "*"
}
And then after running a composer update I checked the version again.
$ ./composer.phar update
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Updating pdepend/pdepend (dev-master 820ceae => 1.1.1)
Checking out 6fcfb7666c95f341b42f02c94a47b3a51ed484e0
Writing lock file
Generating autoload files
$ vendor/bin/phpmd --version
PHPMD @package_version@ by Manuel Pichler
Boom. Yeah, I know, someone forgot to fix the package version in the message here, but everything works now so its all good.
Using the tool is a whole topic in and off itself, but I strongly recommend you either set up the PhpStorm integration for yourself or, even better, setup Jenkins so that your entire team has access to the graphs and pretty colors.