Skip to content

Commit

Permalink
[Finder] Fixes in the iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Apr 20, 2012
1 parent 9d22eb6 commit f728463
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 55 deletions.
51 changes: 20 additions & 31 deletions src/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.php
Expand Up @@ -26,41 +26,34 @@ class FilecontentFilterIterator extends MultiplePcreFilterIterator
*/
public function accept()
{
if (!$this->matchRegexps && !$this->noMatchRegexps) {
return true;
}

$content = file_get_contents($this->getRealpath());
if (false === $content) {
throw new \RuntimeException(sprintf('Error reading file "%s".', $this->getRealpath()));
}

// 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) {
$content = file_get_contents($this->getRealpath());
if (false === $content) {
throw new \RuntimeException(sprintf('Error reading file "%s".', $this->getRealpath()));
}
if (preg_match($regex, $content)) {
$match = true;
break;
return true;
}
}
} else {
$match = true;
}

// should at least not match one rule to exclude
if ($this->noMatchRegexps) {
$exclude = false;
foreach ($this->noMatchRegexps as $regex) {
$content = file_get_contents($this->getRealpath());
if (false === $content) {
throw new \RuntimeException(sprintf('Error reading file "%s".', $this->getRealpath()));
}
if (preg_match($regex, $content)) {
$exclude = true;
break;
}
}
} else {
$exclude = false;
}

return $match && !$exclude;
return $match;
}

/**
Expand All @@ -72,10 +65,6 @@ public function accept()
*/
protected function toRegex($str)
{
if (preg_match('/^([^a-zA-Z0-9\\\\]).+?\\1[ims]?$/', $str)) {
return $str;
}

return sprintf('/%s/', preg_quote($str, '/'));
return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/';
}
}
38 changes: 14 additions & 24 deletions src/Symfony/Component/Finder/Iterator/FilenameFilterIterator.php
Expand Up @@ -28,33 +28,27 @@ class FilenameFilterIterator extends MultiplePcreFilterIterator
*/
public function accept()
{
$filename = $this->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, $this->getFilename())) {
$match = true;
break;
}
}
} else {
$match = true;
}

// should at least not match one rule to exclude
if ($this->noMatchRegexps) {
$exclude = false;
foreach ($this->noMatchRegexps as $regex) {
if (preg_match($regex, $this->getFilename())) {
$exclude = true;
break;
if (preg_match($regex, $filename)) {
return true;
}
}
} else {
$exclude = false;
}

return $match && !$exclude;
return $match;
}

/**
Expand All @@ -69,10 +63,6 @@ public function accept()
*/
protected function toRegex($str)
{
if (preg_match('/^([^a-zA-Z0-9\\\\]).+?\\1[ims]?$/', $str)) {
return $str;
}

return Glob::toRegex($str);
return $this->isRegex($str) ? $str : Glob::toRegex($str);
}
}
Expand Up @@ -44,6 +44,31 @@ public function __construct(\Iterator $iterator, array $matchPatterns, array $no
parent::__construct($iterator);
}

/**
* Checks whether the string is a regex.
*
* @param string $str
*
* @return Boolean Whether the given string is a regex
*/
protected function isRegex($str) {

if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) {
$start = substr($m[1], 0, 1);
$end = substr($m[1], -1);

if ($start === $end) {
return !preg_match('/[[:alnum:] \\\\]/', $start);
}

if ($start === '{' && $end === '}') {
return true;
}
}

return false;
}

/**
* Converts string into regexp.
*
Expand Down
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Finder\Tests\Iterator;

use Symfony\Component\Finder\Iterator\MultiplePcreFilterIterator;

class MultiplePcreFilterIteratorTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getIsRegexFixtures
*/
public function testIsRegex($string, $isRegex, $message)
{
$testIterator = new TestMultiplePcreFilterIterator();
$this->assertEquals($isRegex, $testIterator->isRegex($string), $message);
}

public function getIsRegexFixtures()
{
return array(
array('foo', false, 'string'),
array(' foo ', false, '" " is not a valid delimiter'),
array('\\foo\\', false, '"\\" is not a valid delimiter'),
array('afooa', false, '"a" is not a valid delimiter'),
array('//', false, 'the pattern should contain at least 1 character'),
array('/a/', true, 'valid regex'),
array('/foo/', true, 'valid regex'),
array('/foo/i', true, 'valid regex with a single modifier'),
array('/foo/imsxu', true, 'valid regex with multiple modifiers'),
array('#foo#', true, '"#" is a valid delimiter'),
array('{foo}', true, '"{,}" is a valid delimiter pair'),
);
}
}

class TestMultiplePcreFilterIterator extends MultiplePcreFilterIterator
{
public function __construct()
{
}

public function accept()
{
throw new \BadFunctionCallException('Not implemented');
}

public function isRegex($str)
{
return parent::isRegex($str);
}

public function toRegex($str)
{
throw new \BadFunctionCallException('Not implemented');
}
}

0 comments on commit f728463

Please sign in to comment.