1

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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Sudar
  • 18,954
  • 30
  • 85
  • 131

1 Answers1

4

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

Community
  • 1
  • 1
Greg Sherwood
  • 6,992
  • 2
  • 29
  • 24
  • Thanks. I kind of suspected that. At this point in time I think my only guess is to convince the creators of WordPress standard sniffs to provide an option to convert Doc errors into warnings. – Sudar Jun 09 '15 at 08:41
  • I have the same question, and am just coming across this post. Anyone know if this is still the case with PHPCS? Or did they change something which allows us to change all messages from 'errors' to 'warnings?' – cag8f Jan 18 '20 at 10:15
  • This has been possible since version 3.0.0. I've updated the answer to reflect this (although I forgot I was logged out when I did). – Greg Sherwood Jan 19 '20 at 21:50
  • @GregSherwood sorry for the delay--I'm just getting back to this. OK good to know that this is now possible. I've never made such customization to an entire standard though. Where would the `phpcs.xml` file in-question be located? Or might I have to create it myself? – cag8f Feb 16 '20 at 14:16
  • @cag8f You need to create it yourself. Typically, you'd put it in the root directory of your project so that it sets the coding standard rules for the entire project. When you run `phpcs` without telling it what standard to use, it goes looking for the `phpcs.xml` file and uses that. Some more info here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file – Greg Sherwood Feb 17 '20 at 21:02
  • After another month, I'm back with an update :-) I was able to create a custom standard, added your code, and saw that PHPCS for VSCode indeed started flagging all violations as yellow 'warnings' instead of red 'errors,' as expected and as desired. So thanks for all that! – cag8f Mar 15 '20 at 05:19