Skip to content

Instantly share code, notes, and snippets.

@geerlingguy
Last active January 30, 2024 16:47
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save geerlingguy/6523454 to your computer and use it in GitHub Desktop.
Save geerlingguy/6523454 to your computer and use it in GitHub Desktop.
A simple PHP script to generate the XML necessary for Sonar to detect the Drupal Coder module's sniffs. See: https://drupal.org/node/2082563
<?php
/**
* @file
* Generate rules for Drupal Coding Standards Sniffs.
*
* SonarQube requires manually-entered PHP CodeSniffer rules if you use anything
* outside of the standard set of rules. In this case, there are a bunch of
* Drupal-specific sniffs that we'd like to use.
*
* This file takes an array of sniff keys and generates the appropriate XML to
* pasted into the sonar.phpCodesniffer.customRules.definition setting inside
* Sonar's Configuration area.
*
* After pasting the generated XML, you need to restart the SonarQube server,
* then enable all the rules you want to use in a quality profile.
*
* @see http://docs.codehaus.org/display/SONAR/PHP+Custom+Coding+Rules
* @see http://docs.codehaus.org/display/SONAR/Quality+Profiles
*/
// Create an XML document and open the rules tag.
print '<?xml version="1.0" encoding="UTF-8"?>
<rules>
';
// Define the missing php_codesniffer keys.
$keys = array(
'Drupal.Commenting.FunctionComment.ParamCommentNewLine',
'Drupal.Commenting.FunctionComment.HookReturnDoc',
'Drupal.Commenting.FunctionComment.MissingReturnType',
'Drupal.Commenting.FunctionComment.HookParamDoc',
'Drupal.Commenting.FunctionComment.ParamNameNoMatch',
'Drupal.Commenting.FunctionComment.SpacingBeforeParamType',
'Drupal.Commenting.InlineComment.SpacingBefore',
'Drupal.Commenting.FileComment',
'Drupal.ControlStructures.InlineControlStructure.NotAllowed',
'Drupal.Commenting.FunctionComment',
'Drupal.Files.LineLength.TooLong',
'Drupal.Semantics.FunctionCall.LArg',
'Drupal.WhiteSpace.OperatorSpacing.SpacingAfter',
'Drupal.Formatting.DisallowCloseTag.FinalClose',
'Drupal.WhiteSpace.OpenBracketSpacing.OpeningWhitespace',
'Drupal.Commenting.FileComment.DescriptionMissing',
'Drupal.Commenting.InlineComment.NoSpaceBefore',
'Drupal.Strings.ConcatenationSpacing.Missing',
'Drupal.Commenting.InlineComment.WrongStyle',
'Drupal.Semantics.Br.XHTMLBr',
'Drupal.Strings.UnnecessaryStringConcat.Found',
'Drupal.Commenting.FunctionComment.Empty',
'Drupal.NamingConventions.ValidClassName.NoUnderscores',
'Drupal.Commenting.FileComment.SpacingAfter',
'Drupal.Commenting.FunctionComment.$InReturnType',
'Drupal.Array.Array.LongLineDeclaration',
'Drupal.Commenting.DocCommentAlignment.SpaceBeforeAsterisk',
'Drupal.Commenting.FunctionComment.ShortSingleLine',
'Drupal.Semantics.FunctionCall.NotLiteralString',
'Drupal.Commenting.FunctionComment.ShortFullStop',
'Drupal.Commenting.InlineComment.NotCapital',
'Drupal.Commenting.InlineComment.SpacingAfter',
'Drupal.Commenting.FileComment.WrongStyle',
'Drupal.Formatting.SpaceInlineIf.NoSpaceAfter',
'Drupal.Commenting.FunctionComment.ShortNotCapital',
'Drupal.Array.Array.ArrayIndentation',
'Drupal.Commenting.FunctionComment.MissingParamType',
'Drupal.ControlStructures.ElseIf',
'Drupal.Array.Array',
'Drupal.ControlStructures.TemplateControlStructure.CurlyBracket',
'Drupal.Array.Array.ArrayClosingIndentation',
'Drupal.WhiteSpace.ScopeIndent.IncorrectExact',
'Drupal.ControlStructures.ControlSignature',
'Drupal.Formatting.SpaceInlineIf.NoSpaceBefore',
'Drupal.Semantics.EmptyInstall.EmptyInstall',
'Drupal.Commenting.FunctionComment.ShortStartSpace',
'Drupal.WhiteSpace.OperatorSpacing.NoSpaceAfter',
'Drupal.ControlStructures.ElseCatchNewline.ElseNewline',
'Drupal.Commenting.InlineComment.InvalidEndChar',
'Drupal.WhiteSpace.CloseBracketSpacing.ClosingWhitespace',
'Drupal.Semantics.TInHookMenu.TFound',
'Drupal.Commenting.FunctionComment.Missing',
'Drupal.Semantics.FunctionCall.EmptyString',
'Drupal.Semantics.FunctionCall.ConstantStart',
'Drupal.Formatting.MultiLineAssignment',
'Drupal.Commenting.InlineComment.Empty',
'Drupal.NamingConventions.ValidVariableName',
'Drupal.Commenting.DocCommentAlignment.BlankLine',
'Drupal.WhiteSpace.OperatorSpacing.NoSpaceBefore',
'Drupal.WhiteSpace.EmptyLines.EmptyLines',
'Drupal.WhiteSpace.FileEnd.FileEnd',
'Drupal.WhiteSpace.ScopeIndent.Incorrect',
'Drupal.WhiteSpace.ScopeClosingBrace.BreakIdent',
'Drupal.WhiteSpace.ObjectOperatorSpacing.After',
'Drupal.NamingConventions.ValidClassName.StartWithCaptial',
'Drupal.WhiteSpace.ScopeClosingBrace.Indent',
'Squiz.WhiteSpace.SuperfluousWhitespace.EndLine',
'Drupal.Commenting.FileComment.Missing',
'Squiz.WhiteSpace.SuperfluousWhitespace.StartFile',
'Generic.Functions.OpeningFunctionBraceKernighanRitchie.SpaceAfterBracket',
'Drupal.WhiteSpace.ObjectOperatorSpacing.Before',
'Drupal.Classes.ClassCreateInstance',
'PSR2.Classes.PropertyDeclaration.Multiple',
'Drupal.Semantics.FunctionCall.FunctionAlias',
'Drupal.Commenting.FunctionComment.MissingParamComment',
'Drupal.Commenting.FunctionComment.EmptyLinesAfterDoc',
'Drupal.Semantics.FunctionCall.BackslashSingleQuote',
'Drupal.Formatting.SpaceInlineIf.SpacingBefore',
'Drupal.Semantics.FunctionCall.Concat',
'Drupal.Semantics.FunctionCall.WatchdogT',
'Drupal.Commenting.FunctionComment.SeePunctuation',
'Squiz.WhiteSpace.SuperfluousWhitespace.EndFile',
'Drupal.Semantics.InstallT.TranslationFound',
'Drupal.Commenting.FunctionComment.MissingShort',
'Drupal.NamingConventions.ValidFunctionName.NotLowerCamel',
'Drupal.WhiteSpace.ScopeClosingBrace',
'Drupal.Commenting.FunctionComment.MissingReturnComment',
'Drupal.Commenting.FunctionComment.ReturnCommentNewLine',
'Internal.NoCodeFound',
'Drupal.Commenting.FunctionComment.MissingParamName',
'Drupal.Classes.ClassDeclaration',
'Drupal.Classes.PrivateKeyword.PrivateKeyword',
'Drupal.WhiteSpace.OperatorSpacing.SpacingBefore',
'Drupal.NamingConventions.ValidVariableName.LowerCamelName',
'Drupal.Commenting.InlineComment.DocBlock',
'Drupal.Commenting.FunctionComment.ContentAfterOpen',
'Drupal.Functions.FunctionDeclaration.SpaceBeforeParenthesis',
'Drupal.Commenting.FunctionComment.SpacingAfterParams',
'Drupal.Commenting.FunctionComment.ExtraParamComment',
'Drupal.Commenting.FunctionComment.SeeAdditionalText',
'Drupal.Commenting.FunctionComment.SpacingBeforeParams',
'Drupal.Commenting.FunctionComment.InvalidParamTypeName',
'Drupal.NamingConventions.ValidFunctionName.ScopeNotLowerCamel',
'MySource.Commenting.FunctionComment.ParamCommentFullStop',
'MySource.PHP.GetRequestData.SuperglobalAccessed',
'Generic.PHP.ForbiddenFunctions.FoundWithAlternative',
'MySource.Commenting.FunctionComment.InvalidReturnNotVoid',
'Squiz.Commenting.FileComment.MissingShort',
'MySource.Commenting.FunctionComment.ShortSingleLine',
'MySource.Commenting.FunctionComment.ShortNotCapital',
'Squiz.Commenting.VariableComment.WrongStyle',
'MySource.Commenting.FunctionComment.Missing',
'Squiz.Commenting.ClassComment.Missing',
'Squiz.NamingConventions.ValidFunctionName.ScopeNotCamelCaps',
'Generic.Commenting.Todo.CommentFound',
'Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine',
'Squiz.Arrays.ArrayDeclaration.MultiLineNotAllowed',
'Squiz.Commenting.FileComment.MissingSubpackageTag',
'MySource.Commenting.FunctionComment.SpacingAfterLongType',
'Squiz.Commenting.BlockComment.NoCapital',
'Squiz.Commenting.FileComment.Missing',
'Drupal.Commenting.DocCommentAlignment.SpaceBeforeTag',
'MySource.Commenting.FunctionComment.IncorrectParamVarName',
'MySource.Commenting.FunctionComment.ParamCommentNotCapital',
'PEAR.ControlStructures.MultiLineCondition.NewlineBeforeOpenBrace',
'Drupal.Commenting.FunctionComment.ParamCommentIndentation',
'Squiz.Functions.MultiLineFunctionDeclaration.Indent',
'MySource.Commenting.FunctionComment.SpacingAfterParams',
'Squiz.Commenting.FileComment.MissingCopyrightTag',
'MySource.Commenting.FunctionComment.SpacingBeforeParamType',
'MySource.Commenting.FunctionComment.MissingParamTag',
'Squiz.NamingConventions.ConstantCase.Found',
'Squiz.Commenting.ClassComment.WrongStyle',
'Squiz.Arrays.ArrayDeclaration.CommaAfterLast',
'PEAR.Commenting.ClassComment.WrongTagOrder',
'PEAR.WhiteSpace.ScopeIndent.Incorrect',
'MySource.Commenting.FunctionComment.ParameterCommentsNotAligned',
'Squiz.Commenting.FunctionComment.ParamNameNoMatch',
'MySource.Commenting.FunctionComment.InvalidReturn',
'MySource.Commenting.FunctionComment.SpacingBeforeTags',
'Squiz.NamingConventions.ValidFunctionName.PublicUnderscore',
'Squiz.Commenting.VariableComment.Missing',
'Drupal.Formatting.SpaceInlineIf.SpacingAfter',
'Zend.NamingConventions.ValidVariableName.ContainsNumbers',
'PEAR.NamingConventions.ValidFunctionName.FunctionNoCapital',
'Squiz.Commenting.FileComment.WrongStyle',
'MySource.Commenting.FunctionComment.SpacingAfterLongName',
'MySource.Commenting.FunctionComment.MissingShort',
'MySource.Commenting.FunctionComment.EmptyThrows',
'Drupal.Commenting.FunctionComment.InvalidReturnTypeName',
'Squiz.Functions.MultiLineFunctionDeclaration.CloseBracketLine',
'Generic.Formatting.MultipleStatementAlignment.NotSameWarning',
'Squiz.Classes.ClassDeclaration.OpenBraceNewLine',
'Squiz.Commenting.FileComment.MissingPackageTag',
'MySource.Commenting.FunctionComment.ParameterNamesNotAligned',
'Squiz.WhiteSpace.MemberVarSpacing.Incorrect',
'MySource.Commenting.FunctionComment.LongNotCapital',
'Drupal.Commenting.FunctionComment.VoidReturn',
'MySource.Commenting.FunctionComment.MissingReturn',
'MySource.Commenting.FunctionComment.IncorrectTypeHint',
'MySource.Commenting.FunctionComment.ReturnIndent',
'MySource.Commenting.FunctionComment.InvalidNoReturn',
'MySource.Commenting.FunctionComment.ParamNameNoMatch',
'MySource.Commenting.FunctionComment.TypeHintMissing',
'Squiz.Functions.MultiLineFunctionDeclaration.NewlineBeforeOpenBrace',
'Squiz.ControlStructures.SwitchDeclaration.SpaceBeforeColonCase',
'Squiz.Commenting.BlockComment',
'MySource.Commenting.FunctionComment.ExtraParamComment',
'MySource.Commenting.FunctionComment.ShortFullStop',
'MySource.Commenting.FunctionComment.MissingParamComment',
'MySource.PHP.GetRequestData.SuperglobalAccessedWithVar',
'MySource.Commenting.FunctionComment.SpacingAfter',
'Squiz.Commenting.FileComment.MissingAuthorTag',
'MySource.Commenting.FunctionComment.SeeIndent',
'Generic.Formatting.MultipleStatementAlignment.IncorrectWarning',
'MySource.Commenting.FunctionComment.SpacingBeforeParams',
'MySource.Commenting.FunctionComment.MissingParamName',
'Squiz.Operators.IncrementDecrementUsage',
'Squiz.ControlStructures.SwitchDeclaration.SpaceBeforeColonDefault',
'Drupal.Commenting.FunctionComment.SpacingBeforeReturnType',
'Squiz.Functions.MultiLineFunctionDeclaration.BraceIndent',
);
// Loop through the keys and print them as rules.
foreach ($keys as $key) {
$parts = explode('.', $key);
$rule = end($parts);
print ' <rule key="' . $key . '" priority="MAJOR">
<name>' . $key . '</name>
<configKey>' . strtoupper($rule) . '</configKey>
<description>' . $key . '</description>
</rule>
';
}
// Close the rules tag.
print '</rules>';
@Eli-TW
Copy link

Eli-TW commented Nov 16, 2013

I've added some more keys to a fork here if you're interested: https://gist.github.com/Eli-TW/7499132

@geerlingguy
Copy link
Author

@Eli-TW - I've incorporated your additions, and added a few dozen more :)

@LedzZm
Copy link

LedzZm commented Feb 16, 2022

Hey there @geerlingguy , this script is meant to produce a xml ruleset for SonarQube, which is up-to-date with the drupal/coder rules?

@geerlingguy
Copy link
Author

@LedzZm - It's been a while since last time I updated it. It may still be a good starting point, but is probably a bit out of date now.

@dubeysurbhit
Copy link

It is out dated. SonarQube is not support the xml ruleset. Now we need to write plugin in java to add custom rules in SonarQube. see below link for more info:
https://docs.sonarqube.org/latest/extend/adding-coding-rules/
https://docs.sonarqube.org/latest/analysis/languages/php/

@jonmenard
Copy link

has anyone wrote a plugin for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment