Skip to content

Commit

Permalink
All ignore patterns have been reverted to being checked against the a…
Browse files Browse the repository at this point in the history
…bsolute path of a file. Patterns can be specified to be relative in a rulset.xml file, but nowhere else.
  • Loading branch information
gsherwood committed Oct 11, 2012
1 parent a172ccb commit dd33c26
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
52 changes: 34 additions & 18 deletions CodeSniffer.php
Expand Up @@ -962,13 +962,21 @@ public function populateCustomRules($standard=null)
$this->ignorePatterns[$code] = array(); $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 }//end foreach


// Process custom ignore pattern rules. // Process custom ignore pattern rules.
foreach ($ruleset->{'exclude-pattern'} as $pattern) { 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() }//end populateCustomRules()
Expand Down Expand Up @@ -1116,21 +1124,14 @@ public function getFilesToProcess($paths, $local=false)
continue; continue;
} }


$relativePath = $file->getPathname(); if ($this->shouldProcessFile($file->getPathname(), $path) === false) {

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) {
continue; continue;
} }


$files[] = $file->getPathname(); $files[] = $file->getPathname();
}//end foreach }//end foreach
} else { } else {
if ($this->shouldIgnoreFile($path) === true) { if ($this->shouldIgnoreFile($path, dirname($path)) === true) {
continue; continue;
} }


Expand All @@ -1148,11 +1149,12 @@ public function getFilesToProcess($paths, $local=false)
* *
* Checks both file extension filters and path ignore filters. * 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 * @return bool
*/ */
public function shouldProcessFile($path) public function shouldProcessFile($path, $basedir)
{ {
// Check that the file's extension is one we are checking. // Check that the file's extension is one we are checking.
// We are strict about checking the extension and we don't // We are strict about checking the extension and we don't
Expand All @@ -1178,7 +1180,7 @@ public function shouldProcessFile($path)
} }


// If the file's path matches one of our ignore patterns, skip it. // 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; return false;
} }


Expand All @@ -1190,13 +1192,20 @@ public function shouldProcessFile($path)
/** /**
* Checks filtering rules to see if a file should be ignored. * 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 * @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) { if (is_array($pattern) === true) {
// A sniff specific ignore pattern. // A sniff specific ignore pattern.
continue; continue;
Expand All @@ -1215,7 +1224,14 @@ public function shouldIgnoreFile($path)
} }


$pattern = strtr($pattern, $replacements); $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; return true;
} }
}//end foreach }//end foreach
Expand Down
5 changes: 4 additions & 1 deletion CodeSniffer/CLI.php
Expand Up @@ -407,10 +407,13 @@ public function processLongArgument($arg, $pos, $values)
} else if (substr($arg, 0, 7) === 'ignore=') { } else if (substr($arg, 0, 7) === 'ignore=') {
// Split the ignore string on commas, unless the comma is escaped // Split the ignore string on commas, unless the comma is escaped
// using 1 or 3 slashes (\, or \\\,). // using 1 or 3 slashes (\, or \\\,).
$values['ignored'] = preg_split( $ignored = preg_split(
'/(?<=(?<!\\\\)\\\\\\\\),|(?<!\\\\),/', '/(?<=(?<!\\\\)\\\\\\\\),|(?<!\\\\),/',
substr($arg, 7) substr($arg, 7)
); );
foreach ($ignored as $pattern) {
$values['ignored'][$pattern] = 'absolute';
}
} else if (substr($arg, 0, 10) === 'generator=') { } else if (substr($arg, 0, 10) === 'generator=') {
$values['generator'] = substr($arg, 10); $values['generator'] = substr($arg, 10);
} else if (substr($arg, 0, 9) === 'encoding=') { } else if (substr($arg, 0, 9) === 'encoding=') {
Expand Down
4 changes: 3 additions & 1 deletion CodeSniffer/File.php
Expand Up @@ -467,7 +467,9 @@ public function start($contents=null)
if (isset($parts[3]) === true) { if (isset($parts[3]) === true) {
$source = $parts[0].'.'.$parts[2].'.'.substr($parts[3], 0, -5); $source = $parts[0].'.'.$parts[2].'.'.substr($parts[3], 0, -5);
$patterns = $this->phpcs->getIgnorePatterns($source); $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( $replacements = array(
'\\,' => ',', '\\,' => ',',
'*' => '.*', '*' => '.*',
Expand Down
3 changes: 3 additions & 0 deletions package.xml
Expand Up @@ -26,6 +26,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
</stability> </stability>
<license uri="http://matrix.squiz.net/developer/tools/php_cs/licence">BSD License</license> <license uri="http://matrix.squiz.net/developer/tools/php_cs/licence">BSD License</license>
<notes> <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 - 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 { ... -- For example: namespace MyStandard\Sniffs\Arrays; class ArrayDeclarationSniff implements \PHP_CodeSniffer_Sniff { ...
-- Thanks to Till Klampaeckel for the patch -- Thanks to Till Klampaeckel for the patch
Expand Down
2 changes: 1 addition & 1 deletion scripts/phpcs-svn-pre-commit
Expand Up @@ -159,7 +159,7 @@ class PHP_CodeSniffer_SVN_Hook extends PHP_CodeSniffer_CLI


// We need to check ignore rules ourself because they are // We need to check ignore rules ourself because they are
// not checked when processing a single file. // not checked when processing a single file.
if ($phpcs->shouldProcessFile($path) === false) { if ($phpcs->shouldProcessFile($path, dirname($path)) === false) {
continue; continue;
} }


Expand Down

0 comments on commit dd33c26

Please sign in to comment.