Squiz Labs Blog - The latest news from the R&D division of Squiz®

Subscribe to our RSS feeds

Rule-based exclude patterns

PHP_CodeSniffer has been able to exclude files and directories while checking large code bases for quite some time, but it has always been an "all or nothing" feature. If the file is excluded then it will not generate any errors or warnings.

A feature to exclude specific sniffs for specific files has been requested a couple of times, but the suggested implementations have always been around the idea of adding comments into your source code. I'm not a big fan of annotating your source code for external tools, so I've never really looked into this feature.

But now that PHP_CodeSniffer uses ruleset.xml files, a recent feature request prompted me to take another look. A it turns out, the ruleset format is the perfect for defining these sort of custom rules.

I've just committed this feature into the PHP_CodeSniffer SVN repository. The documentation wont be updated until the weekend, so here is some information about how to add it to your ruleset.xml file now:

<!--
   You can also hard-code ignore patterns for specific sniffs,
   a feature not available on the command line.

   The code here will hide all messages from the Squiz DoubleQuoteUsage
   sniff for files that match either of the two exclude patterns.
-->
<rule ref="Squiz.Strings.DoubleQuoteUsage">
   <exclude-pattern>*/tests/*</exclude-pattern>
   <exclude-pattern>*/data/*</exclude-pattern>
</rule>

<!--
   You can also be more specific and just exclude some messages.

   The code here will just hide the ContainsVar error generated by the
   Squiz DoubleQuoteUsage sniff for files that match either of the two
   exclude patterns.
-->
<rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
   <exclude-pattern>*/tests/*</exclude-pattern>
   <exclude-pattern>*/data/*</exclude-pattern>
</rule> 

Squiz Labs

R & D division of Squiz Pty Ltd

Open source web experience management solutions

Squiz Labs is the research and development division of Squiz, the company behind the Squiz Suite of web experience management and development tools.

Our PHP and JavaScript developers are responsible for producing innovative enterprise-quality software while our interface designers and testing team ensure our products both look great and are easy to use.