Skip to content

Commit da909a5

Browse files
committed
minor #15807 Remove duplication of the handling of regex filters in the Finder (stof)
This PR was merged into the 2.8 branch. Discussion ---------- Remove duplication of the handling of regex filters in the Finder The logic to handle the multiple regexs in MultiplePcreFilterIterator children is the same each time (and will always be the same given it is related to the meaning of properties in MultiplePcreFilterIterator itself). This extracts this logic in MultiplePcreFilterIterator itself rather than duplicating it in all child classes. Commits ------- e66bf64 Remove duplication of the handling of regex filters in the Finder
2 parents ac42eb0 + 5b26dff commit da909a5

File tree

4 files changed

+38
-59
lines changed

4 files changed

+38
-59
lines changed

Iterator/FilecontentFilterIterator.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,7 @@ public function accept()
4141
return false;
4242
}
4343

44-
// should at least not match one rule to exclude
45-
foreach ($this->noMatchRegexps as $regex) {
46-
if (preg_match($regex, $content)) {
47-
return false;
48-
}
49-
}
50-
51-
// should at least match one rule
52-
$match = true;
53-
if ($this->matchRegexps) {
54-
$match = false;
55-
foreach ($this->matchRegexps as $regex) {
56-
if (preg_match($regex, $content)) {
57-
return true;
58-
}
59-
}
60-
}
61-
62-
return $match;
44+
return $this->isAccepted($content);
6345
}
6446

6547
/**

Iterator/FilenameFilterIterator.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,7 @@ class FilenameFilterIterator extends MultiplePcreFilterIterator
2727
*/
2828
public function accept()
2929
{
30-
$filename = $this->current()->getFilename();
31-
32-
// should at least not match one rule to exclude
33-
foreach ($this->noMatchRegexps as $regex) {
34-
if (preg_match($regex, $filename)) {
35-
return false;
36-
}
37-
}
38-
39-
// should at least match one rule
40-
$match = true;
41-
if ($this->matchRegexps) {
42-
$match = false;
43-
foreach ($this->matchRegexps as $regex) {
44-
if (preg_match($regex, $filename)) {
45-
return true;
46-
}
47-
}
48-
}
49-
50-
return $match;
30+
return $this->isAccepted($this->current()->getFilename());
5131
}
5232

5333
/**

Iterator/MultiplePcreFilterIterator.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@ public function __construct(\Iterator $iterator, array $matchPatterns, array $no
4343
parent::__construct($iterator);
4444
}
4545

46+
/**
47+
* Checks whether the string is accepted by the regex filters.
48+
*
49+
* If there is no regexps defined in the class, this method will accept the string.
50+
* Such case can be handled by child classes before calling the method if they want to
51+
* apply a different behavior.
52+
*
53+
* @param string $string The string to be matched against filters
54+
*
55+
* @return bool
56+
*/
57+
protected function isAccepted($string)
58+
{
59+
// should at least not match one rule to exclude
60+
foreach ($this->noMatchRegexps as $regex) {
61+
if (preg_match($regex, $string)) {
62+
return false;
63+
}
64+
}
65+
66+
// should at least match one rule
67+
if ($this->matchRegexps) {
68+
foreach ($this->matchRegexps as $regex) {
69+
if (preg_match($regex, $string)) {
70+
return true;
71+
}
72+
}
73+
74+
return false;
75+
}
76+
77+
// If there is no match rules, the file is accepted
78+
return true;
79+
}
80+
4681
/**
4782
* Checks whether the string is a regex.
4883
*

Iterator/PathFilterIterator.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,7 @@ public function accept()
3232
$filename = str_replace('\\', '/', $filename);
3333
}
3434

35-
// should at least not match one rule to exclude
36-
foreach ($this->noMatchRegexps as $regex) {
37-
if (preg_match($regex, $filename)) {
38-
return false;
39-
}
40-
}
41-
42-
// should at least match one rule
43-
$match = true;
44-
if ($this->matchRegexps) {
45-
$match = false;
46-
foreach ($this->matchRegexps as $regex) {
47-
if (preg_match($regex, $filename)) {
48-
return true;
49-
}
50-
}
51-
}
52-
53-
return $match;
35+
return $this->isAccepted($filename);
5436
}
5537

5638
/**

0 commit comments

Comments
 (0)