Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to make PHPCS ignore one given rule for a particular file? #1179

Closed
rodrigoprimo opened this issue Sep 28, 2016 · 11 comments
Closed

Comments

@rodrigoprimo
Copy link

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?

@gsherwood
Copy link
Member

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.

@geoffreyvanwyk
Copy link

Put this at the top of the file:

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

@linaori
Copy link

linaori commented Dec 13, 2018

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

@jrfnl
Copy link
Contributor

jrfnl commented Dec 13, 2018

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

  1. You can add the annotation to a file docblock like so:
    <?php
    /**
     * File description
     *
     * @tags
     * @phpcs:disable Standard.Cat.SniffName
     */
  2. You can exclude a file for a particular rule or errorcode from a custom ruleset:
    <rule ref="Standard.Cat.SniffName.ErrorCode">
        <exclude-pattern>/path/to/file\.php</exclude-pattern>
    </rule>

@linaori
Copy link

linaori commented Dec 13, 2018

@jrfnl That seems to work, thanks!

@bkdotcom
Copy link
Contributor

bkdotcom commented May 6, 2020

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

@tiagofrancafernandes
Copy link

Put this at the top of the file:

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

Can you show a example?

@jrfnl
Copy link
Contributor

jrfnl commented Feb 22, 2022

@darius-v
Copy link

darius-v commented Dec 6, 2023

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

  1. You can add the annotation to a file docblock like so:
    <?php
    /**
     * File description
     *
     * @tags
     * @phpcs:disable Standard.Cat.SniffName
     */
    
  2. You can exclude a file for a particular rule or errorcode from a custom ruleset:
    <rule ref="Standard.Cat.SniffName.ErrorCode">
        <exclude-pattern>/path/to/file\.php</exclude-pattern>
    </rule>
    

is the @tags needed? or this is just example of other random tags? I tried without @tags and for me it did not work

@darius-v
Copy link

darius-v commented Dec 6, 2023

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

I also cannot make it work with

SlevomatCodingStandard.TypeHints.DeclareStrictTypes rule unless I add exclude pattern tag in phpcs.xml

@jrfnl
Copy link
Contributor

jrfnl commented Dec 6, 2023

is the @tags needed? or this is just example of other random tags? I tried without @tags and for me it did not work

@tags was just an example of other random tags and should not be needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants