忽略 PHP_CodeSniffer 中的代码片段

发布于 2024-10-05 04:59:56 字数 63 浏览 3 评论 0原文

PHP_CodeSniffer 分析 php 文件时,是否可以忽略其中的某些代码?

It is possible to ignore some parts of code from a php file when it's analyzed by PHP_CodeSniffer?

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

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

发布评论

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

评论(3

尾戒 2024-10-12 04:59:56

是的,可以使用@codingStandardsIgnoreStart和@codingStandardsIgnoreEnd注释

<?php
some_code();
// @codingStandardsIgnoreStart
this_will_be_ignored();
// @codingStandardsIgnoreEnd
some_other_code();

它也被描述在文档中。

Yes it is possible with @codingStandardsIgnoreStart and @codingStandardsIgnoreEnd annotations

<?php
some_code();
// @codingStandardsIgnoreStart
this_will_be_ignored();
// @codingStandardsIgnoreEnd
some_other_code();

It is also described in the documentation.

掀纱窥君容 2024-10-12 04:59:56

您可以使用组合:@codingStandardsIgnoreStart@codingStandardsIgnoreEnd,也可以使用 @codingStandardsIgnoreLine

示例:

<?php

command1();
// @codingStandardsIgnoreStart
command2(); // this line will be ignored by Codesniffer
command3(); // this one too
command4(); // this one too
// @codingStandardsIgnoreEnd

command6();

// @codingStandardsIgnoreLine
command7(); // this line will be ignored by Codesniffer

You can either use the combination: @codingStandardsIgnoreStart and @codingStandardsIgnoreEnd or you can use @codingStandardsIgnoreLine.

Example:

<?php

command1();
// @codingStandardsIgnoreStart
command2(); // this line will be ignored by Codesniffer
command3(); // this one too
command4(); // this one too
// @codingStandardsIgnoreEnd

command6();

// @codingStandardsIgnoreLine
command7(); // this line will be ignored by Codesniffer
尐偏执 2024-10-12 04:59:56

在 3.2.0 版本之前,PHP_CodeSniffer 使用不同的语法来忽略文件中的部分代码。请参阅Anti Veeranna'sMartin Vseticka 的 答案。旧语法将在 4.0 版本中删除

PHP_CodeSniffer 现在使用 // phpcs:disable// phpcs:enable 注释来忽略部分文件和 < code>// 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. See the Anti Veeranna's and Martin Vseticka's answers. The old syntax will be removed in version 4.0

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.

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