blog

Sonar For PHP Code Coverage

Sonar is quality management platform, checking code quality based on certain rules and custom rules used by development teams to manage source code quality. The main objective is to make code quality management accessible to everyone with minimal effort. As such, it covers the 7 axes of code quality.

Sonar is a web based code coverage report tool. Sonar provides reporting tools, code analyzers, defect hunting tools and timemachine as its core functionality. It also has a plugin mechanism to modify the code coverage functionality for other languages. Sonar provides built in support for Java projects which can be extented using sonar plugins for other languages like PHP, PL/SQL, C, C#, FLEX, COBOL and VB6.

Sonar covers the 7 axes of code quality

•Design and architecture
•Duplicated code
•Coding standards
•Unit tests
•Complex code
•Potential bugs
•Comments

Sonar can be used for one-off audits, but has been designed to support global continuous improvement strategy on code quality in a company and therefore can be used as a shared central repository for quality management.

We will have a quick go through on how to setup sonar for PHP code coverage.

Download latest version of sonar from http://www.sonarsource.org/downloads/

Starting sonar server [PATH_TO_SONAR]/bin/[OS]/sonar.sh console
e.g. /usr/local/sonar/linux-x86-32/sonar.sh start

Now the sonar has been started at http://localhost:9000 by default, config details of sonar can be updated
at sonar.properties inside conf.

For running php code coverage

Sonar is basically used for Java projects which can be extended for php using maven plugin. So download latest maven from http://maven.apache.org/download.htm

Now for adding project to sonar pom.xml file has to be created in the root folder of the project

<?xml version="1.0"?>
<project >
<modelVersion>1.0.0</modelVersion>
<groupId>Group_name</groupId>
<artifactId>name</artifactId>
<name>Project</name>
<version>1.0</version>
<packaging>pom</packaging>
<build>
<directory>build</directory>
<sourceDirectory>application</sourceDirectory>
<testSourceDirectory>tests</testSourceDirectory>
</build>
<properties>
<sonar.language>php</sonar.language>
</properties>
</project>

For running php code coverage it requires phpunit to be configured.
Then coverage report is created using phpunit.

e.g.

phpunit –coverage-clover build/logs/phpunit.coverage.xml –log-junit build/logs/phpunit.xml

According to the pom.xml file the reports are created inside build/logs folder.

Some of the additional plugins are required for maven to run for PHP.

1.Install PHP runtime with the corresponding xdebug extensions. Version 5.2.6 or newer is required.

2.Install PEAR. Version 1.8.0 or newer is required

3.Install PHPUnit version 3.5.5 (strictly)

4.Install phpcpd version 1.3.0 (strictly)

5.Install PHPDepend PHPMD version 0.2.5

6.Install PHP_CodeSniffer version 1.3.0

After running PHPUnit, it ready for sonar to run with maven from project root folder.

[PATH_TO_MAVEN]/bin/mvn sonar:sonar

with -e option it lists the trace errors. After Build is success the web is updated with the new build reports.

For more details ref : http://docs.codehaus.org/display/SONAR/PHP+PluginFor running php code coverage it requires phpunit to be configured.

Then coverage report is created using phpunit.

e.g. phpunit –coverage-clover build/logs/phpunit.coverage.xml –log-junit build/logs/phpunit.xml
According to the pom.xml file the reports are created inside build/logs folder.

Some of the additional plugins are required for maven to run for PHP.
1. Install PHP runtime with the corresponding xdebug extensions. Version 5.2.6 or newer is required.
2. Install PEAR. Version 1.8.0 or newer is required
3. Install PHPUnit version 3.5.5 (strictly)
4. Install phpcpd version 1.3.0 (strictly)
5. Install PHPDepend PHPMD version 0.2.5
6. Install PHP_CodeSniffer version 1.3.0

After running PHPUnit, it ready for sonar to run with maven from project root folder. [PATH_TO_MAVEN]/bin/mvn sonar:sonar with -e option it lists the trace errors. After Build is success the web is updated with the new build reports.

For more details ref : http://docs.codehaus.org/display/SONAR/PHP+Plugin

Leave a Reply