使用 PHP_CodeSniffer 忽略特定警告

发布于 2024-11-29 18:47:14 字数 1366 浏览 1 评论 0原文

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

I am working on a password tools module, and part of it is using base64 Encoding/Decoding. As a result I have a number of variables which include the term 'base64' for obvious reasons. The problem is that when I run the PHP_CodeSniffer tool, it throws warnings: "Variable "...64" contains numbers but this is discouraged".

Is there any way to tell PHP_CodeSniffer to ignore these warnings for this specific file? I'm sure there is a good reason to avoid numbers, but in this case I'd rather use 'base64' than 'baseSixtyFour'...

This is how I am running 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友欢 2024-12-06 18:47:14

在版本 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 文档

Before version 3.2.0, PHP_CodeSniffer used different syntax for ignoring parts of code from file which will be removed in version 4.0

Old syntax:

// @codingStandardsIgnoreStart

/* put your bad code here! */    

// @codingStandardsIgnoreEnd

This requires version 1.2 or later.

New syntax:

PHP_CodeSniffer is now using // phpcs:disable and // phpcs:enable comments to ignore parts of files and // phpcs:ignore to ignore one line.

Now, it is also possible to only disable or enable specific error message codes, sniffs, categories of sniffs, or entire coding standards. You should specify them after comments. If required, you can add a note explaining why sniffs are being disable and re-enabled by using the -- separator.

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

For more details see PHP_CodeSniffer's documentation.

桃扇骨 2024-12-06 18:47:14

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

In CodeSniffer version 1.3 you can exclude specific sniffs from specific files at the level of the ruleset.xml file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文