From HaFrWiki
Jump to: navigation, search

Sonar [1] is an open source project hosted at Codehaus [2]. Sonar is well known in the Java Development, since it is a based on the old Maven plugins. Also Sonar no longer is dependent from maven as build tool.


Sonar is an open platform to manage code quality. Is looks at 7 axes of code quality:

  1. Architecture and design
  2. Coding rules
  4. Complexity
  5. Duplications
  6. Potential bugs
  7. Unit tests

Sonar is based on the ability of extending by plug-ins principle for covering new language, adding riles engines, computing advanced metrics. In summary:

  • More than 50 plug-ing [3] are already available.
  • It not only covers Java anymore but also PHP and much more languages (C++, C#, Cobol, Delphi, Drools, Flex, JavaScript, Natural, PHP, Visual Basic, and more...).

Sonar is a web-based application. Rules, alerts, thresholds, exclusions settings cab all be configured online. It is:

  • Database storage of (previous) results
  • Historical measures can be compared


The only pre-requisite is Java 1.5 or higher.


A description of the installation can be found on the Sonar Website [4]. However this installation focusses on the installation on Apple OS X running MAMP.

  1. Download the latest code from the SonarSource downloads page.
  2. Uncompress the downloaded file into a directory (i.e. /wwwroot/Sonar). This directory is named SONAR_WWW_HOME.
  3. Add the web-settings to $SONAR_WWW_HOME/conf/ Please note that you may have to change this in case you have already used these settings.
    • sonar.web.port: 8080
    • sonar.web.contect: /sonar
  4. Add the database settings to $SONAR_WWW_HOME/conf/
    • sonar.jdbc.user: sonar
    • sonar.jdbc.password: sonar
  5. In MySQL of MAMP do:
    • Create new schema sonar
    • Add user named 'sonar'
    • Add password for user sonar sonar.
  6. Very important, uncheck the settings 'Allow local access only in the MySQL-Database settings of MAMP.
  7. Execute the following script to start the server:
    • Go to the location where sonar is installed (SONAR_RUNNER_HOME/bin/<YOUR-OS>
    • $ ./ start
  8. Check the logger of Sonar:
    • $ tail -f -n 50 $SONAR_WWW_HOME/logs/sonar.log
  9. In case not errors can be found you can still check:
    • $ ./ status
  10. Now check sonar by going to the web-url:

Installing the client

The possible clients are:

  • Sonar runner
  • Maven
  • Ant
  • CI Engine.

If the previous steps are successful you can add the recommended client: Sonar Runner to this installation.

  1. Download the runner from Sonar-Runner download webpage.
  2. Uncompress the downloaded file into a directory named $SONAR_RUNNER_HOME in the next steps (i.e. /wwwroot/sonar/runner).
  3. Update the global settings in file $SONAR_RUNNER_HOME/conf/
    • Database connection (i.e. MySql, sonar.jdbc.url, sonar.jdbc.driver)
    • Server URL (i.e.
  4. Create a new SONAR_RUNNER_HOME environment variable set the $SONAR_RUNNER_HOME.
  5. Check the basic installation.
    • Open a new shell
    • Execute the command:
$ sonar-runner -h
usage: sonar-runner [options]
 -h,--help             Display help information
 -X,--debug            Produce execution debug output
 -D,--define <arg>     Define property

Adding PHP

For successful sonar-running a PHP projects:

  1. Downlaod the PHP jar from sonar
  2. Install in the extensions/plugins directory.
  3. Stop Sonar
  4. Start Sonar

However you need to have installed:

  • PHPUnit (PHP Unit testing)
    1. pear channel-discover
    2. pear channel-discover
    3. pear install phpunit/PHPUnit-3.5.5
  • XDebug (for code coverage computation) [5]. Since Apple OS X does not have apt-get utility you need an alternative such as home-brew, but installing with this tools is also no 100%.
    1. apt-get install make
    2. apt-get install php5-dev
    3. pecl install xdebug
    • The best and easy way is to follow Kubishkin's installation method, (Installing PHP Xdebug on Mac OS X 10.7 Lion in the See also).
  • pdepended [6].
    1. Download pdepend using pear (you may have to sudo the commands):
    2. pear channel-discover
    • pear install pdepend/PHP_Depend-beta
  • phpmd or PHP Mess Detector [7] installable using pear:
    1. pear channel-discover
    2. pear channel-discover
    3. pear install --alldeps phpmd/PHP_PMD
  • phpcs PHP Code Sniffer [8] using pear:
    1. pear install PHP_CodeSniffer-1.5.0RC1

Look at the for more information.

See also

top Apple OS X does not have the apt-get utility. An alternative is homebrew [9].

It is more easy to read the following instruction for installing debug

  • Kubishkin, Installing PHP Xdebug on Mac OS X 10.7 Lion.

PHP CodeSniffer

  • CodeSniffer, Technical Documentation of the PHP CodeSniffer Core Code and the CodeSniffer sniffs.



  1. Sonar Website on Codehaus.
  2., website. Codehaus is a collaborative environment for building opensource projects with a strong emphasis on modern languages, focussed on quality components that meet real world needs.
  3. Sonar plug-ins library and languages.
  4. Sonar Install + Configuration, Installing and Configuring Sonar Runner for different OS.
  5., The Xdebug extension helps you debugging your script by providing a lot of valuable debug information.
  6. pdepend, Website. Static analysis and software metrics computation (i.e. Cyclomatic Complexity, Coupling, Class interface Size).
  7. PHP Mess Detector, a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly and easy to configure frontend for the raw metrics measured by PHP Depend.
  8. PHP CS, PHP Code Sniffer webpage
  9. homebrew, The missing package manager for OS X