squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

Cannot reference custom sniff via name #3849

Closed datadestroyd closed 1 year ago

datadestroyd commented 1 year ago

I am trying to create a custom standard including a few custom sniffs. I have the following directory structure:

ruleset.xml
Sniffs
    Category
        BarSniff.php

In ruleset.xml I set namespace="MyStandard" and the namespace in BarSniff.php is MyStandard\Sniffs\Category.

In the project to be linted I reference the custom standard in phpcs.xml with <rule ref="../path/to/standard"/>. This works like a charm. However, I don't seem to be able to reference the custom sniffs from phpcs.xml in any way. For instance, if I wanted to exclude BarSniff, I would add <exclude name="MyStandard.Category.Bar"/>, but phpcs complains that the sniff does not exist, even though it is executed when run without the exclude tag. It is also listed under this name if I run phpcs -e. What am I doing wrong?

jrfnl commented 1 year ago

@datadestroyd While you are providing a lot of useful info, I'm still missing the essential info to debug this.

datadestroyd commented 1 year ago

What is the directory in which you have your ruleset.xml file called ?

That was it!! I didn't know that the directory name determined the standard's name. It though it was based on the namespace. Duh. Thanks a bunch!

jrfnl commented 1 year ago

@datadestroyd Glad it helped and you got things working!