To understand how rules extension works in Sonar, please refer to the Extending Coding Rules documentation.
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 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/. And then, you must restart you sonar instance You have to restart your Sonar server to make it available for PHP profiles.
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 works the same way . Except as PHP_CodeSniffer, except that you have to copy your file containing your custom violations in $SONAR_HOME/extensions/rules/phppmd_rules/