phpcs -为自定义规则集的所有片段设置默认消息类型为警告


phpcs - Set default message type to warning for all snips from a custom ruleset

我正在配置phpcs使用使用自定义规则集的WordPress编码标准。

我的phpcs.xml看起来像这样

<?xml version="1.0"?>
<ruleset name="Bulk Delete">
    <description>Bulk Delete coding standard</description>
    <file>./</file>
    <!--Docs issues should be shown as warnings -->
    <rule ref="WordPress-Docs">
        <type>warning</type>
    </rule>
</ruleset>

WordPress-Docs是一个自定义规则集,定义为WordPress编码标准嗅嗅的一部分,我希望从这个规则集的所有消息被标记为warnings而不是errors

PHP CodeSniffer wiki中的注释规则集文件说我可以这样做。

<!--
    Here we are including a specific sniff but also changing
    the error message of a specific message inside the sniff.
    Note that the specific code for the message, which is
    CommentFound in this case, is defined by the sniff developer.
    You can display these codes by using the -s command line
    argument when checking a file.
    Also note that this message has a variable inside it,
    which is why it is important that sniffs use a printf style
    format for their error messages.
    We also drop the severity of this message from the
    default value (5) so that it is hidden by default. It can be
    displayed by setting the minimum severity on the PHP_CodeSniffer
    command line. This is great if you want to use some messages
    only in code reviews and not have them block code commits.
 -->
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <message>Please review this TODO comment: %s</message>
  <severity>3</severity>
 </rule>

但这只在直接包含嗅探时才有效。但在我的情况下,我想使这个工作的自定义规则集,包括使用rule标签。

这可能吗?

编辑:从3.0.0版开始,PHPCS现在支持更改整个标准、类别或嗅嗅的消息类型。所以下面的规则集语法现在可以工作了:

<rule ref="WordPress-Docs">
    <type>warning</type>
</rule>

原始回答:不,这在PHP_CodeSniffer中是不可能的。您只能更改特定消息代码的类型,而不能更改整个规则集、类别或嗅探文件。

如果你可以控制WordPress标准,你可以使用自定义配置选项让用户指定Doc标准是否应该使用错误或警告,但这将是一个非常特殊的用例。

如果没有这种级别的控制,在一次运行中你真的什么都做不了。你需要跑2次;第一种是除了Doc标准之外的所有内容,第二种是只有Doc标准。第一个是您要纠正的规则错误列表,第二个是您的信息列表。

这不是一个很好的解决方案,但没有什么我能想到做你之后没有核心PHP_CodeSniffer的变化,你可以在这里建议:https://github.com/squizlabs/PHP_CodeSniffer/issues