| Live 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
- Install the PHP Environment
- Install the Sonar PHP plugin through the Update Center or download it into the SONAR_HOME/extensions/plugins directory
- 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.
Configuring PHPUnit to be run by Sonar
To configure the execution of PHPUnit, it is recommended to create a configuration file and set the path to this file with the sonar.phpUnit.configuration property.
If a configuration file is not used, the following property can be set to configure the execution of PHPUnit:
Key | Default value | Description |
|---|---|---|
sonar.phpUnit.ignore.configuration | false | If true, PHPUnit will ignore any phpunit.xml file for launching the unit tests. |
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.:
- Set the property 'sonar.<external_tool>.analyzeOnly' to 'true'
- Set the 'sonar.<external_tool>.reportFileName' to the name of the report
- Set the 'sonar.<external_tool>.reportFileRelativePath' to the path where the report has been placed
| Note 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
- PHP 1.2 - short-term activity (see the JIRA open tickets)
- Improve the PHP CodeSniffer rule repository (adding missing parameters, descriptions, ...)
- Work on the "Sonar Way", PEAR and Zend profiles (<= for PHP gurus!
)
- PHP 2.0 - mid-term activity
- Handle multiple files with the same name
- Consider root folders as "Projects"
- Non structured PHP files
- If it turns out that those tickets are technically difficult/long to implement, then they can be postponed
Change Log

