Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPCS Limit to Filetype

I'm trying to create a custom ruleset for a particular frameworks "guidelines". However, I want to be able to limit sniffs to only be relevant to a .php or .phtml filetype.

Is this possible within to have Sniffs to only use or ignore a defined filetype, or would I need to do this check within the sniffs process() method?

like image 513
Adam Paterson Avatar asked May 21 '14 19:05

Adam Paterson


1 Answers

You can specify exclude patterns using regular expressions inside a ruleset. By using a negative lookahead (or behind if you prefer) you can limit a specific sniff or error message to files that match the pattern.

This examples only runs the DoubleQuoteUsage sniff on .phtml files only:

<rule ref="Squiz.Strings.DoubleQuoteUsage">
  <exclude-pattern>*\.(?!phtml$)</exclude-pattern>
</rule>

But the current PHPCS releases use | as the delimiter for regular expressions, and escaping that character doesn't seem to work in PHP. I've just committed a change for this in the phpcs-fixer branch (the 2.x line of releases), allowing you to do this:

<rule ref="Squiz.Strings.DoubleQuoteUsage">
  <exclude-pattern>*\.(?!(php$|phtml$))</exclude-pattern>
</rule>

If you want to give that a go, you can clone the git repo, checkout the phpcs-fixer branch and run the code directly. Or you can require 2.0.*@dev via composer.

If not, you will need to do the filename check yourself in your sniff's process() method.

like image 65
Greg Sherwood Avatar answered Oct 11 '22 03:10

Greg Sherwood