Customize & Extend PHP Codesniffer ruleset for Sonar PHP Profile

Customize & Extend PHP Codesniffer ruleset for Sonar PHP Profile

Sonar the tool that we are using for our code review. For PHP sonar has plugin which uses a PHP Codesniffer PEAR package to sniff code for violations. Almost all of them are predefined. If there is a requirement to add few more rules to the default ruleset, there is a way to do it with PHP_Codesniffer pear package.

How to customize default ruleset of PHP_Codesniffer ruleset for Sonar profile.

Following are the steps when one needs to change the predefined value for a rule and make it configuratioble settings via sonar profile page user interface.

  1. Violation in your project,

BREAK statement must be indented 4 spaces from SWITCH keyword. Here the statement is indented 2 space as per it's a code done for Drupal, but the rules show that we need 4 space which avoid the drupal standards .





Here it is SwitchDeclarationBreakIndent is the rule name which is having that specific codeSniffer for checking the indentation.
  • goto configuration and get the rule key.





It's Squiz.ControlStructures.SwitchDeclaration.BreakIndent
Here there is no modification for the indentation is available. So we need to Customize it.

  • now goto your codesniffer installed package and follow following path
    • CodeSniffer (/usr/share/php/PHP/CodeSniffer is mine location)
    • in that /Standards have all the standard coding rules
    • you will get Squiz/Sniffers/ControlStructures representing the first 2 part of the key Squiz.ControlStructures
    • SwitchDeclaration.BreakIndent indicates the sniffer file name it's SwitchDeclarationSniff.php

  1. Making Require Changes in It.

  • Look for
$caseAlignment = ($switch['column'] + 4); - line no 77
here the value 4 is given static for maknig any keyword aligned 4 spaced from switch .
It's applied to both case and break.
So we create a new one for us.
    • First define a public variable for class
      • public $breakalign = 4;
    • make a local variable for local scope changes.
      • $localbreakalign = ($switch['column'] + $this->breakalign);
    • And apply the new variable in the portion of code to make related changes.
      • if ($tokens[$nextBreak]['column'] !== $localbreakalign) {
$error = 'BREAK statement must be indented '.$this->breakalign.' spaces from SWITCH keyword';
$phpcsFile->addError($error, $nextBreak, 'BreakIndent');
}
    • make changes as u required.

  • Now why we have made it . Is simply we need a public variable which we can modify from a ruleset.xml file which is the next part.


  1. Making a changes in ruleset.xml

  • ruleset.xml is where all we have to define a property of a given rule. Follow this template.
    • <rule ref="Squiz.ControlStructures.SwitchDeclaration.BreakIndent">
<properties>
<property name="breakalign" value="4"/>
</properties>
</rule>



  1. Making a changes in sonar rule configuration for Extending the rule.

  • Just create a rules.xml at $SONAR_HOME/extensions/rules/php_codesniffer_rules/
<?xml version="1.0" encoding="ISO-8859-1"?>
<rules>
<rule key="Squiz.ControlStructures.SwitchDeclaration.BreakIndent" priority="MAJOR">
<category name="Maintainability"/>
<name>SwitchDeclarationBreakIndent</name>
<configKey>rulesets/SwitchDeclaration</configKey>
<description><![CDATA[BREAK statement must be indented 4 spaces from SWITCH keyword]]></description>
<param>
<key>breakalign</key>
<description>The number of spaces code should be indented.</description>
<defaultValue>4</defaultValue>
</param>
</rule>
</rules>
    • use this code here we have added a key breakalign for some making it changable from sonar panel.
  1. Restart your Sonar.
  2. Goto the configuration

  1. And now you can make a change to the rule with your own standards.






Comments

Popular posts from this blog

Privacy Policy - StoryScribe

Privacy Policy - aXa Voice Command Assistant

StoryScribe