I'm trying to integrate Sonar into our Drupal project's code review process, and so far I've had good luck getting Jenkins + Phing + other parts (like phploc) integrated, but for the PHP_CodeSniffer integration, I get a bunch of errors when using sonar-runner to check the codebase against the Drupal-specific PHP coding standards, like the following:
09:10:35.624 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Commenting.InlineComment.SpacingBefore
09:10:35.624 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Commenting.FileComment
09:10:35.624 INFO - No rule found in repository php_codesniffer_rules for key Drupal.ControlStructures.InlineControlStructure.NotAllowed
09:10:35.624 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Commenting.FunctionComment
09:10:35.624 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Files.LineLength.TooLong
09:10:35.625 INFO - No rule found in repository php_codesniffer_rules for key Drupal.WhiteSpace.OperatorSpacing.SpacingAfter
09:10:35.625 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Semantics.FunctionCall.LArg
09:10:35.625 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Formatting.DisallowCloseTag.FinalClose
09:10:35.625 INFO - No rule found in repository php_codesniffer_rules for key Drupal.WhiteSpace.ObjectOperatorSpacing.After
09:10:35.625 INFO - No rule found in repository php_codesniffer_rules for key Drupal.WhiteSpace.OpenBracketSpacing.OpeningWhitespace
09:10:35.625 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Commenting.FileComment.DescriptionMissing
09:10:35.625 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Commenting.InlineComment.NoSpaceBefore
09:10:35.625 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Strings.ConcatenationSpacing.Missing
09:10:35.625 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Commenting.InlineComment.WrongStyle
09:10:35.625 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Semantics.Br.XHTMLBr
09:10:35.625 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Strings.UnnecessaryStringConcat.Found
09:10:35.625 INFO - No rule found in repository php_codesniffer_rules for key Drupal.Commenting.FunctionComment.Empty
...
I found PhpCodeSnifferKeyFinder on GitHub, referenced through the Stack Exchange question How to get all the keys for of a PHP Codesniffer Ruleset to use with Sonar?, but it seems to only find a majority of the sniffs, not all of them.
It looks like I will need to take the keys and build some XML (following these directions for adding PHP custom coding rules) to add to the rules to Sonar's PHP CodeSniffer settings, then enable those keys in the SonarQube quality profile I'm using. But I don't want to manually generate a huge XML file if someone else already has, or if there's a more automated way to do it that I haven't seen...
Comments
Comment #1
geerlingguy commentedIt looks like this is a fairly manual process. I've found that the simplest thing to do was copy out all the missing rules from the Terminal, clean them up and put them in an array, and use a simple script to loop through and generate the XML for the rules to paste into the PHP CodeSniffer configuration in Sonar.
So, here are the steps I used to get the Drupal rules working with Sonar:
sonar.phpCodesniffer.customRules.definitionsection under Configuration > PHP CodeSniffer.Now, next time you run sonar (like with sonar-runner as I'm doing alongside Jenkins), Sonar should pick up all the Drupal coding standards violations. Nice!
Comment #2
klausiCoder 7.x is frozen now and will not receive updates. Coder 8.x-2.x can be used to check code for any Drupal version, Coder 8.x-2.x also supports the phpcbf command to automatically fix conding standard errors. Please check if this issue is still relevant and reopen against that version if necessary.