Skip to content
Browse files

You can now exclude directories or whole standards in a ruleset XML f…

…ile (request #19731)
  • Loading branch information...
1 parent 1f032d7 commit f172fa8abda3c378b786d5f69ac8890697c81b21 @gsherwood gsherwood committed Mar 27, 2013
Showing with 31 additions and 22 deletions.
  1. +27 −21 CodeSniffer.php
  2. +4 −1 package.xml
View
48 CodeSniffer.php
@@ -540,17 +540,7 @@ public function processRuleset($rulesetPath, $depth=0)
$includedSniffs = array();
$excludedSniffs = array();
- $rulesetDir = dirname($rulesetPath);
- if (in_array($rulesetDir, self::$rulesetDirs) === true) {
- // We've already processed this ruleset.
- if (PHP_CODESNIFFER_VERBOSITY > 1) {
- echo str_repeat("\t", $depth);
- echo "\t* ruleset already processed; skipping *".PHP_EOL;
- }
-
- return array();
- }
-
+ $rulesetDir = dirname($rulesetPath);
self::$rulesetDirs[] = $rulesetDir;
if (is_dir($rulesetDir.'/Sniffs') === true) {
@@ -732,19 +722,35 @@ private function _expandRulesetReference($ref, $rulesetDir, $depth=0)
} else {
// Work out the sniff path.
$parts = explode('.', $ref);
- if (count($parts) < 3) {
- $error = "Referenced sniff $ref does not exist";
- throw new PHP_CodeSniffer_Exception($error);
+
+ if (count($parts) === 1) {
+ // A whole standard?
+ $path = '';
+ } else if (count($parts) === 2) {
+ // A directory of sniffs?
+ $path = '/Sniffs/'.$parts[1];
+ } else {
+ // A single sniff?
+ $path = '/Sniffs/'.$parts[1].'/'.$parts[2].'Sniff.php';
}
- $path = $parts[0].'/Sniffs/'.$parts[1].'/'.$parts[2].'Sniff.php';
- $ref = realpath(dirname(__FILE__).'/CodeSniffer/Standards/'.$path);
+ $ref = realpath(dirname(__FILE__).'/CodeSniffer/Standards/'.$parts[0].$path);
if ($ref === false) {
// The sniff is not locally installed, so check if it is being
- // referenced as a remote sniff outside the install. We do this by
- // looking directly in the passed standard dir to see if it is
- // installed in there.
- $ref = realpath($rulesetDir.'/Sniffs/'.$parts[1].'/'.$parts[2].'Sniff.php');
+ // referenced as a remote sniff outside the install. We do this
+ // by looking through all directories where we have found ruleset
+ // files before, looking for ones for this particular standard,
+ // and seeing if it is in there.
+ foreach (self::$rulesetDirs as $dir) {
+ if (basename($dir) !== $parts[0]) {
+ continue;
+ }
+
+ $ref = realpath($dir.$path);
+ if ($ref !== false) {
+ break;
+ }
+ }
}
if (PHP_CODESNIFFER_VERBOSITY > 1) {
@@ -776,7 +782,7 @@ private function _expandRulesetReference($ref, $rulesetDir, $depth=0)
}
} else {
if (is_file($ref) === false) {
- $error = "Referenced sniff $ref does not exist";
+ $error = "Referenced sniff \"$ref\" does not exist";
throw new PHP_CodeSniffer_Exception($error);
}
View
5 package.xml
@@ -31,8 +31,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- May mean that sniffs that were previously ignored are now being included when importing external rulesets
-- Ruleset processing output can be seen by using the -vv command line argument
-- Internal sniff registering functions have all changed, so please review custom scripts
- - You can now pass multiple coding standards on the command line, comma separated
+ - You can now pass multiple coding standards on the command line, comma separated (request #19144)
-- Works with built-in or custom standards and rulesets, or a mix of both
+ - You can now exclude directories or whole standards in a ruleset XML file (request #19731)
+ -- e.g., exclude "Generic.Commenting" or just "Generic"
+ -- You can also pass in a path to a directory instead, if you know it
- Added Generic LowerCaseKeywordSniff to ensure all PHP keywords are defined in lowercase
-- The PSR2 and Squiz standards now use this sniff
- Added Generic SAPIUsageSniff to ensure the PHP_SAPI constant is used instead of php_sapi_name() (request #19863)

0 comments on commit f172fa8

Please sign in to comment.
Something went wrong with that request. Please try again.