2017-08-06 54 views
2

我正在使用專有的遺留代碼庫,有些變量是駱駝殼,而其他變量是蛇形的。我想清理和強制執行只駱駝變量的名稱,但我似乎無法找到一個嗅探。以下是我的自定義規則集的內容。需要使用哪些嗅探來強制執行camelCase變量命名約定?

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 

回答

3

您會想要使用Squiz.NamingConventions.ValidVariableName sniff來檢查變量名稱。

這嗅探目前包括5個不同的錯誤代碼。其中3人正在檢查字符串中的變量,成員變量和變量都是駝峯式的。其他2個代碼強制私人成員變量以下劃線開頭。

如果只想確保變量是駱駝,套管,包括這在你的規則集,如果你正在使用PHPCS版本3:

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

如果你也想確保成員VAR和串瓦爾的駱駝套管,包括這些,如果您使用的版本3:

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

如果你想一大堆,包括此:

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

PHPCS版本2不允許包含特定的嗅探代碼,因此您首先需要包括整個嗅探,然後通過將其嚴重性設置爲0來排除您不需要的特定代碼。因此,如果您只需要3頭駱駝 - 案例檢查,你使用的版本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

謝謝你的答案。這些嗅探不適用於方法參數。你有沒有其他ssolution? –

+0

@TomášVotruba他們應該申請。我已經完成了'Squiz.NamingConventions.ValidVariableName'嗅探的基本檢查,並且它正在拾取方法參數。如果你的代碼不工作,最好向項目報告一個錯誤。 –

+0

它忽略私有屬性 –