Skip to content

Commit

Permalink
minor #14287 [Finder] Removed duplicated toRegex() code (jaytaph)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.7 branch.

Discussion
----------

[Finder] Removed duplicated toRegex() code

This patch removes duplicated `toRegex()` code by using the already existing `Glob` class. As this class wasn't unit-tested to begin with, this duplication also makes sure this class is tested properly.

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #14075
| License       | MIT

Commits
-------

6150c3a Removed duplicated toRegex() code
  • Loading branch information
fabpot committed Apr 15, 2015
2 parents c52320b + 6150c3a commit 995448c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 56 deletions.
58 changes: 5 additions & 53 deletions src/Symfony/Component/Finder/Expression/Glob.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Finder\Expression;

use Symfony\Component\Finder\Glob as FinderGlob;

/**
* @author Jean-François Simon <contact@jfsimon.fr>
*/
Expand Down Expand Up @@ -100,58 +102,8 @@ public function isExpandable()
*/
public function toRegex($strictLeadingDot = true, $strictWildcardSlash = true)
{
$firstByte = true;
$escaping = false;
$inCurlies = 0;
$regex = '';
$sizeGlob = strlen($this->pattern);
for ($i = 0; $i < $sizeGlob; $i++) {
$car = $this->pattern[$i];
if ($firstByte) {
if ($strictLeadingDot && '.' !== $car) {
$regex .= '(?=[^\.])';
}

$firstByte = false;
}

if ('/' === $car) {
$firstByte = true;
}

if ('.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
$regex .= "\\$car";
} elseif ('*' === $car) {
$regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
} elseif ('?' === $car) {
$regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
} elseif ('{' === $car) {
$regex .= $escaping ? '\\{' : '(';
if (!$escaping) {
++$inCurlies;
}
} elseif ('}' === $car && $inCurlies) {
$regex .= $escaping ? '}' : ')';
if (!$escaping) {
--$inCurlies;
}
} elseif (',' === $car && $inCurlies) {
$regex .= $escaping ? ',' : '|';
} elseif ('\\' === $car) {
if ($escaping) {
$regex .= '\\\\';
$escaping = false;
} else {
$escaping = true;
}

continue;
} else {
$regex .= $car;
}
$escaping = false;
}

return new Regex('^'.$regex.'$');
$regex = FinderGlob::toRegex($this->pattern, $strictLeadingDot, $strictWildcardSlash, '');

return new Regex($regex);
}
}
5 changes: 3 additions & 2 deletions src/Symfony/Component/Finder/Glob.php
Expand Up @@ -41,10 +41,11 @@ class Glob
* @param string $glob The glob pattern
* @param bool $strictLeadingDot
* @param bool $strictWildcardSlash
* @param string $delimiter Optional delimiter
*
* @return string regex The regexp
*/
public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true)
public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true, $delimiter = '#')
{
$firstByte = true;
$escaping = false;
Expand Down Expand Up @@ -98,6 +99,6 @@ public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardS
$escaping = false;
}

return '#^'.$regex.'$#';
return $delimiter.'^'.$regex.'$'.$delimiter;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Finder/Tests/FinderTest.php
Expand Up @@ -306,7 +306,7 @@ public function testIn($adapter)
$finder = $this->buildFinder($adapter);
$iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();

$this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php'), $iterator);
$this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php', __DIR__.DIRECTORY_SEPARATOR.'GlobTest.php'), $iterator);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/Finder/Tests/GlobTest.php
@@ -0,0 +1,26 @@
<?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;

use Symfony\Component\Finder\Glob;

class GlobTest extends \PHPUnit_Framework_TestCase
{

public function testGlobToRegexDelimiters()
{
$this->assertEquals(Glob::toRegex('.*'), '#^\.[^/]*$#');
$this->assertEquals(Glob::toRegex('.*', true, true, ''), '^\.[^/]*$');
$this->assertEquals(Glob::toRegex('.*', true, true, '/'), '/^\.[^/]*$/');
}

}

0 comments on commit 995448c

Please sign in to comment.