Php_codesniffer: Possible to make PHPCS ignore one given rule for a particular file?

Created on 28 Sep 2016  路  6Comments  路  Source: squizlabs/PHP_CodeSniffer

I wonder if it is possible to make PHPCS ignore one given rule for a particular file or line of code? Sometimes for different reasons I have to use // @codingStandardsIgnoreLine but this will make PHPCS completely ignore the line.

As a example, I would like to be able to do something like // @codingStandardsIgnoreLine Generic.WhiteSpace.ScopeIndent to make PHPCS ignore just indentation for the line. Is there a way to do that?

Question

Most helpful comment

@iltar Not sure about the details of your issue, but...

  1. You can add the annotation to a file docblock like so:
    php <?php /** <ul> <li>File description<br /> *</li> <li>@tags</li> <li>@<a href="phpcs:disable">phpcs:disable</a> Standard.Cat.SniffName<br /> */<br />

  2. You can exclude a file for a particular rule or errorcode from a custom ruleset:
    ```xml

    /path/to/file.php

All 6 comments

There is no way to do this using annotations in your files. See issue #604 for a feature request.

You can add exclusions for entire files using a ruleset.xml file. See https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml for an example of hard-coding exclude patterns for specific sniffs. But you cannot do this for individual lines.

Put this at the top of the file:

// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace

@systemovich this does not work for me in combination with slevomat, it will either remove the comment or keep complaining. No other solution than to remove it for me.

@iltar Not sure about the details of your issue, but...

  1. You can add the annotation to a file docblock like so:
    php <?php /** <ul> <li>File description<br /> *</li> <li>@tags</li> <li>@<a href="phpcs:disable">phpcs:disable</a> Standard.Cat.SniffName<br /> */<br />

  2. You can exclude a file for a particular rule or errorcode from a custom ruleset:
    ```xml

    /path/to/file.php

@jrfnl That seems to work, thanks!

Now we just need the ability to do it at the class and method level

2367

Was this page helpful?
0 / 5 - 0 ratings