【发布时间】:2011-05-17 10:02:38
【问题描述】:
当被PHP_CodeSniffer分析时,可以忽略php文件中的某些部分代码吗?
【问题讨论】:
标签: php codesniffer
当被PHP_CodeSniffer分析时,可以忽略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 使用不同的语法来忽略文件中的部分代码。请参阅 Anti Veeranna's 和 Martin Vseticka's 答案。旧语法将在 4.0 版中移除
PHP_CodeSniffer 现在使用 // phpcs:disable 和 // phpcs:enable cmets 忽略部分文件,使用 // phpcs:ignore 忽略一行。
现在,还可以仅禁用或启用特定的错误消息代码、嗅探、嗅探类别或整个编码标准。您应该在 cmets 之后指定它们。如果需要,您可以使用-- 分隔符添加注释,解释为何禁用和重新启用嗅探。
<?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
【讨论】: