bcmcfc
bcmcfc

Reputation: 26825

PHPCS sniff names don't seem to match to PHPCBF fixes

I have some sniffs defined in a phpcs.xml, like so:

<rule ref="PSR2">
    <exclude name="Generic.ControlStructures.InlineControlStructure.NotAllowed"/>
</rule>

My intention is to gradually tighten up the CS rules (there are many more excludes) in a granular fashion.

However, when I try to run CBF it fails with the same sniff name:

$ ./vendor/bin/phpcbf --standard=PEAR --sniffs=Generic.Sniffs.ControlStructures.InlineControlStructure.NotAllowed path/to/code

Producing:

ERROR: The specified sniff code "Generic.Sniffs.ControlStructures.InlineControlStructure.NotAllowed" is invalid

How is it invalid if it works for phpcs? I can't seem to find a mapping or naming convention mapping in the docs.

(I've tried with "PEAR", "PSR2" and "Generic" as standards and I've also tried it just as Generic.ControlStructures.InlineControlStructure.NotAllowed)

Upvotes: 3

Views: 1655

Answers (2)

kenorb
kenorb

Reputation: 166919

Make sure the standard is registered by:

phpcs -i

otherwise add it by:

phpcs --config-set installed_paths path/to/coder_sniffer/standards

When it is registered, you can list all your sniffs by:

phpcs -e --standard=PEAR

In my phpcs this sniff is called as: Generic.ControlStructures.InlineControlStructure.

$ phpcs -e --standard=PEAR | grep ControlStructures.InlineControlStructure
  Generic.ControlStructures.InlineControlStructure

Upvotes: 2

Greg Sherwood
Greg Sherwood

Reputation: 7232

When you run PHPCBF, run it with the exact same arguments that you do for PHPCS. PHPCBF will read your ruleset.xml file as normal and process all the rules found within. Anything you've exclude from being reported as an error will also be excluded from being fixed. If you have any config variables set, it will also read those. PHPCBF and PHPCS share the same codebase.

The specific error you've got is that Generic.Sniffs.ControlStructures.InlineControlStructure.NotAllowed is not a sniff name. It is a complete error code. You can't exclude specific error codes on the command line. If you used Generic.Sniffs.ControlStructures.InlineControlStructure instead, it would work but it would also exclude all errors in that sniff and not just the specific message you have supplied.

So the best thing to do is create your custom coding standard for PHPCS and make sure all the errors you want fixed are being reported, and that the errors you want excluded are being hidden. When you are happy with that result, just change the phpcs command to phpcbf and let it run.

If you ever find yourself in a situation where you need different rules for checking and fixing, you can mark up the ruleset.xml file to tell PHPCS and PHPCBF which rules to enforce: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml#selectively-applying-rules

Upvotes: 2

Related Questions