【问题标题】:Ignore specific warnings with PHP_CodeSniffer使用 PHP_CodeSniffer 忽略特定警告
【发布时间】:2011-08-13 02:02:19
【问题描述】:

我正在开发一个密码工具模块,其中一部分正在使用 base64 编码/解码。结果,出于显而易见的原因,我有许多变量,其中包括术语“base64”。问题是当我运行 PHP_CodeSniffer 工具时,它会抛出警告:“变量“...64”包含数字,但不鼓励这样做”

有没有办法告诉 PHP_CodeSniffer 忽略这个特定文件的这些警告?我确信有充分的理由避免使用数字,但在这种情况下,我宁愿使用 'base64' 而不是 'baseSixtyFour'...

这就是我运行 PHP_CodeSniffer 的方式:

valorin@gandalf:~/workspace/library$ phpcs --standard=ZEND ./Tools/

FILE: /home/valorin/workspace/library/Tools/Password.php
--------------------------------------------------------------------------------
FOUND 0 ERROR(S) AND 6 WARNING(S) AFFECTING 5 LINE(S)
--------------------------------------------------------------------------------
  38 | WARNING | Variable "_bEncryptionBase64" contains numbers but this is
     |         | discouraged
  94 | WARNING | Variable "_bEncryptionBase64" contains numbers but this is
     |         | discouraged
  94 | WARNING | Variable "base64" contains numbers but this is discouraged
  95 | WARNING | Variable "base64" contains numbers but this is discouraged
 210 | WARNING | Variable "bEncryptionBase64" contains numbers but this is
     |         | discouraged
 251 | WARNING | Variable "bEncryptionBase64" contains numbers but this is
     |         | discouraged
--------------------------------------------------------------------------------

Time: 1 second, Memory: 7.50Mb

【问题讨论】:

    标签: php codesniffer


    【解决方案1】:

    在 3.2.0 版本之前,PHP_CodeSniffer 使用不同的语法来忽略文件中将在 4.0 版本中删除的部分代码

    旧语法:

    // @codingStandardsIgnoreStart
    
    /* put your bad code here! */    
    
    // @codingStandardsIgnoreEnd
    

    这需要 1.2 或更高版本。

    新语法:

    PHP_CodeSniffer 现在使用 // phpcs:disable// phpcs:enable cmets 忽略部分文件,使用 // phpcs:ignore 忽略一行。

    现在,还可以仅禁用或启用特定的错误消息代码、嗅探、嗅探类别或整个编码标准。您应该在 cmets 之后指定它们。如果需要,您可以使用-- 分隔符添加注释,解释为何禁用和重新启用嗅探。

    <?php
    
    /* Example: Ignoring parts of file for all sniffs */
    $xmlPackage = new XMLPackage;
    // phpcs:disable
    $xmlPackage['error_code'] = get_default_error_code_value();
    $xmlPackage->send();
    // phpcs:enable
    
    /* Example: Ignoring parts of file for only specific sniffs */
    // phpcs:disable Generic.Commenting.Todo.Found
    $xmlPackage = new XMLPackage;
    $xmlPackage['error_code'] = get_default_error_code_value();
    // TODO: Add an error message here.
    $xmlPackage->send();
    // phpcs:enable
    
    /* Example: Ignoring next line */
    // phpcs:ignore
    $foo = [1,2,3];
    bar($foo, false);
    
    /* Example: Ignoring current line */
    $foo = [1,2,3]; // phpcs:ignore
    bar($foo, false);
    
    /* Example: Ignoring one line for only specific sniffs */
    // phpcs:ignore Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed
    $foo = [1,2,3];
    bar($foo, false);
    
    /* Example: Optional note */ 
    // phpcs:disable PEAR,Squiz.Arrays -- this isn't our code
    $foo = [1,2,3];
    bar($foo,true);
    // phpcs:enable PEAR.Functions.FunctionCallSignature -- check function calls again
    bar($foo,false);
    // phpcs:enable -- this is out code again, so turn everything back on
    

    更多详情请见PHP_CodeSniffer's documentation

    【讨论】:

    • 问题在于,我需要将它们放在变量的每次使用中(这很常见),所以这是一个非常混乱的解决方案......
    【解决方案2】:

    在 CodeSniffer 1.3 版中,您可以在 ruleset.xml 文件级别上exclude specific sniffs from specific files

    【讨论】:

    • 更好的解决方案,因为您不会使用嗅探指令污染源代码。您确实可以指定一条错误消息。
    • 是的,但是您如何确定单个错误消息的名称?我什至看过其中一个,无法弄清楚。 (ScopeClosingBraceSniff.php)
    • @AmigableClarkKant - 我实际上并没有使用特定的消息排除逻辑(我只在嗅探级别排除),但查看 ScopeClosingBraceSniff.php 我看到以下几行:$phpcsFile-> addError($error, $scopeEnd, 'ContentBefore');和 $phpcsFile->addError($error, $scopeEnd, 'Indent', $data);,我想这将通过 ref="Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore" 和 ref="Squiz.WhiteSpace.ScopeClosingBrace .Indent”,分别。
    • @Peter,这是有道理的。不过,这些东西可以更好地记录下来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 2014-03-15
    • 2019-05-26
    • 2018-06-17
    • 1970-01-01
    • 2013-06-16
    相关资源
    最近更新 更多