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

How can I exclude this rule? #1755

Closed
shivapoudel opened this issue Nov 21, 2017 · 9 comments
Labels

Comments

@shivapoudel
Copy link

@shivapoudel shivapoudel commented Nov 21, 2017

I wanted to use class name WooCommerce_eSewa but PHPCS shows this error:

FILE: ...t\plugins\woocommerce-esewa\includes\class-woocommerce-esewa.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 20 | ERROR | Class name is not valid; consider WooCommerce_ESewa
    |       | instead
----------------------------------------------------------------------
@gsherwood

This comment has been minimized.

Copy link
Member

@gsherwood gsherwood commented Nov 21, 2017

If you are using a coding standard that enforces this rule but you don't want the rule applied, you'll need to create your own coding standard by writing a ruleset.xml file that excludes this particular check.

If you use the -s CLI option, PHPCS will show you the code for each error. In this case, it is probably PEAR.NamingConventions.ValidClassName.Invalid but you would have to confirm that.

You don't say what coding standard you are using, so I'll assume it is PEAR (the default one).

To exclude this rule, you'd create a ruleset.xml file with the following content:

<?xml version="1.0"?>
<ruleset name="MyStandard">
    <description>My custom coding standard.</description>
    <rule ref="PEAR">
        <exclude name="PEAR.NamingConventions.ValidClassName.Invalid" />
    </rule>
</ruleset>

Then you run PHPCS using this file: phpcs /path/to/code --standard=/path/to/ruleset.xml

If you instead name the file phpcs.xml and put it in the root dir of your project, PHPCS will auto-detect and use this custom standard when checking any code inside your project. This allows you to just run PHPCS like this: phpcs /path/to/code but still have it use your customised PEAR standard.

Once you have your own ruleset, you can customise things a lot more. See this doc for example: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml

@shivapoudel

This comment has been minimized.

Copy link
Author

@shivapoudel shivapoudel commented Nov 22, 2017

Thanks @gsherwood . This fix the error:

<rule ref="WordPress">
	<exclude name="PEAR.NamingConventions.ValidClassName.Invalid" />
</rule>
@andreshg112

This comment has been minimized.

Copy link

@andreshg112 andreshg112 commented Oct 26, 2018

Do I have to add Invalid at the end in order to make it work? It's not working for me.

<?xml version="1.0"?>
<ruleset name="PSR2">
    <description>The PSR2 coding standard.</description>
    <rule ref="PSR2">
        <exclude name="Generic.NamingConventions.CamelCapsFunctionName.Invalid" />
    </rule>
    <file>app/</file>
    <exclude-pattern>vendor</exclude-pattern>
    <exclude-pattern>resources</exclude-pattern>
    <exclude-pattern>database/</exclude-pattern>
    <exclude-pattern>storage/</exclude-pattern>
    <exclude-pattern>node_modules/</exclude-pattern>
</ruleset>
@gsherwood

This comment has been minimized.

Copy link
Member

@gsherwood gsherwood commented Oct 26, 2018

It's not working for me.

@andreshg112 What's not working? What version of PHPCS? What does the output (with -s) look like?

@andreshg112

This comment has been minimized.

Copy link

@andreshg112 andreshg112 commented Oct 26, 2018

This is just one part of the output for the command phpcs -s:

318 | ERROR   | [ ] Method name "AdminTiposDocumentosController::hook_after_edit" is not in camel caps format
     |         |     (PSR1.Methods.CamelCapsMethodName.NotCamelCaps)
@andreshg112

This comment has been minimized.

Copy link

@andreshg112 andreshg112 commented Oct 26, 2018

This is the output for just one file. The phpcs.xml file is still the same and It's placed in the root directory of the project.

andreshg112@andreshg112-HP-240-G4-Notebook-PC:/opt/lampp/htdocs/transito$ phpcs app/Http/Controllers/AdminAgentesController.php

FILE: /opt/lampp/htdocs/transito/app/Http/Controllers/AdminAgentesController.php
--------------------------------------------------------------------------------------------------------------------------
FOUND 17 ERRORS AND 2 WARNINGS AFFECTING 19 LINES
--------------------------------------------------------------------------------------------------------------------------
  89 | WARNING | [ ] Line exceeds 120 characters; contains 192 characters
  90 | WARNING | [ ] Line exceeds 120 characters; contains 196 characters
 233 | ERROR   | [x] Function closing brace must go on the next line following the body; found 1 blank lines before brace
 247 | ERROR   | [x] Function closing brace must go on the next line following the body; found 1 blank lines before brace
 256 | ERROR   | [ ] Method name "AdminAgentesController::hook_query_index" is not in camel caps format
 268 | ERROR   | [ ] Method name "AdminAgentesController::hook_row_index" is not in camel caps format
 280 | ERROR   | [ ] Method name "AdminAgentesController::hook_before_add" is not in camel caps format
 284 | ERROR   | [x] Function closing brace must go on the next line following the body; found 1 blank lines before brace
 293 | ERROR   | [ ] Method name "AdminAgentesController::hook_after_add" is not in camel caps format
 297 | ERROR   | [x] Function closing brace must go on the next line following the body; found 1 blank lines before brace
 307 | ERROR   | [ ] Method name "AdminAgentesController::hook_before_edit" is not in camel caps format
 311 | ERROR   | [x] Function closing brace must go on the next line following the body; found 1 blank lines before brace
 320 | ERROR   | [ ] Method name "AdminAgentesController::hook_after_edit" is not in camel caps format
 324 | ERROR   | [x] Function closing brace must go on the next line following the body; found 1 blank lines before brace
 333 | ERROR   | [ ] Method name "AdminAgentesController::hook_before_delete" is not in camel caps format
 337 | ERROR   | [x] Function closing brace must go on the next line following the body; found 1 blank lines before brace
 346 | ERROR   | [ ] Method name "AdminAgentesController::hook_after_delete" is not in camel caps format
 350 | ERROR   | [x] Function closing brace must go on the next line following the body; found 1 blank lines before brace
 354 | ERROR   | [x] The closing brace for the class must go on the next line after the body
--------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 9 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------------------------

Time: 171ms; Memory: 6Mb
@gsherwood

This comment has been minimized.

Copy link
Member

@gsherwood gsherwood commented Oct 26, 2018

If you want to exclude PSR1.Methods.CamelCapsMethodName.NotCamelCaps, then you need to use:

<rule ref="PSR2">
    <exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps" />
</rule>
@alessandro-candon

This comment has been minimized.

Copy link

@alessandro-candon alessandro-candon commented Apr 11, 2019

Hello,
If I try to exclude the rule by commandline it doesn't work, do you know why?

phpcs --standard=PSR2 --extensions=php --encoding=utf8 --report=full --exclude=PSR1.Methods.CamelCapsMethodName.NotCamelCaps tests

Because in my tests I use another way to nameing methods test_myTest()

Thank you

@gsherwood

This comment has been minimized.

Copy link
Member

@gsherwood gsherwood commented Apr 12, 2019

@alessandro-candon You can only exclude entire sniffs (3-part codes) on the command line, not individual error messages (4-part codes). Try this:

phpcs --standard=PSR2 --extensions=php --encoding=utf8 --report=full --exclude=PSR1.Methods.CamelCapsMethodName tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.