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.67k stars 1.48k forks source link

Allow exclude patterns for whole standards and categories #686

Closed FabianKoestring closed 9 years ago

FabianKoestring commented 9 years ago

I want to create a custom ruleset that would check PHP Files for compiliance with PSR-2. But it should only test .php files. I use PhpStorm as IDE. PhpStorm supports code sniffer integration but it seems like exclude-pattern doesnt work.

<?xml version="1.0"?>
<ruleset name="Test Ruleset">
    <description>A custom PSR2 coding standard</description>
    <rule ref="PSR2">
        <exclude-pattern>*.phtml</exclude-pattern>
        <exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
        <exclude name="Generic.Files.LineEndings.InvalidEOLChar"/>
    </rule>
</ruleset>

Whats wrong? Is there an error my custom ruleset or is it a problem with PhpStorm?

Thanks

aik099 commented 9 years ago

I recommend to:

  1. create scope in PhpStorm to include PHP files in relevant folders
  2. when configuring inspection select it to be executed in specific scope only

The exclude patterns in PHP_CodeSniffer might only work, when you're passing a folder to be scanned to PHP_CodeSniffer, but PhpStorm is passing absolute file path and in that case PHP_CodeSniffer assumes that file needs to be checked anyway.

gsherwood commented 9 years ago

One other point: You can't include an exclude-pattern tag inside a coding standard, as you have done in your ruleset. That pattern will be assigned to the PSR2 standard, but nothing in PHPCS is actually checking that. Exclude patterns are either global, or specified inside a rule tag for a specific sniff. Examples of both are in here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml

But the specific issue you are getting is also because of what @aik099 has said. If PHPCS is asked to check a file, it will check that file even if the global exclude patterns would normally exclude it.

What you really need is a new PHPCS feature to support the way you've defined your exclude pattern. In the meantime, you'll need to configure PHPStorm as @aik099 has said.

I'll turn this into a feature request so I don't forgot. It might be something I can do in the 3.0 branch.

aik099 commented 9 years ago

After more detailed @gsherwood explanation I now realized, that per-sniff exclude patterns still work when file name is given directly to phpcs or phpcbf. The per-ruleset patterns aren't.

FabianKoestring commented 9 years ago

@gsherwood :+1:

@aik099 Thanks, works!

gsherwood commented 9 years ago

I've added this feature into the 3.0 branch.