查找 PHP CodeSniffer 规则
Finding PHP CodeSniffer rules
如何找到我需要的 PHP CodeSniffer 规则?
例如,我希望函数参数和数组不要像这样间隔
function asd( $var, $var2 = '' ) {
$array = [ $arg, $arg2 ];
}
应该是这样的:
function asd($var, $var2 = '') {
$array = [$arg, $arg2];
}
我希望 phpcbf 能够解决这些问题,但我不知道如何找到执行此操作的规则。
没有 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 中没有嗅探器来检查这一点,所以如果你想强制执行它,你必须编写一个自定义嗅探器。
这显然不是获取此信息的好方法,文档会好得多,但这是我确定哪些嗅探器可用于检查代码(以及嗅探器是否可用)的方式所以我想我会 post 它作为答案。希望对您有所帮助。
如何找到我需要的 PHP CodeSniffer 规则?
例如,我希望函数参数和数组不要像这样间隔
function asd( $var, $var2 = '' ) {
$array = [ $arg, $arg2 ];
}
应该是这样的:
function asd($var, $var2 = '') {
$array = [$arg, $arg2];
}
我希望 phpcbf 能够解决这些问题,但我不知道如何找到执行此操作的规则。
没有 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 中没有嗅探器来检查这一点,所以如果你想强制执行它,你必须编写一个自定义嗅探器。
这显然不是获取此信息的好方法,文档会好得多,但这是我确定哪些嗅探器可用于检查代码(以及嗅探器是否可用)的方式所以我想我会 post 它作为答案。希望对您有所帮助。