JournalWebTake 057

Sonar For PHP Code Coverage

By Xminds Editorial2 min read

Sonar is a 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 extended 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>

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 is 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 the build succeeds, the web is updated with the new build reports.

  1. Take06309 Nov 2012· 2 min read

    Visual.ly makes everyone a designer – The infographic creation tool

    Infographics are becoming popular each day because of their ability to make the readers get interested to read even the most boring statistics. Though the concept of infographics…

    Xminds Editorial

  2. Take06201 Nov 2012· 3 min read

    Post Penguin SEO – Don’t hunt for links, instead plant and grow them naturally!

    It has been a while since we are dealing with the Google panda and penguin update and the minor refresh after that. The Internet is abuzz with news that this is probably the end…

    Xminds Editorial

  3. Take04723 Nov 2011· 3 min read

    From Search Engine Inclusion to Search Engine Optimization

    In the late 1990’s & very early 2000’s terms like Search Engine Inclusion & Search Engine Submission gained parlance. Search engines had Submission forms, to which Webmasters…

    Xminds Editorial

What we build in this territory

Web Platforms & E-commerce →
Start a project