忽略PHP_CodeSniffer中的代码片段

Mad*_*ina 51 php codesniffer

当它被分析时,可以忽略来自php文件的代码的某些部分PHP_CodeSniffer

Ant*_*nna 78

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

<?php
some_code();
// @codingStandardsIgnoreStart
this_will_be_ignored();
// @codingStandardsIgnoreEnd
some_other_code();
Run Code Online (Sandbox Code Playgroud)

它也在文档中描述.

  • 有可能忽略某些规则吗? (8认同)

Mar*_*cka 32

您可以使用组合:@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
Run Code Online (Sandbox Code Playgroud)

  • 是的,这篇文章应该是公认的答案. (2认同)

Fil*_*p Š 5

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

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
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参见PHP_CodeSniffer的文档