使用PHP_CodeSniffer忽略特定警告

时间:2011-08-13 02:02:20

标签: php codesniffer

我正在使用密码工具模块,其中一部分是使用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

2 个答案:

答案 0 :(得分:4)

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

旧语法:

// @codingStandardsIgnoreStart

/* put your bad code here! */    

// @codingStandardsIgnoreEnd

这需要1.2或更高版本。

新语法:

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

现在,也可以仅禁用或启用特定的错误消息代码,嗅探,嗅探类别或整个编码标准。您应该在评论后指定它们。如果需要,您可以添加说明,说明使用--分隔符禁用和重新启用嗅探的原因。

<?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

答案 1 :(得分:4)

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