Php_codesniffer: How can I exclude this rule?

Created on 21 Nov 2017  路  10Comments  路  Source: squizlabs/PHP_CodeSniffer

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

Most helpful comment

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

All 10 comments

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

Thanks @gsherwood . This fix the error:

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

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>

It's not working for me.

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

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)

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

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

<rule ref="PSR2">
    <exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps" />
</rule>

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

@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

This is off topic but how do I find rules like Methods.CamelCapsMethodName.NotCamelCaps

Was this page helpful?
0 / 5 - 0 ratings