首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找PHP CodeSniffer规则

查找PHP CodeSniffer规则
EN

Stack Overflow用户
提问于 2017-07-05 06:28:25
回答 1查看 3K关注 0票数 2

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

例如,我希望函数参数和数组不像这样排列

代码语言:javascript
复制
function asd( $var, $var2 = '' ) {
    $array = [ $arg, $arg2 ];
}

相反,应该是这样的:

代码语言:javascript
复制
function asd($var, $var2 = '') {
    $array = [$arg, $arg2];
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-05 22:40:16

没有关于PHPCS规则的文档(总有一天我会讨论这个问题),因此了解需要哪些规则的唯一方法是在一些示例代码上运行PHPCS。

在本例中,我将示例糟糕的代码放入temp.php文件中:

代码语言:javascript
复制
<?php
function asd( $var, $var2 = '' ) {
    $array = [ $arg, $arg2 ];
}

然后,我使用包含的标准运行PHPCS。我使用-s命令行参数来确保我也能看到嗅探代码。我得到了这个输出:

代码语言:javascript
复制
$ 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

然后我挑出你想要保留的信息。可能是这两个可修复的错误:

代码语言:javascript
复制
 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中没有包含用于检查这一点的嗅探,所以如果要强制执行它,就必须编写自定义嗅探。

这显然不是获取这些信息和文档的好方法,但这是我知道哪些嗅探可以用来检查代码(如果有嗅探)的方式,所以我想把它作为一个答案。希望能帮上点忙。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44918498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档