Remove duplication of the handling of regex filters in the Finder

This commit is contained in:
Christophe Coevoet 2015-09-15 16:03:47 +02:00
parent 6393ec3169
commit e66bf64da7
4 changed files with 38 additions and 59 deletions

View File

@ -41,25 +41,7 @@ class FilecontentFilterIterator extends MultiplePcreFilterIterator
return false;
}
// should at least not match one rule to exclude
foreach ($this->noMatchRegexps as $regex) {
if (preg_match($regex, $content)) {
return false;
}
}
// should at least match one rule
$match = true;
if ($this->matchRegexps) {
$match = false;
foreach ($this->matchRegexps as $regex) {
if (preg_match($regex, $content)) {
return true;
}
}
}
return $match;
return $this->isAccepted($content);
}
/**

View File

@ -27,27 +27,7 @@ class FilenameFilterIterator extends MultiplePcreFilterIterator
*/
public function accept()
{
$filename = $this->current()->getFilename();
// should at least not match one rule to exclude
foreach ($this->noMatchRegexps as $regex) {
if (preg_match($regex, $filename)) {
return false;
}
}
// should at least match one rule
$match = true;
if ($this->matchRegexps) {
$match = false;
foreach ($this->matchRegexps as $regex) {
if (preg_match($regex, $filename)) {
return true;
}
}
}
return $match;
return $this->isAccepted($this->current()->getFilename());
}
/**

View File

@ -43,6 +43,41 @@ abstract class MultiplePcreFilterIterator extends FilterIterator
parent::__construct($iterator);
}
/**
* Checks whether the string is accepted by the regex filters.
*
* If there is no regexps defined in the class, this method will accept the string.
* Such case can be handled by child classes before calling the method if they want to
* apply a different behavior.
*
* @param string $string The string to be matched against filters
*
* @return bool
*/
protected function isAccepted($string)
{
// should at least not match one rule to exclude
foreach ($this->noMatchRegexps as $regex) {
if (preg_match($regex, $string)) {
return false;
}
}
// should at least match one rule
if ($this->matchRegexps) {
foreach ($this->matchRegexps as $regex) {
if (preg_match($regex, $string)) {
return true;
}
}
return false;
}
// If there is no match rules, the file is accepted
return true;
}
/**
* Checks whether the string is a regex.
*

View File

@ -32,25 +32,7 @@ class PathFilterIterator extends MultiplePcreFilterIterator
$filename = str_replace('\\', '/', $filename);
}
// should at least not match one rule to exclude
foreach ($this->noMatchRegexps as $regex) {
if (preg_match($regex, $filename)) {
return false;
}
}
// should at least match one rule
$match = true;
if ($this->matchRegexps) {
$match = false;
foreach ($this->matchRegexps as $regex) {
if (preg_match($regex, $filename)) {
return true;
}
}
}
return $match;
return $this->isAccepted($filename);
}
/**