1

如何找到我需要的 PHP CodeSniffer 规则?

例如,我希望函数参数和数组不要像这样隔开

function asd( $var, $var2 = '' ) {
    $array = [ $arg, $arg2 ];
}

相反,它应该是这样的:

function asd($var, $var2 = '') {
    $array = [$arg, $arg2];
}

而且我希望 phpcbf 能够解决这些问题,但我不知道如何找到哪些规则可以做到这一点。

4

1 回答 1

6

没有关于 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 中没有包含嗅探器来检查这一点,所以如果你想强制执行它,你必须编写一个自定义嗅探器。

这显然不是获取此信息的好方法,文档会好得多,但这是我弄清楚哪些嗅探可用于检查代码(以及是否可用嗅探)的方式,所以我想我会发布它作为答案。希望它有一点帮助。

于 2017-07-05T22:40:16.637 回答