Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The plugin enables analysis of PHP projects within Sonarwith SonarQube.

It relies on well-known external tools: PHPUnit, PHP Depend, PHPMD and PHP_CodeSniffer.

Installation

  1. Install the PHP Environment
  2. Install the Sonar PHP plugin through the Update Center or download it into the SONAR_HOME/extensions/plugins directory
  3. Restart the Sonar server

Usage

Run a Sonar Analysis with the Sonar Runner (Recommended Way)

To launch a Sonar Pure PHP files and mixed HTML / PHP files can be analyzed.

Include Page
Include - Plugin Installation
Include - Plugin Installation

Usage

Run an Analysis with the SonarQube Runner (Recommended method)

To run an analysis of your PHP project, use the Sonar SonarQube Runner.

Sample projects are A sample project is available on GitHub that can be browsed or downloaded: /projects/languages/php/php-sonar-runner.

...

You can also have a look at the SonarQube Runner documentation page to define a multi-module PHP project.

Run an Analysis with the other Analyzers

Maven and Ant can also be used to launch analysis on PHP projects.

Advanced Settings

Disabling or enabling external tools on demand

It is possible to disable each external tool. By default, all the external tools are enabled.

Code Block
languagebash
langxml
sonar.phpPmd.skip=true
sonar.phpCodesniffer.skip=true
sonar.phpDepend.skip=true    # Deactiving the PHP Depend plugin is highly discouraged since all the basic metrics rely on it.
sonar.phpUnit.skip=true
sonar.phpUnit.coverage.skip=true

Configuring PHPUnit to be run by Sonar

By default, Sonar runs PHPUnit. To configure the execution of PHPUnit, you can set the following properties:

Key

Default value

Description

sonar.phpUnit.configuration Read configuration from XML file. Recommended way to configure the tests that should be launched.

sonar.phpUnit.ignore.configuration

 false

If true, PHPUnit will ignore any phpunit.xml file for launching the unit tests.
If false, PHPUnit will use any existing phpunit.xml file in the running directory.

sonar.phpUnit.mainTestClass

 

The project main test file including the relative path, ie: "/source/tests/AllTests.php". If not present, PHPUnit will look for phpunit.xml file in the test directory.

sonar.phpUnit.filter

 

Ignore the unit tests files matching this pattern.

sonar.phpUnit.bootstrap

 

Use this bootsrap file to initialize the unit tests.

sonar.phpUnit.analyze.test.directory

 true

If true, Sonar PHP will append test directory to PHPUnit. This will make PHPUnit look for test cases inside this directory. If several directories are defined as test directories, a phpunitRANDOM.xml file will be generated and passed to phpunit --configuration=. This generated file will contain all files inside the test directories.

sonar.phpUnit.group Only runs tests from the specified group(s).
sonar.phpUnit.loader To specify which TestSuiteLoader implementation to use.

Reusing existing reports

To reuse existing reports from PHP Depend, PHPUnit, etc.:

  1. Set the property 'sonar.<external_tool>.analyzeOnly' to 'true'
  2. Set the 'sonar.<external_tool>.reportFileName' to the name of the report
  3. Set the 'sonar.<external_tool>.reportFileRelativePath' to the path where the report has been placed
Code Block
languagebash
langxml
#PHPMD
sonar.phpPmd.analyzeOnly=true
sonar.phpPmd.reportFileName=myPmdReport.xml 
#default is logs; parent is $PROJECT_HOME/.sonar/target when the analysis is triggered with the Sonar Runner, $PROJECT_HOME/target when triggered with Maven
sonar.phpPmd.reportFileRelativePath=<relative_path_from_parent_to_xml_report>

 
#PHP_CodeSniffer
sonar.phpCodesniffer.analyzeOnly=true
sonar.phpCodesniffer.reportFileName=myCodeSnifferReport.xml    #default is codesniffer.xml
#default is logs; parent is $PROJECT_HOME/.sonar/target when the analysis is triggered with the Sonar Runner, $PROJECT_HOME/target when triggered with Maven
sonar.phpCodesniffer.reportFileRelativePath=<relative_path_from_parent_to_xml_report>
 
#PHP Depend
sonar.phpDepend.analyzeOnly=true
sonar.phpDepend.reportFileName=myPhpDependReport.xml    #default is pdepend.xml
#default is logs; parent is $PROJECT_HOME/.sonar/target when the analysis is triggered with the Sonar Runner, $PROJECT_HOME/target when triggered with Maven
sonar.phpDepend.reportFileRelativePath=<relative_path_from_parent_to_xml_report>
sonar.phpDepend.reportType=summary-xml    #Type of report generated by PHP Depend. Valid values: summary-xml, phpunit-xml (default value, deprecated)
 
