8000 minor #14287 [Finder] Removed duplicated toRegex() code (jaytaph) · symfony/symfony@995448c · GitHub
[go: up one dir, main page]

Skip to content

Commit 995448c

Browse files
committed
minor #14287 [Finder] Removed duplicated toRegex() code (jaytaph)
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
2 parents c52320b + 6150c3a commit 995448c

File tree

4 files changed

+35
-56
lines changed

4 files changed

+35
-56
lines changed

src/Symfony/Component/Finder/Expression/Glob.php

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Finder\Expression;
1313

14+
use Symfony\Component\Finder\Glob as FinderGlob;
15+
1416
/**
1517
* @author Jean-François Simon <contact@jfsimon.fr>
1618
*/
@@ -100,58 +102,8 @@ public function isExpandable()
100102
*/
101103
public function toRegex($strictLeadingDot = true, $strictWildcardSlash = true)
102104
{
103-
$firstByte = true;
104-
$escaping = false;
105-
$inCurlies = 0;
106-
$regex = '';
107-
$sizeGlob = strlen($this->pattern);
108-
for ($i = 0; $i < $sizeGlob; $i++) {
109-
$car = $this->pattern[$i];
110-
if ($firstByte) {
111-
if ($strictLeadingDot && '.' !== $car) {
112-
$regex .= '(?=[^\.])';
113-
}
114-
115-
$firstByte = false;
116-
}
117-
118-
if ('/' === $car) {
119-
$firstByte = true;
120-
}
121-
122-
if ('.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
123-
$regex .= "\\$car";
124-
} elseif ('*' === $car) {
125-
$regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
126-
} elseif ('?' === $car) {
127-
$regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
128-
} elseif ('{' === $car) {
129-
$regex .= $escaping ? '\\{' : '(';
130-
if (!$escaping) {
131-
++$inCurlies;
132-
}
133-
} elseif ('}' === $car && $inCurlies) {
134-
$regex .= $escaping ? '}' : ')';
135-
if (!$escaping) {
136-
--$inCurlies;
137-
}
138-
} elseif (',' === $car && $inCurlies) {
139-
$regex .= $escaping ? ',' : '|';
140-
} elseif ('\\' === $car) {
141-
if ($escaping) {
142-
$regex .= '\\\\';
143-
$escaping = false;
144-
} else {
145-
$escaping = true;
146-
}
147-
148-
continue;
149-
} else {
150-
$regex .= $car;
151-
}
152-
$escaping = false;
153-
}
154-
155-
return new Regex('^'.$regex.'$');
105+
$regex = FinderGlob::toRegex($this->pattern, $strictLeadingDot, $strictWildcardSlash, '');
106+
107+
return new Regex($regex);
156108
}
157109
}

src/Symfony/Component/Finder/Glob.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ class Glob
4141
* @param string $glob The glob pattern
4242
* @param bool $strictLeadingDot
4343
* @param bool $strictWildcardSlash
44+
* @param string $delimiter Optional delimiter
4445
*
4546
* @return string regex The regexp
4647
*/
47-
public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true)
48+
public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true, $delimiter = '#')
4849
{
4950
$firstByte = true;
5051
$escaping = false;
@@ -98,6 +99,6 @@ public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardS
9899
$escaping = false;
99100
}
100101

101-
return '#^'.$regex.'$#';
102+
return $delimiter.'^'.$regex.'$'.$delimiter;
102103
}
103104
}

src/Symfony/Component/Finder/Tests/FinderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public function testIn($adapter)
306306
$finder = $this->buildFinder($adapter);
307307
$iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
308308

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

312312
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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;
13+
14+
use Symfony\Component\Finder\Glob;
15+
16+
class GlobTest extends \PHPUnit_Framework_TestCase
17+
{
18+
19+
public function testGlobToRegexDelimiters()
20+
{
21+
$this->assertEquals(Glob::toRegex('.*'), '#^\.[^/]*$#');
22+
$this->assertEquals(Glob::toRegex('.*', true, true, ''), '^\.[^/]*$');
23+
$this->assertEquals(Glob::toRegex('.*', true, true, '/'), '/^\.[^/]*$/');
24+
}
25+
26+
}

0 commit comments

Comments
 (0)
0