Php 需要使用什么嗅探来强制camelCase变量命名约定?

Php 需要使用什么嗅探来强制camelCase变量命名约定?,php,codesniffer,phpcodesniffer,phpcs,Php,Codesniffer,Phpcodesniffer,Phpcs,我正在处理一个专有的遗留代码库,一些变量是驼峰式的,而另一些是蛇式的。我只想清理和强制使用驼峰大小写的变量名,但我似乎找不到嗅探。以下是我的自定义规则集的内容 The ruleset.xml standard contains 68 sniffs Generic (22 sniffs) ------------------- Generic.Classes.DuplicateClassName Generic.CodeAnalysis.ForLoopShouldBeWhileLoop

我正在处理一个专有的遗留代码库,一些变量是驼峰式的,而另一些是蛇式的。我只想清理和强制使用驼峰大小写的变量名,但我似乎找不到嗅探。以下是我的自定义规则集的内容

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

您需要使用
Squiz.NamingConventions.ValidVariableName
sniff检查变量名

此嗅探当前包括5个不同的错误代码。其中3个正在检查变量、成员变量和字符串中的变量是否都是大小写的。其他两个代码强制私有成员变量以下划线开头

为了确保变量采用驼峰式大小写,如果您使用的是PHPCS版本3,请将其包含在规则集中:

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

如果还希望确保成员变量和字符串变量采用驼峰式大小写,请在使用版本3时包括这些变量:

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

如果您想要全部,请包括以下内容:

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

PHPCS版本2不允许您包含特定的嗅探代码,因此您首先需要包含整个嗅探,然后通过将其严重性设置为0来排除不需要的特定代码。因此,如果您只需要3个camel case检查,并且正在使用版本2,请将其放入规则集中:

<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>

0
0

谢谢你的回答。这些嗅探不适用于方法参数。你还有其他解决办法吗?@TomášVotruba他们应该申请。我已经对
Squiz.NamingConventions.ValidVariableName
sniff进行了基本检查,它也在提取方法参数。如果您的代码不起作用,最好向项目报告一个bug。它会忽略私有属性它也会检查私有类属性,但会强制它们以下划线开头。如果它们不是以下划线开头,它不会一直检查它们是否也是有效的大小写。这对于任何不包括
PrivateNoUnderscore
消息的人来说都是一个很好的补充。如果这是您的问题,请考虑在项目上打开一个功能请求。好的,您总结得很好。我会把它贴在那里。编辑: