php 如何使用注释抑制 PHPCS 警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16039362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:25:55  来源:igfitidea点击:

How can I suppress PHPCS warnings using comments?

phpphpunitphpcs

提问by gremo

My TestMyClass.phphas 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?

TestMyClass.php在同一个文件(单元测​​试类)中有两个类定义,而 PHP 代码嗅探器抱怨每个类必须单独存在于一个文件中。我怎样才能抑制这个警告?

class MyClassImpl implements MyInterface
{
    // ...
}

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

回答by Greg Sherwood

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

您可以使用注释让 PHP_CodeSniffer 忽略文件中的特定文件或行: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
    // ...
}

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).

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

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:

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

<?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>

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

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

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

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

回答by Will

The @codingStandardssyntax is deprecated as of version 3.2.0, use phpcsinstead.

@codingStandardsversion 开始不推荐使用语法3.2.0,请phpcs改用。

Some examples:

一些例子:

// phpcs:disable
// phpcs:ignore
// phpcs:enable

See this page for more information: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-parts-of-a-file

有关更多信息,请参阅此页面:https: //github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-parts-of-a-file

回答by piscator

With the following comments you can ignore whatever part of a file you would like.

使用以下注释,您可以忽略您想要的文件的任何部分。

Ignore all the sniffs for the file:

忽略文件的所有嗅探:

<?php
// phpcs:ignoreFile

Ignore only the current and next line:

仅忽略当前行和下一行:

// phpcs:ignore
public function store($myArray)

Ignore the current line:

忽略当前行:

public function store($myArray) // phpcs:ignore

Ignore a certain amount of lines in a file:

忽略文件中一定数量的行:

// phpcs:disable
public function store($myArray): void
{
    if (count($myArray) > 3) {
        // Humongous amount of legacy code you don't want to look at
    }

    return;
}
// phpcs:enable

With // phpcs:ignoreand // phpcs:disableyou can also specify which messages, sniffs, categories or standards you would like to disable, for example:

您还可以使用// phpcs:ignore// phpcs:disable指定要禁用的消息、嗅探、类别或标准,例如:

// phpcs:ignore Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed
$foo = [1,2,3];

If you want to read more about this subject, you can visit the wiki. Some of the examples were taken from there.

如果您想阅读有关此主题的更多信息,可以访问wiki。一些例子取自那里。

回答by Brad

Altho I like the top answer though I'd also suggest this one if you don't care about specific files.

虽然我喜欢最佳答案,但如果您不关心特定文件,我也会建议使用这个答案。

Run this command:

运行此命令:

phpcs --config-set show_warnings 0

phpcs --config-set show_warnings 0

as seen here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#hiding-warnings-by-default

如下所示:https: //github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#hiding-warnings-by-default

A note about setting configs this way is they will get wiped out if you delete the vendor folder or update the library and you will have to set them again.

关于以这种方式设置配置的注意事项是,如果您删除供应商文件夹或更新库,它们将被清除,您将不得不再次设置它们。