#PHPUnit
sonar.phpUnit.analyzeOnly=true
sonar.phpUnit.reportFileName=myPhpUnitReport.xml    #default is phpunit.xml
#default is logs; parent is $PROJECT_HOME/.sonar/target when the analysis is triggered with the Sonar Runner, $PROJECT_HOME/target when triggered with Maven
sonar.phpUnit.reportFileRelativePath=<relative_path_from_parent_to_xml_report>

sonar.phpUnit.coverage.analyzeOnly=true
sonar.phpUnit.coverageReportFile=myCoverageReport.xml    #default is phpunit.coverage.xml
#Same path as sonar.phpUnit.reportFileRelativePath
Info
titleNote

Note that the reports should be generated with the recommended version of each tool. Otherwise you may face some incompatibility issues.

FAQ and Troubleshooting

Extending Coding Rules

See the tutorial to extend coding rules with PHP_CodeSniffer and/or PHPMD.

Roadmap

  1. PHP 1.2 - short-term activity (see the JIRA open tickets)
    1. Improve the PHP CodeSniffer rule repository (adding missing parameters, descriptions, ...)
    2. Work on the "Sonar Way", PEAR and Zend profiles (<= for PHP gurus! (wink) )
  2. PHP 2.0 - mid-term activity
    1. Handle multiple files with the same name
    2. Consider root folders as "Projects"
    3. Non structured PHP files
    4. If it turns out that those tickets are technically difficult/long to implement, then they can be postponed

Change Log

JIRA Issues
anonymoustrue
titleRelease 1.1
height110
renderModestatic
width900
columnstype;key;summary;priority
urlhttp://jira.codehaus.org/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?jqlQuery=project+%3D+SONARPLUGINS+AND+fixVersion+%3D+"PHP-1.1"+AND+component+%3D+PHP+AND+status+%3D+Closed+ORDER+BY+priority+DESC&tempMax=1000

 

JIRA Issues
anonymoustrue
titleRelease 1.0
height110
renderModestatic
width900
columnstype;key;summary;priority
urlhttp://jira.codehaus.org/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?jqlQuery=project+%3D+SONARPLUGINS+AND+fixVersion+%3D+"PHP-1.0"+AND+component+%3D+PHP+AND+status+%3D+Closed+ORDER+BY+priority+DESC&tempMax=1000

 

JIRA Issues
anonymoustrue
titleRelease 0.6
height110
renderModestatic
width900
columnstype;key;summary;priority
urlhttp://jira.codehaus.org/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?jqlQuery=project+%3D+SONARPLUGINS+AND+fixVersion+%3D+17853+AND+component+%3D+PHP+AND+status+%3D+Closed+ORDER+BY+priority+DESC&tempMax=1000

 

JIRA Issues
anonymoustrue
titleRelease 0.5
height110
renderModestatic
width900
columnstype;key;summary;priority
urlhttp://jira.codehaus.org/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?fixfor=17298&pid=11911&sorter/field=issuekey&sorter/order=DESC&tempMax=1000

 

JIRA Issues
anonymoustrue
titleRelease 0.4
height110
renderModestatic
width900
columnstype;key;summary;priority
urlhttp://jira.codehaus.org/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?fixfor=16696&pid=11911&sorter/field=issuekey&sorter/order=DESC&tempMax=1000

 

JIRA Issues
anonymoustrue
titleRelease 0.3
height110
renderModestatic
width900
columnstype;key;summary;priority
urlhttp://jira.codehaus.org/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?fixfor=16522&pid=11911&sorter/field=issuekey&sorter/order=DESC&tempMax=1000

 

...

Unit Tests and Code Coverage

To display unit test execution and code coverage data:

  1. Prior to the SonarQube analysis, execute your unit tests and generate PHPUnit (execution + code coverage) XML reports.
  2. Import these reports while running the SonarQube analysis by setting the:
    •  sonar.php.coverage.reportPath property to the path to the PHPUnit code coverage report file. The path may be either absolute or relative to the project base directory.
    •  sonar.php.tests.reportPath property to the path to the PHPUnit unit test execution report file. The path may be either absolute or relative to the project base directory.

A sample project is available on GitHub that can be browsed or downloaded/projects/languages/php/php-sonar-runner-unit-tests.

Metrics

See Metrics documentation page.

Release Notes

Version 2.0

The PHP plugin does not rely anymore on external tools.