如何找到我需要的PHP CodeSniffer规则?
例如,我希望函数参数和数组不像这样间隔
function asd( $var, $var2 = '' ) {
$array = [ $arg, $arg2 ];
}
相反它应该是这样的:
function asd($var, $var2 = '') {
$array = [$arg, $arg2];
}
我希望phpcbf能够解决这些问题,但我不知道如何找到规则。
答案 0 :(得分:1)
没有关于PHPCS规则的文档(我将在这一天中解决这个问题)所以找出所需规则的唯一方法是在一些示例代码上运行PHPCS。
对于此示例,我已将您的示例错误代码放入temp.php文件中:
<?php
function asd( $var, $var2 = '' ) {
$array = [ $arg, $arg2 ];
}
然后我使用附带的标准运行PHPCS。我使用-s
命令行参数来确保我也可以看到嗅探代码。我得到了这个输出:
$ phpcs temp.php --standard=Generic,Squiz,PEAR,PSR2,Zend -s
FILE: temp.php
---------------------------------------------------------------------------------------------------------------------------------------------
FOUND 17 ERRORS AND 5 WARNINGS AFFECTING 4 LINES
---------------------------------------------------------------------------------------------------------------------------------------------
1 | ERROR | [ ] The PHP open tag does not have a corresponding PHP close tag (Generic.PHP.ClosingPHPTag.NotFound)
1 | ERROR | [ ] Missing file doc comment (Squiz.Commenting.FileComment.Missing)
2 | WARNING | [ ] The method parameter $var is never used (Generic.CodeAnalysis.UnusedFunctionParameter.Found)
2 | WARNING | [ ] The method parameter $var2 is never used (Generic.CodeAnalysis.UnusedFunctionParameter.Found)
2 | WARNING | [ ] Consider putting global function "asd" in a static class (Squiz.Functions.GlobalFunction.Found)
2 | ERROR | [ ] Missing file doc comment (PEAR.Commenting.FileComment.Missing)
2 | ERROR | [ ] Missing function doc comment (Squiz.Commenting.FunctionComment.Missing)
2 | ERROR | [x] Expected 2 blank lines before function; 0 found (Squiz.WhiteSpace.FunctionSpacing.Before)
2 | ERROR | [ ] Missing function doc comment (PEAR.Commenting.FunctionComment.Missing)
2 | ERROR | [x] Expected 0 spaces between opening bracket and argument "$var"; 1 found
| | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpen)
2 | WARNING | [ ] Variable "var2" contains numbers but this is discouraged (Zend.NamingConventions.ValidVariableName.ContainsNumbers)
2 | ERROR | [x] Expected 0 spaces between argument "$var2" and closing bracket; 1 found
| | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeClose)
2 | ERROR | [x] Opening brace should be on a new line (Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine)
2 | ERROR | [x] Opening brace should be on a new line (Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine)
2 | ERROR | [x] Opening brace should be on a new line (PEAR.Functions.FunctionDeclaration.BraceOnSameLine)
3 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed (Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed)
3 | ERROR | [x] Short array syntax is not allowed (Generic.Arrays.DisallowShortArraySyntax.Found)
3 | ERROR | [x] Array with multiple values cannot be declared on a single line (Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed)
3 | WARNING | [ ] Variable "arg2" contains numbers but this is discouraged (Zend.NamingConventions.ValidVariableName.ContainsNumbers)
4 | ERROR | [x] Expected //end asd() (Squiz.Commenting.ClosingDeclarationComment.Missing)
4 | ERROR | [x] Expected 1 blank line before closing function brace; 0 found
| | (Squiz.WhiteSpace.FunctionClosingBraceSpace.SpacingBeforeClose)
4 | ERROR | [x] File must not end with a newline character (Generic.Files.EndFileNoNewline.Found)
---------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 12 MARKED SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------------------------------------------------------------------------
Time: 84ms; Memory: 8Mb
然后我挑选出你想要保留的信息。可能是这两个可修复的错误:
2 | ERROR | [x] Expected 0 spaces between opening bracket and argument "$var"; 1 found
| | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpen)
2 | ERROR | [x] Expected 0 spaces between argument "$var2" and closing bracket; 1 found
| | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeClose)
因此,您可以尝试在自定义标准中包含整个Squiz.Functions.FunctionDeclarationArgumentSpacing
嗅探,看看您是否喜欢这些结果。如果没有,您可以将这两条消息放入您的标准中,这将忽略其余的嗅探。
您会注意到短数组的开头和结尾没有报告错误。这告诉我PHPCS中没有包含嗅探来检查这一点,所以如果你想强制执行它,你必须写一个自定义嗅探器。
这显然不是获取此信息和文档的一种非常好的方式会更好,但这是我弄清楚哪些嗅探可用于检查代码的方式(如果有嗅探可用所以我想我会把它作为答案发布。希望它有所帮助。