Sunday, July 24, 2011

Quality Assurance Tools for PHP

1. PHPUnit
No need to introduce this one, i believe. And it can be integrated into the  continuous integration tool 

I don't see this one is very valuable.

3. phpcpd: PHP Copy-Paste-Detector (http://github/sebastianbergmann/phpcpd)
This one is interesting. Let's have a look at it.
to install:
sudo pear channel-discover;
sudo pear install phpunit/phpcpd;

to check copy & paste code:
phpcpd /var/www/project/application

And the result is like below:
phpcpd 1.3.2 by Sebastian Bergmann.

Found 1 exact clones with 10 duplicated lines in 2 files:

  - modules/user/login.php:28-38

0.03% duplicated lines out of 35460 total lines of code.

Time: 2 seconds, Memory: 19.50Mb

Just remind that its detection doesn't always make sense. As a tool, it can only help you to find possible copy & paste codes. It finally depends on you, the developer, to judge if the codes make sense or not.

4. phpdcd: PHP Dead Code Detector (http://github/sebastianbergmann/phpdcd)
Many developers are very cautious when it comes to delete. In fact, usually they would not like to delete anything even the function/method is not used anymore. The good side of not deleting is you can have a piece of mind, cause you know you never accidently delete something. The bad side of it is the system expands rapidly and is filled with lots of dead code.

This tool can help you detect dead code. BUT, have to warn you again that you should not rely on its detection result.  It cannot detect the methods which are called implicitly/dynamically, which is a practice that should be avoid as much as possible. Say for example:
function test(){};
$fn = 'test';
If your function is called in this way, phpdcd cannot correctly recognize it. However, you can still use the tool to help you find dead code. Just ensure you confirm the code is really dead before you delete it.

To install:
sudo pear channel-discover;
sudo pear channel-discover
sudo pear install phpunit/phpdcd-beta

To use:
phpdcd /var/www/project/application

And you will get a list of functions that are considered 'dead'

5. PHP_Depend: pdepend (
6. PHP Mess Detector (phpmd)
7. PHP_CodeSniffer (phpcs)
8. PHP_CodeBrowswer (phpcb)

These tools can be integrated into the continuous integration tool

9. phpUnderControl (
This is the most wildly used continuous integration tool in PHP world.

Hudson is another solution to continuous integration.  phpUnderControl is based on CruiseControl, however, CruiseControl is outdated and Hudson is more robust and easier to handle.  Hudson is now renamed to Jenkins.

Installing Jenkins on ubuntu is easy:
wget -q -O - | sudo apt-key add -
sudo sh -c "echo 'deb binary/' > /etc/apt/sources.list.d/jenkins.list"
sudo apt-get update
sudo apt-get install jenkins

After the installation is done, you can try jenkins locally: http://localhost:8080. It provides a very nice web interface for you to manage your projects. You can go to " Manage Jenkins" -> "Manage Plugins" to install plugins you need.

Now you can read
to learn more about how to integrate PHP project with Jenkins

No comments: