Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
{iframe:src=http://updateredirect.sonarsource.orgcom/plugins/php.html|width=700|height=250300|frameborder=0}
YourDocumentation browser does not support iframes.
{iframe}
Tip
titleLive example

If you want to see a live example of the capabilities of the PHP plugin, you can have a look at the analysis of the PHP CodeSniffer project on Nemo.

Description / Features

The plugin enables analysis of PHP projects within Sonar.

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 analysis of your PHP project, use the Sonar Runner.

Sample projects are available on GitHub that can be browsed or downloaded: /projects/languages/php.

Run a Sonar 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

 

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

has been moved to [http://redirect.sonarsource.com/plugins/php.html].
{iframe}