Search code examples
phpcoding-stylephpcodesniffer

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


I am configuring phpcs to use WordPress coding standards using a custom ruleset.

My phpcs.xml looks like this

<?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 is a custom ruleset that is defined as part of WordPress coding standard sniffs and I want all messages from this ruleset to be marked as warnings instead of errors.

The Annotated ruleset file in PHP CodeSniffer wiki says that I can do something like this.

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

But this works only if you are including sniffs directly. But in my case I want to make this work for a custom ruleset that is included using the rule tag.

Is it possible to do it?


Solution

  • Edit: Changing the type of message for an entire standard, category, or sniff is now supported in PHPCS from version 3.0.0. So the following ruleset syntax now works:

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

    Original answer:

    No, this is not possible in PHP_CodeSniffer. You can only change the type of specific message codes and not entire rulesets, categories or sniff files.

    If you had control over the WordPress standard, you could use a custom config option to let users specify if the Doc standard should use errors or warnings, but it would be a very specific use case.

    Without that level of control, there isn't really anything you can do in a single run. You'd have to do 2 runs; the 1st with everything except the Doc standard and the second with only the Doc standard. The first would be your rule error list that you aim to correct and the second would be your information list.

    It's not a good solution, but there isn't anything I can think of to do what you are after without a core PHP_CodeSniffer change, which you can suggest here: https://github.com/squizlabs/PHP_CodeSniffer/issues