To understand how rules extension works in Sonar, please refer to the Extending Coding Rules documentation.
Extending php_codesniffer rules
PHP_CodeSniffer is built on an extensible architecture where you can define you own sniffs and thus add some detectors for violations. Out of the box phpcs contains more than 500 rules among hundreds of files. Unfortunatly, the violations associated to the sniffs are not described in a xml file.
So Sonar PHP Plugin team developped a parser that tried to read this violations and put them inside a rules.xml file which is the file format used to describe and extends rules inside sonar.
The drawback is that some sniffs are not detected and maybe missing from the rule repository. Moreover, some people can also develop their own sniffs and of course, these sniffs won't be available in Sonar PHP.
The good news is that since Sonar 2.3 a mechanism for extending rules is present and supported by Sonar PHP Plugin.
To do so, you just have to create your own rules.xml file following these format:
You can reference your custom rules from PHP CodeSniffer and/or PHPMD in SonarQube:
- Write a piece of XML to reference your custom coding rules. See formats below.
- Go to Settings > General Settings > PHP CodeSniffer or PHPMD
- Paste this piece of XML in PHP XXX custom rules
- Click on Save PHP XXX settings
- Restart your SonarQube server
- Activate your custom coding rules in your PHP quality profiles
PHP CodeSniffer Format
<?xml version="1.0" encoding="ISOUTF-8859-18"?> <rules> <rule key="PEAR.Commenting.FileComment.TagIndent" priority="MAJOR"> <category<!-- name="Maintainability" /, configKey, description are mandatory --> <name>Tag <name>Tag comment incorrectly indented</name> <configKey>TAG_INDENT</configKey> <description>@category tag comment incorrectly indented</description> </rule> <rule key="...> ... </rule> </rules>
This file must be copied in the directory $SONAR_HOME/extensions/rules/php_codesniffer_rules/. And then, you must restart you sonar instance to make it available for PHP profiles.
The value of the "key" attribute is a combination of different elements:
For instance, for the given key above ("
- "PEAR " is the folder found in the "Standards" directory of PHPCodeSniffer PHP CodeSniffer install directory
Commenting" is the folder found in the "Sniffs" directory of the "PEAR" folder
FileComment" is the name of the PHP file "FileCommentSniff.php" for from which "Sniff.php" was is removed
TagIndent" is the name of the error that can be found in "FileCommentSniff.php" file
Extending PHPMD works the same way. Except that you have to copy your file containing your custom violations in $SONAR_HOME/extensions/rules/phppmd_rules/
This XML file must look like the following example:
<?xml version="1.0" encoding="ISOUTF-8859-18"?> <rules> <rule <rule key="Code Size Rules/CyclomaticComplexity" priority="CRITICAL"> <!-- name, configKey, description are mandatory --> <name><![CDATA[Class cyclomatic complexity exceed maximum]]></name> <configKey>rulesets/codesize.xml/CyclomaticComplexity</configKey> <category name="Maintainability"/> <description><![CDATA[Class cyclomatic complexity exceed maximum]]></description> <param key="maximum" type="i"> <description><![CDATA[The maximum cyclomatic complexity threshold. Default is 10.]]></description> </param> </rule> <!-- other rules <rule key="... --> ... </rule> </rules>