Search code examples
phpphpcodesnifferphpcs

What sniff needs to be used to enforce a camelCase variable naming convention?


I'm working on a proprietary legacy code base and some variables are camel-cased while others are snake-cased. I'd like to clean up and enforce only camel-cased variable names but I can't seem to find a sniff for that. Here are the contents of my custom ruleset.

The ruleset.xml standard contains 68 sniffs

Generic (22 sniffs)
-------------------
  Generic.Classes.DuplicateClassName
  Generic.CodeAnalysis.ForLoopShouldBeWhileLoop
  Generic.CodeAnalysis.UnconditionalIfStatement
  Generic.CodeAnalysis.UnnecessaryFinalModifier
  Generic.CodeAnalysis.UnusedFunctionParameter
  Generic.CodeAnalysis.UselessOverridingMethod
  Generic.Commenting.Fixme
  Generic.Commenting.Todo
  Generic.ControlStructures.InlineControlStructure
  Generic.Files.ByteOrderMark
  Generic.Files.LineEndings
  Generic.Files.LineLength
  Generic.Formatting.DisallowMultipleStatements
  Generic.Formatting.NoSpaceAfterCast
  Generic.Functions.FunctionCallArgumentSpacing
  Generic.NamingConventions.CamelCapsFunctionName
  Generic.NamingConventions.UpperCaseConstantName
  Generic.PHP.DisallowShortOpenTag
  Generic.PHP.LowerCaseConstant
  Generic.PHP.LowerCaseKeyword
  Generic.WhiteSpace.DisallowTabIndent
  Generic.WhiteSpace.ScopeIndent

PEAR (5 sniffs)
---------------
  PEAR.Commenting.InlineComment
  PEAR.Formatting.MultiLineAssignment
  PEAR.Functions.ValidDefaultValue
  PEAR.WhiteSpace.ScopeClosingBrace
  PEAR.WhiteSpace.ScopeIndent

PSR1 (3 sniffs)
---------------
  PSR1.Classes.ClassDeclaration
  PSR1.Files.SideEffects
  PSR1.Methods.CamelCapsMethodName

PSR2 (12 sniffs)
----------------
  PSR2.Classes.ClassDeclaration
  PSR2.Classes.PropertyDeclaration
  PSR2.ControlStructures.ControlStructureSpacing
  PSR2.ControlStructures.ElseIfDeclaration
  PSR2.ControlStructures.SwitchDeclaration
  PSR2.Files.ClosingTag
  PSR2.Files.EndFileNewline
  PSR2.Methods.FunctionCallSignature
  PSR2.Methods.FunctionClosingBrace
  PSR2.Methods.MethodDeclaration
  PSR2.Namespaces.NamespaceDeclaration
  PSR2.Namespaces.UseDeclaration

Squiz (26 sniffs)
-----------------
  Squiz.Classes.ValidClassName
  Squiz.ControlStructures.ControlSignature
  Squiz.ControlStructures.ForEachLoopDeclaration
  Squiz.ControlStructures.ForLoopDeclaration
  Squiz.ControlStructures.LowercaseDeclaration
  Squiz.Functions.FunctionDeclarationArgumentSpacing
  Squiz.Functions.FunctionDeclaration
  Squiz.Functions.LowercaseFunctionKeywords
  Squiz.Functions.MultiLineFunctionDeclaration
  Squiz.PHP.CommentedOutCode
  Squiz.PHP.Eval
  Squiz.PHP.GlobalKeyword
  Squiz.PHP.Heredoc
  Squiz.PHP.InnerFunctions
  Squiz.PHP.LowercasePHPFunctions
  Squiz.PHP.NonExecutableCode
  Squiz.Scope.MethodScope
  Squiz.Scope.StaticThisUsage
  Squiz.WhiteSpace.ControlStructureSpacing
  Squiz.WhiteSpace.ObjectOperatorSpacing
  Squiz.WhiteSpace.OperatorSpacing
  Squiz.WhiteSpace.PropertyLabelSpacing
  Squiz.WhiteSpace.ScopeClosingBrace
  Squiz.WhiteSpace.ScopeKeywordSpacing
  Squiz.WhiteSpace.SemicolonSpacing
  Squiz.WhiteSpace.SuperfluousWhitespace

Solution

  • You'll want to use the Squiz.NamingConventions.ValidVariableName sniff to check variable names.

    This sniff currently includes 5 different error codes. 3 of them are checking that variables, member vars, and vars inside strings are all camel-cased. The other 2 codes enforce that private member vars begin with an underscore.

    To just ensure that variables are camel-cased, include this in your ruleset if you are using PHPCS version 3:

    <rule ref="Squiz.NamingConventions.ValidVariableName.NotCamelCaps"/>
    

    If you also want to ensure member vars and string vars are camel-cased, include these if you are using version 3:

    <rule ref="Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps"/>
    <rule ref="Squiz.NamingConventions.ValidVariableName.StringNotCamelCaps"/>
    

    If you want the whole lot, include this:

    <rule ref="Squiz.NamingConventions.ValidVariableName"/>
    

    PHPCS version 2 does not let you include specific sniff codes, so you will first need to include the entire sniff and then exclude the specific codes you don't want by setting their severity to 0. So if you want just the 3 camel-case checks and you are using version 2, put this in your ruleset:

    <rule ref="Squiz.NamingConventions.ValidVariableName"/>
    <rule ref="Squiz.NamingConventions.ValidVariableName.PublicHasUnderscore">
        <severity>0</severity>
    </rule>
    <rule ref="Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore">
        <severity>0</severity>
    </rule>