Permalink
Browse files

All ignore patterns have been reverted to being checked against the a…

…bsolute path of a file. Patterns can be specified to be relative in a rulset.xml file, but nowhere else.
  • Loading branch information...
1 parent a172ccb commit dd33c264adbd9c11827b621a9d560920f1ec95d3 @gsherwood gsherwood committed Oct 11, 2012
Showing with 45 additions and 21 deletions.
  1. +34 −18 CodeSniffer.php
  2. +4 −1 CodeSniffer/CLI.php
  3. +3 −1 CodeSniffer/File.php
  4. +3 −0 package.xml
  5. +1 −1 scripts/phpcs-svn-pre-commit
View
@@ -962,13 +962,21 @@ public function populateCustomRules($standard=null)
$this->ignorePatterns[$code] = array();
}
- $this->ignorePatterns[$code][] = (string) $pattern;
+ if (isset($pattern['type']) === false) {
+ $pattern['type'] = 'absolute';
+ }
+
+ $this->ignorePatterns[$code][(string) $pattern] = (string) $pattern['type'];
}
}//end foreach
// Process custom ignore pattern rules.
foreach ($ruleset->{'exclude-pattern'} as $pattern) {
- $this->ignorePatterns[] = (string) $pattern;
+ if (isset($pattern['type']) === false) {
+ $pattern['type'] = 'absolute';
+ }
+
+ $this->ignorePatterns[(string) $pattern] = (string) $pattern['type'];
}
}//end populateCustomRules()
@@ -1116,21 +1124,14 @@ public function getFilesToProcess($paths, $local=false)
continue;
}
- $relativePath = $file->getPathname();
-
- if (strpos($relativePath, $path) === 0) {
- // The +1 cuts off the directory separator as well.
- $relativePath = substr($relativePath, (strlen($path) + 1));
- }
-
- if ($this->shouldProcessFile($relativePath) === false) {
+ if ($this->shouldProcessFile($file->getPathname(), $path) === false) {
continue;
}
$files[] = $file->getPathname();
}//end foreach
} else {
- if ($this->shouldIgnoreFile($path) === true) {
+ if ($this->shouldIgnoreFile($path, dirname($path)) === true) {
continue;
}
@@ -1148,11 +1149,12 @@ public function getFilesToProcess($paths, $local=false)
*
* Checks both file extension filters and path ignore filters.
*
- * @param string $path The path to the file being checked.
+ * @param string $path The path to the file being checked.
+ * @param string $basedir The directory to use for relative path checks.
*
* @return bool
*/
- public function shouldProcessFile($path)
+ public function shouldProcessFile($path, $basedir)
{
// Check that the file's extension is one we are checking.
// We are strict about checking the extension and we don't
@@ -1178,7 +1180,7 @@ public function shouldProcessFile($path)
}
// If the file's path matches one of our ignore patterns, skip it.
- if ($this->shouldIgnoreFile($path) === true) {
+ if ($this->shouldIgnoreFile($path, $basedir) === true) {
return false;
}
@@ -1190,13 +1192,20 @@ public function shouldProcessFile($path)
/**
* Checks filtering rules to see if a file should be ignored.
*
- * @param string $path The path to the file being checked.
+ * @param string $path The path to the file being checked.
+ * @param string $basedir The directory to use for relative path checks.
*
* @return bool
*/
- public function shouldIgnoreFile($path)
+ public function shouldIgnoreFile($path, $basedir)
{
- foreach ($this->ignorePatterns as $pattern) {
+ $relativePath = $path;
+ if (strpos($path, $basedir) === 0) {
+ // The +1 cuts off the directory separator as well.
+ $relativePath = substr($path, (strlen($basedir) + 1));
+ }
+
+ foreach ($this->ignorePatterns as $pattern => $type) {
if (is_array($pattern) === true) {
// A sniff specific ignore pattern.
continue;
@@ -1215,7 +1224,14 @@ public function shouldIgnoreFile($path)
}
$pattern = strtr($pattern, $replacements);
- if (preg_match("|{$pattern}|i", $path) === 1) {
+
+ if ($type === 'relative') {
+ $testPath = $relativePath;
+ } else {
+ $testPath = $path;
+ }
+
+ if (preg_match("|{$pattern}|i", $testPath) === 1) {
return true;
}
}//end foreach
View
@@ -407,10 +407,13 @@ public function processLongArgument($arg, $pos, $values)
} else if (substr($arg, 0, 7) === 'ignore=') {
// Split the ignore string on commas, unless the comma is escaped
// using 1 or 3 slashes (\, or \\\,).
- $values['ignored'] = preg_split(
+ $ignored = preg_split(
'/(?<=(?<!\\\\)\\\\\\\\),|(?<!\\\\),/',
substr($arg, 7)
);
+ foreach ($ignored as $pattern) {
+ $values['ignored'][$pattern] = 'absolute';
+ }
} else if (substr($arg, 0, 10) === 'generator=') {
$values['generator'] = substr($arg, 10);
} else if (substr($arg, 0, 9) === 'encoding=') {
View
@@ -467,7 +467,9 @@ public function start($contents=null)
if (isset($parts[3]) === true) {
$source = $parts[0].'.'.$parts[2].'.'.substr($parts[3], 0, -5);
$patterns = $this->phpcs->getIgnorePatterns($source);
- foreach ($patterns as $pattern) {
+ foreach ($patterns as $pattern => $type) {
+ // While there is support for a type of each pattern
+ // (absolute or relative) we don't actually support it here.
$replacements = array(
'\\,' => ',',
'*' => '.*',
View
@@ -26,6 +26,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
</stability>
<license uri="http://matrix.squiz.net/developer/tools/php_cs/licence">BSD License</license>
<notes>
+ - All ignore patterns have been reverted to being checked against the absolute path of a file
+ -- Patterns can be specified to be relative in a rulset.xml file, but nowhere else
+ -- e.g., [exclude-pattern type="relative"]^tests/*[/exclude-pattern] (with angle brackets, not square brackets)
- Sniffs inside PHP 5.3 namespaces are now supported, along with the existing underscore-style emulated namespaces
-- For example: namespace MyStandard\Sniffs\Arrays; class ArrayDeclarationSniff implements \PHP_CodeSniffer_Sniff { ...
-- Thanks to Till Klampaeckel for the patch
@@ -159,7 +159,7 @@ class PHP_CodeSniffer_SVN_Hook extends PHP_CodeSniffer_CLI
// We need to check ignore rules ourself because they are
// not checked when processing a single file.
- if ($phpcs->shouldProcessFile($path) === false) {
+ if ($phpcs->shouldProcessFile($path, dirname($path)) === false) {
continue;
}

0 comments on commit dd33c26

Please sign in to comment.