Code Quality Hacks in PHP

Here's a few brief wins you can make to make you more productive in your environment:


Composer is PHP's npm or gem and while its best practice to keep your composer dependencies locked to a composer.json (e.g package.json) there are a few packages that you will want to install globally that help you out regardless of the project you are working in.

Keep in mind though, if you are on a project that has a CI build (which it should) then you need to save dev dependencies so the build can run them too.


For starters, you need a version of PHP on your machine and you'll need to do this:

php -r "copy('', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

More on installation here:

Next you need to make sure composer is in your path:

echo $PATH

Global requires will be placed into the ~/.composer directory in your home folder and to make those binaries available in your bash envionrment simply add this to your .bashrc or .bash_profile:

export PATH="$PATH:$HOME/.composer/vendor/bin"

Remember to source .bash_profile or open a new terminal session.


Once you have composer setup and your PATH is set correctly, you are going to want these packages:

$ composer global require phpunit/phpunit
$ composer global require phpdocumentor/phpdocumentor
$ composer global require sebastian/phpcpd
$ composer global require phploc/phploc
$ composer global require phpmd/phpmd
$ composer global require squizlabs/php_codesniffer

Here's what they enable:

  • $ phpunit - to run the test suite for your project.
  • $ phpcpd path/to/folder - Tells you when code is copy-pasta.
  • $ phploc path/to/folder - prints out awesome metrics about the size of your project.
  • $ phpmd - is PHP's JMD which will run static analysis on your code. More here.
  • $ phpcs - to run code style checks for your project.
  • $ phpcbf - will automatically fix code-style issues

Analyzing your Code

Its best to have these tools report automatically in your build so it enforces the best possible code quality without obstructing developers as they write code.

For kicks, lets run a few of these commands on a Laravel app in a private repo I'm working on:


Red, green, refactor.


Keeping it DRY.


The LoC report is pretty cool. Good for supplementing tech-debt repayment reports to the business. Also good to look at the dependencies section to see if you are pulling in too much global state or abusing static method/attributes.


Developers run in fear when the build forces you to adhere to code style. Its important thought; having this consistency makes the team more productive and peer code review's can focus on the actual code rather than formatting.


Save a shit ton of time fixing phpcs issues with phpcbf. Its pretty safe, particularly if you are running codestyle against a well known standard like PSR-2.