3

I am trying to add the Drupal Coding standards in phpcs.
I can run the Drupal Standards with phpcs --standard=Drupal

Now I want to execute the same standards with Sonar.

In Sonar I can add the keys of all the rulesets in the rules.xml extension, but how can I get all the keys for these rules?

I can identify some of the keys using the *sniff.php files and the folder structure but I'm not sure I am getting all of them.

Can anyone suggest an automated way to get all the rules available in a particular standard?

hakre
  • 193,403
  • 52
  • 435
  • 836
Bhavin Vora
  • 451
  • 3
  • 12

2 Answers2

2

You have to identify all the sniffs referenced by the Drupal standard and create a profile in Sonar (through the Web UI) that references them all. Then you activate this profile as the default one, and you're ready for an analysis.

  • Fabrice ,thanks for reply, You haven't understand it properly I want to create a profile , I don't have it. no rules are available in php plugin of sonar. – Bhavin Vora Jun 01 '12 at 05:00
  • Well, that's what I said: you have to "_create a profile in Sonar_"... When you say that no rules are available, you mean "no rule at all" (in which case there's a problem with the install on your side) or "no Drupal-related rule" (in which case you have to [add them like it is explained on the Wiki](http://docs.codehaus.org/display/SONAR/Extend+PHP+coding+rules))? – Fabrice - SonarSource Team Jun 01 '12 at 06:30
  • thanks for reply again, I knew that I have to add that rules using http://docs.codehaus.org/display/SONAR/Extend+PHP+coding+rules and I have done it. for some known one. but What I want is the key value of rules. I haven't any of them with me. and there is no list available for it. – Bhavin Vora Jun 01 '12 at 07:02
  • 1
    OK, I think some part of the documentation was missing on the Wiki: I updated the page to explain how you can find the "key" of a specific rule that you want to add in the XML file. You have to do it manually (but maybe PHPCodeSniffer provides a list of the Drupal standard sniffs somewhere?). – Fabrice - SonarSource Team Jun 01 '12 at 07:38
  • thats the thing was needed over there , I do have found this way also but still i think there must be some automation for this process. – Bhavin Vora Jun 01 '12 at 09:00
  • We tried to automate it but that's just impossible on our (Sonar) side. The only solution would be that the author of PHPCodeSniffer provides XML descriptions of all the checks, because parsing the PHP source code to identify the checks leads nowhere (we've already tried). – Fabrice - SonarSource Team Jun 01 '12 at 09:37
  • @Fabrice-SonarTeam I disagree about parsing the code, see my answer. – Potherca Sep 20 '12 at 14:23
  • By the way, how cool is it to have an actual member of the Sonar Team answering questions on SO! WootWoot! – Potherca Sep 20 '12 at 14:24
  • @Potherca We used to have a script that parses the PHP Checks, but it does not work: we not only need the name of the check, but also its description and the error messages that it can generate... and the biggest problem is that a single check can generate several different error messages, representing different rules. – Fabrice - SonarSource Team Sep 20 '12 at 14:42
  • @Fabrice-SonarTeam That's basically what I'm working on right now... grabbing variations of the key and messages. -- So far it doesn't look all that complex or mind boggling. In the end it basically boils down to the PHP code you're parsing. The lower the quality of the code, the harder it becomes to parse it. Still managing to get 99.99% of the stuff I need so far... – Potherca Sep 20 '12 at 18:12
0

It may not be clear from the help message but the command-line tools has a flag for this:

-e    Explain a standard by showing the sniffs it includes

So to show all of the sniffs in the Drupal coding standard one would run:

phpcs -e --standard=Drupal

The output would be something like this:

The vendor/drupal/coder/coder_sniffer/Drupal/ standard contains 111 sniffs

Drupal (68 sniffs)
------------------
  Drupal.Array.Array
  Drupal.Array.DisallowLongArraySyntax
  Drupal.CSS.ClassDefinitionNameSpacing
  Drupal.CSS.ColourDefinition
  Drupal.Classes.ClassCreateInstance
  Drupal.Classes.ClassDeclaration
  Drupal.Classes.FullyQualifiedNamespace
  Drupal.Classes.InterfaceName
  Drupal.Classes.PropertyDeclaration
  Drupal.Classes.UnusedUseStatement
  Drupal.Classes.UseLeadingBackslash
  Drupal.Commenting.ClassComment
  Drupal.Commenting.DataTypeNamespace
  Drupal.Commenting.DocCommentAlignment
  Drupal.Commenting.DocComment
  Drupal.Commenting.DocCommentStar
  Drupal.Commenting.FileComment
  Drupal.Commenting.FunctionComment
  Drupal.Commenting.HookComment
  Drupal.Commenting.InlineComment
  Drupal.Commenting.PostStatementComment
  Drupal.Commenting.VariableComment
  Drupal.ControlStructures.ControlSignature
  Drupal.ControlStructures.ElseIf
  Drupal.ControlStructures.InlineControlStructure
  Drupal.Files.EndFileNewline
  Drupal.Files.FileEncoding
  Drupal.Files.LineLength
  Drupal.Files.TxtFileLineLength
  Drupal.Formatting.MultiLineAssignment
  Drupal.Formatting.MultipleStatementAlignment
  Drupal.Formatting.SpaceInlineIf
  Drupal.Formatting.SpaceUnaryOperator
  Drupal.Functions.DiscouragedFunctions
  Drupal.Functions.FunctionDeclaration
  Drupal.InfoFiles.AutoAddedKeys
  Drupal.InfoFiles.ClassFiles
  Drupal.InfoFiles.DuplicateEntry
  Drupal.InfoFiles.Required
  Drupal.Methods.MethodDeclaration
  Drupal.NamingConventions.ValidClassName
  Drupal.NamingConventions.ValidFunctionName
  Drupal.NamingConventions.ValidGlobal
  Drupal.NamingConventions.ValidVariableName
  Drupal.Scope.MethodScope
  Drupal.Semantics.ConstantName
  Drupal.Semantics.EmptyInstall
  Drupal.Semantics.FunctionAlias
  Drupal.Semantics.FunctionT
  Drupal.Semantics.FunctionWatchdog
  Drupal.Semantics.InstallHooks
  Drupal.Semantics.LStringTranslatable
  Drupal.Semantics.PregSecurity
  Drupal.Semantics.RemoteAddress
  Drupal.Semantics.TInHookMenu
  Drupal.Semantics.TInHookSchema
  Drupal.Strings.UnnecessaryStringConcat
  Drupal.WhiteSpace.CloseBracketSpacing
  Drupal.WhiteSpace.Comma
  Drupal.WhiteSpace.EmptyLines
  Drupal.WhiteSpace.Namespace
  Drupal.WhiteSpace.ObjectOperatorIndent
  Drupal.WhiteSpace.ObjectOperatorSpacing
  Drupal.WhiteSpace.OpenBracketSpacing
  Drupal.WhiteSpace.OpenTagNewline
  Drupal.WhiteSpace.OperatorSpacing
  Drupal.WhiteSpace.ScopeClosingBrace
  Drupal.WhiteSpace.ScopeIndent

Generic (13 sniffs)
-------------------
  Generic.CodeAnalysis.UselessOverridingMethod
  Generic.Files.ByteOrderMark
  Generic.Files.LineEndings
  Generic.Formatting.SpaceAfterCast
  Generic.Functions.FunctionCallArgumentSpacing
  Generic.Functions.OpeningFunctionBraceKernighanRitchie
  Generic.NamingConventions.ConstructorName
  Generic.NamingConventions.UpperCaseConstantName
  Generic.PHP.DeprecatedFunctions
  Generic.PHP.DisallowShortOpenTag
  Generic.PHP.LowerCaseKeyword
  Generic.PHP.UpperCaseConstant
  Generic.WhiteSpace.DisallowTabIndent

MySource (1 sniffs)
-------------------
  MySource.Debug.DebugCode

PEAR (3 sniffs)
---------------
  PEAR.Files.IncludingFile
  PEAR.Functions.FunctionCallSignature
  PEAR.Functions.ValidDefaultValue

PSR2 (2 sniffs)
---------------
  PSR2.Namespaces.NamespaceDeclaration
  PSR2.Namespaces.UseDeclaration

Squiz (23 sniffs)
-----------------
  Squiz.Arrays.ArrayBracketSpacing
  Squiz.Arrays.ArrayDeclaration
  Squiz.CSS.ClassDefinitionClosingBraceSpace
  Squiz.CSS.ClassDefinitionOpeningBraceSpace
  Squiz.CSS.ColonSpacing
  Squiz.CSS.DisallowMultipleStyleDefinitions
  Squiz.CSS.EmptyClassDefinition
  Squiz.CSS.EmptyStyleDefinition
  Squiz.CSS.Indentation
  Squiz.CSS.MissingColon
  Squiz.CSS.SemicolonSpacing
  Squiz.ControlStructures.ForEachLoopDeclaration
  Squiz.ControlStructures.ForLoopDeclaration
  Squiz.ControlStructures.SwitchDeclaration
  Squiz.Functions.FunctionDeclarationArgumentSpacing
  Squiz.Functions.MultiLineFunctionDeclaration
  Squiz.PHP.LowercasePHPFunctions
  Squiz.PHP.NonExecutableCode
  Squiz.Strings.ConcatenationSpacing
  Squiz.WhiteSpace.FunctionSpacing
  Squiz.WhiteSpace.LanguageConstructSpacing
  Squiz.WhiteSpace.SemicolonSpacing
  Squiz.WhiteSpace.SuperfluousWhitespace

Zend (1 sniffs)
---------------
  Zend.Files.ClosingTag
Potherca
  • 13,207
  • 5
  • 76
  • 94