Skip to content

Commit f728463

Browse files
committed
[Finder] Fixes in the iterators
1 parent 9d22eb6 commit f728463

File tree

4 files changed

+124
-55
lines changed

4 files changed

+124
-55
lines changed

src/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.php

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,34 @@ class FilecontentFilterIterator extends MultiplePcreFilterIterator
2626
*/
2727
public function accept()
2828
{
29+
if (!$this->matchRegexps && !$this->noMatchRegexps) {
30+
return true;
31+
}
32+
33+
$content = file_get_contents($this->getRealpath());
34+
if (false === $content) {
35+
throw new \RuntimeException(sprintf('Error reading file "%s".', $this->getRealpath()));
36+
}
37+
38+
// should at least not match one rule to exclude
39+
foreach ($this->noMatchRegexps as $regex) {
40+
if (preg_match($regex, $content)) {
41+
return false;
42+
}
43+
}
44+
2945
// should at least match one rule
46+
$match = true;
3047
if ($this->matchRegexps) {
3148
$match = false;
3249
foreach ($this->matchRegexps as $regex) {
33-
$content = file_get_contents($this->getRealpath());
34-
if (false === $content) {
35-
throw new \RuntimeException(sprintf('Error reading file "%s".', $this->getRealpath()));
36-
}
3750
if (preg_match($regex, $content)) {
38-
$match = true;
39-
break;
51+
return true;
4052
}
4153
}
42-
} else {
43-
$match = true;
4454
}
4555

46-
// should at least not match one rule to exclude
47-
if ($this->noMatchRegexps) {
48-
$exclude = false;
49-
foreach ($this->noMatchRegexps as $regex) {
50-
$content = file_get_contents($this->getRealpath());
51-
if (false === $content) {
52-
throw new \RuntimeException(sprintf('Error reading file "%s".', $this->getRealpath()));
53-
}
54-
if (preg_match($regex, $content)) {
55-
$exclude = true;
56-
break;
57-
}
58-
}
59-
} else {
60-
$exclude = false;
61-
}
62-
63-
return $match && !$exclude;
56+
return $match;
6457
}
6558

6659
/**
@@ -72,10 +65,6 @@ public function accept()
7265
*/
7366
protected function toRegex($str)
7467
{
75-
if (preg_match('/^([^a-zA-Z0-9\\\\]).+?\\1[ims]?$/', $str)) {
76-
return $str;
77-
}
78-
79-
return sprintf('/%s/', preg_quote($str, '/'));
68+
return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/';
8069
}
8170
}

src/Symfony/Component/Finder/Iterator/FilenameFilterIterator.php

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,27 @@ class FilenameFilterIterator extends MultiplePcreFilterIterator
2828
*/
2929
public function accept()
3030
{
31+
$filename = $this->getFilename();
32+
33+
// should at least not match one rule to exclude
34+
foreach ($this->noMatchRegexps as $regex) {
35+
if (preg_match($regex, $filename)) {
36+
return false;
37+
}
38+
}
39+
3140
// should at least match one rule
41+
$match = true;
3242
if ($this->matchRegexps) {
3343
$match = false;
3444
foreach ($this->matchRegexps as $regex) {
35-
if (preg_match($regex, $this->getFilename())) {
36-
$match = true;
37-
break;
38-
}
39-
}
40-
} else {
41-
$match = true;
42-
}
43-
44-
// should at least not match one rule to exclude
45-
if ($this->noMatchRegexps) {
46-
$exclude = false;
47-
foreach ($this->noMatchRegexps as $regex) {
48-
if (preg_match($regex, $this->getFilename())) {
49-
$exclude = true;
50-
break;
45+
if (preg_match($regex, $filename)) {
46+
return true;
5147
}
5248
}
53-
} else {
54-
$exclude = false;
5549
}
5650

57-
return $match && !$exclude;
51+
return $match;
5852
}
5953

6054
/**
@@ -69,10 +63,6 @@ public function accept()
6963
*/
7064
protected function toRegex($str)
7165
{
72-
if (preg_match('/^([^a-zA-Z0-9\\\\]).+?\\1[ims]?$/', $str)) {
73-
return $str;
74-
}
75-
76-
return Glob::toRegex($str);
66+
return $this->isRegex($str) ? $str : Glob::toRegex($str);
7767
}
7868
}

src/Symfony/Component/Finder/Iterator/MultiplePcreFilterIterator.php

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

47+
/**
48+
* Checks whether the string is a regex.
49+
*
50+
* @param string $str
51+
*
52+
* @return Boolean Whether the given string is a regex
53+
*/
54+
protected function isRegex($str) {
55+
56+
if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) {
57+
$start = substr($m[1], 0, 1);
58+
$end = substr($m[1], -1);
59+
60+
if ($start === $end) {
61+
return !preg_match('/[[:alnum:] \\\\]/', $start);
62+
}
63+
64+
if ($start === '{' && $end === '}') {
65+
return true;
66+
}
67+
}
68+
69+
return false;
70+
}
71+
4772
/**
4873
* Converts string into regexp.
4974
*
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Finder\Tests\Iterator;
13+
14+
use Symfony\Component\Finder\Iterator\MultiplePcreFilterIterator;
15+
16+
class MultiplePcreFilterIteratorTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @dataProvider getIsRegexFixtures
20+
*/
21+
public function testIsRegex($string, $isRegex, $message)
22+
{
23+
$testIterator = new TestMultiplePcreFilterIterator();
24+
$this->assertEquals($isRegex, $testIterator->isRegex($string), $message);
25+
}
26+
27+
public function getIsRegexFixtures()
28+
{
29+
return array(
30+
array('foo', false, 'string'),
31+
array(' foo ', false, '" " is not a valid delimiter'),
32+
array('\\foo\\', false, '"\\" is not a valid delimiter'),
33+
array('afooa', false, '"a" is not a valid delimiter'),
34+
array('//', false, 'the pattern should contain at least 1 character'),
35+
array('/a/', true, 'valid regex'),
36+
array('/foo/', true, 'valid regex'),
37+
array('/foo/i', true, 'valid regex with a single modifier'),
38+
array('/foo/imsxu', true, 'valid regex with multiple modifiers'),
39+
array('#foo#', true, '"#" is a valid delimiter'),
40+
array('{foo}', true, '"{,}" is a valid delimiter pair'),
41+
);
42+
}
43+
}
44+
45+
class TestMultiplePcreFilterIterator extends MultiplePcreFilterIterator
46+
{
47+
public function __construct()
48+
{
49+
}
50+
51+
public function accept()
52+
{
53+
throw new \BadFunctionCallException('Not implemented');
54+
}
55+
56+
public function isRegex($str)
57+
{
58+
return parent::isRegex($str);
59+
}
60+
61+
public function toRegex($str)
62+
{
63+
throw new \BadFunctionCallException('Not implemented');
64+
}
65+
}

0 commit comments

Comments
 (0)