如何使用注释抑制PHPCS警告? [英] How can I suppress PHPCS warnings using comments?

查看:149
本文介绍了如何使用注释抑制PHPCS警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的TestMyClass.php在同一文件(单元测​​试类)中有两个类定义,并且PHP Code Sniffer抱怨每个类本身必须在文件中.如何抑制此警告?

My TestMyClass.php has two class definitions in the same file (unit testing class), and PHP Code Sniffer complains about each class must be in a file by itself. How can I suppress this warning?

class MyClassImpl implements MyInterface
{
    // ...
}

class MyClassTest extends \PHPUnit_Framework_TestCase
{
    // ...
}

推荐答案

您可以使用注释使PHP_CodeSniffer忽略文件中的特定文件或行:

You can get PHP_CodeSniffer to ignore specific files or lines in a file using comments: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders

在这种情况下,错误将在您的第二个类定义上生成,因此您必须像这样编写第二个定义:

In this case, the error will be generated on your second class definition, so you'd have to write you second definition like this:

// @codingStandardsIgnoreStart
class MyClassTest extends \PHPUnit_Framework_TestCase
{
    // @codingStandardsIgnoreEnd
    // ...
}

但是,如果不应该检查整个文件,也可以选择忽略整个文件,方法是使用@codingStandardsIgnoreFile注释,或者在命令行上指定排除项(有关信息,请参见上一个链接).

But you might also choose to just ignore the whole file if it is not supposed to be checked, either using the @codingStandardsIgnoreFile comment or by specifying exclusions on the command line (see the previous link for info).

如果发现您经常这样做,并且不想在代码中添加注释,则还可以创建自己的自定义编码标准.假设您现在正在使用PSR2标准,您将创建一个XML文件(例如mystandard.xml)并包含以下内容:

If you find that you do this a lot and you don't want to add comments to your code, you can also create your own custom coding standard. Assuming you are using the PSR2 standard right now, you'd create an XML file (e.g., mystandard.xml) and include the following content:

<?xml version="1.0"?>
<ruleset name="MyStandard">
 <description>My custom coding standard.</description>
 <rule ref="PSR2" />
 <rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
   <severity>0</severity>
 </rule>
</ruleset>

然后像这样运行PHP_CodeSniffer:phpcs --standard=/path/to/mystandard.xml /path/to/code

Then run PHP_CodeSniffer like this: phpcs --standard=/path/to/mystandard.xml /path/to/code

拥有自己的规则集可让您做很多事情,包括更改错误消息,更改消息的严重性或类型(包括来自其他标准的检查)以及设置全局忽略规则.此处的更多信息: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated -ruleset.xml

Having your own ruleset lets you do a lot of things, including changing error messages, changing the severity or type of a message, including checks from other standards, and setting global ignore rules. More info here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml

这篇关于如何使用注释抑制PHPCS警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