9

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

有没有办法告诉 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
4

2 回答 2

6

在 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 的文档

于 2011-08-13T04:54:38.613 回答
4

在 CodeSniffer 1.3 版中,您可以在 ruleset.xml 文件级别从特定文件中排除特定嗅探。

于 2011-09-29T01:19:43.763 回答