题
当该文件分析时,可以从PHP文件中忽略代码的某些部分 PHP_CodeSniffer
?
解决方案
是的,@CodingStandardSignorestart和@codingStandardSignoreend注释是可能的
<?php
some_code();
// @codingStandardsIgnoreStart
this_will_be_ignored();
// @codingStandardsIgnoreEnd
some_other_code();
也描述了 在文档中。
其他提示
您可以使用该组合: @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
在版本3.2.0之前,php_codesniffer使用了不同的语法来忽略文件中的代码部分。看到 反Veeranna的 和 马丁·维塞尼卡(Martin 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
有关更多详细信息,请参见 php_codesniffer的文档.
不隶属于 StackOverflow