标签:php codesniffer phpcodesniffer phpcs
我正在研究一个专有的遗留代码库,一些变量是驼峰式的,而另一些则是蛇形的.我想清理并强制执行只有驼峰的变量名称,但我似乎无法找到它的嗅觉.以下是我的自定义规则集的内容.
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嗅探来检查变量名称.
此嗅探当前包含5个不同的错误代码.其中3个正在检查字符串中的变量,成员变量和变量都是驼峰式的.其他2个代码强制私有成员变量以下划线开头.
要确保变量是camel-cased,如果您使用的是PHPCS版本3,请将其包含在规则集中:
<rule ref="Squiz.NamingConventions.ValidVariableName.NotCamelCaps"/>
如果您还想确保成员变量和字符串变量是驼峰式的,请在使用版本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>
标签:php,codesniffer,phpcodesniffer,phpcs 来源: https://codeday.me/bug/20190828/1751002.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。