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 rules.
To do so, you just have to create your own rules.xml file following the below format:
<?xml version="1.0" encoding="ISO-8859-1"?> <rules> <rule key="PEAR.Commenting.FileComment.TagIndent" priority="MAJOR"> <category name="Maintainability" /> <name>Tag comment incorrectly indented</name> <configKey>TAG_INDENT</configKey> <description>@category tag comment incorrectly indented</description> </rule> </rules>
This file must be copied in the directory $SONAR_HOME/extensions/rules/php_codesniffer_rules/. You have to restart your Sonar server 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 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 which "Sniff.php" was removed
TagIndent" is the name of the error that can be found in "FileCommentSniff.php" file
Extending PHPMD Rules
Extending PHPMD works the same way as PHP_CodeSniffer, 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="ISO-8859-1"?> <rules> <rule key="Code Size Rules/CyclomaticComplexity" priority="CRITICAL"> <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 ... --> </rules>