In my last post on The 10 commandments of code reviews and clean code” I outlined the golden rules of submitting code for review.

Before we can begin writing our high quality code there are several tools you will need to install and configure first. Learning to use each of these tools effectively will give you power over your code and mean that you will never fail a code review again.

Installation instructions for each application are given for Linux (I am using Linux Mint). If you want to install for a different platform, instructions are given on the related sites.

Apache, MySQL and PHP.
Obviously if we’re writing PHP, we need the base language installed. This allows us to execute our scripts and also provides static code analysis (lint) functionality.

We also want to install MySQL and Apache for simple access to Jenkins, Sonar and for any websites you may want to build.

XDebug is used for debugging and reporting back information about your code. It can be used to generate stack traces and produce profiling information, helping you to spot memory leaks in your application and reduce its overall footprint. It will also report code coverage of your unit tests and is a requirement for PHPUnit.

Installing XDebug

Configuring XDebug

There are a lot of different configuration options for XDebug and these need to be loaded into the file /etc/php5/mods-available/xdebug.ini. At the very least you will want to set the following:

Note: If you are going to use XHProf for memory profiling, you will almost certainly want to leave the profiler permanently disabled or you will end up generating the same data in two different ways.

Memory profiling

For memory profiling output from XDebugs profiler, you will want to download and install KCachegrind if you are on Linux or QCachegrind if you are working on any other platform.

As an alternative to KCachegrind, you may wish to use XHProf. This offers a web based alternative to KCachegrind for fast analysis and profiling of PHP applications.

To install XHProf, execute the following in a terminal.

Unit testing for PHP.

PHP Codesniffer.
The PHP Codesniffer application. This application checks the style of your code and informs you where you deviate from the defined standard.

PHPLOC is an application for measuring the size and complexity of a project. It will report details such as the cyclomatic complexity of your code, the number of lines it contains and information about the structure.

Software analyser and metrics for PHP.

Note: PDepend requires the ImageMagick development libraries for a full installation.

PHP Mess Detector.
Looks for mess in your PHP code and helps you spot bugs and remove complexity. Note, this has a requirement on PDepend so make sure that is installed first.

PHP Copy Paste Detector.
Analyses your code and looks for code which is similar to other sections.

Doxygen is probably the most advanced code documentation tool on the planet. It is much more advanced than PHPDocumentor and can work across many languages. Not only does it generate your API documentation but will also generate class diagrams, call graphs and callee graphs.

Used to generate class diagrams, caller and callee diagrams within API documentation provided by Doxygen, KCachegrind and XHProf.

Jenkins CI.
Automated build environment and continuous integration platform.

Set up a host file entry for jenkins.

Configure apache

Create  a new file in /etc/apache2/sites-available called jenkins.conf and paste in the following contents:

Symlink this file to /etc/apache2/sites-enabled and restart apache.

Setup JENKINS_HOME as an environment variable:

That’s it. You can now browse to http://jenkins.local/ and start adding and configuring build jobs.
Screenshot from 2014-02-08 16:57:36

Code quality reporting environment.

Configure Sonar.

Create the MySQL Database for sonar

Set up a host file entry for sonar:

Configure apache

Create  a new file in /etc/apache2/sites-available called sonar.conf and paste in the following contents:

Symlink this file to /etc/apache2/sites-enabled and restart apache

Setup SONAR_HOME as an environment variable:

Configure Sonar as a service

Create a file at /etc/init.d/sonar with the following content:

Register Sonar for running at boot-time:

Start sonar

That’s it. You can now browse to http://sonar.local/.
Screenshot from 2014-02-08 16:56:59

Configuring sonar-runner

Sonar-runner is used for executing sonar analysis against non-maven projects. It is not a requirement to have this installed if you are wanting to build your projects with Maven (which I thoroughly recommend doing) but is useful to have.

To configure sonar-runner, edit the file /opt/sonar-runner/conf/ end set the configuration as follows, matching the values you set in the sonar configuration above.

Now we need to put sonar-runner into our PATH variable and set SONAR_RUNNER_HOME to our install location.

I personally use Maven for building PHP projects. For a start it integrates very nicely into Jenkins and Sonar. You can skip this step if you want to use another build environment.

Install and configure Maven

Of course no environment would be complete without a source control management system. You may already have Git set up, or you may wish to use Mecurial, SVN or even CVS. Whichever your choice, for the purposes of these tutorials, I shall be giving examples using Git.

Installing Git

For Git to work in the way I am going to show you, you need to install git, git-daemon-runner and runit. These latter two packages provide git-daemon as a service which automatically starts at boot-time.

By default git-daemon serves repositories from /var/cache/git and requires that a file git-daemon-export-ok is created in the .git base of all repositories you wish to export.

I personally hate that for working locally. I’m not serving them over the network (that’s what GitHub and Bitbucket are for), this is purely for internal CI.

To overcome this, edit the file /etc/sv/git-daemon/run and add the parameter –export-all to the script provided

Once you have made these changes you will probably want to link the sv command so that you can start / restart git-daemon using the system service scripts.

Restart git-daemon:

And finally…

That’s it, you’re all done. You should now have a fully configured PHP Environment from which you will be able to write nothing but the highest quality PHP code.

Because a lot of configuration entries have been made to /etc/profile you may have to log out and log back in again for them to take effect.

In my next post I will show you how to tie all of these tools together using Maven, load your repo into Jenkins as a new job and configure Jenkins to automatically load the results into Sonar on build success.

Leave a Comment