Skip to content

Commit b96fa28

Browse files
committed
Add doc
1 parent 177cdde commit b96fa28

File tree

3 files changed

+55
-35
lines changed

3 files changed

+55
-35
lines changed

src/Util/FileMatcher.php

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@
1313
use function array_pop;
1414
use function count;
1515
use function ctype_alpha;
16-
use function preg_match;
1716
use function preg_quote;
1817
use function sprintf;
1918
use function strlen;
20-
use function substr;
2119
use RuntimeException;
2220

2321
/**
22+
* FileMatcher attempts to emulate the behavior of PHP's glob function on file
23+
* paths based on POSIX.2.
24+
*
25+
* - https://en.wikipedia.org/wiki/Glob_(programming)
26+
* - https://man7.org/linux/man-pages/man7/glob.7.html
27+
*
2428
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
2529
*
2630
* @internal This class is not covered by the backward compatibility promise for PHPUnit
@@ -43,27 +47,13 @@
4347
private const T_COLON = 'colon';
4448
private const T_CHAR_CLASS = 'char_class';
4549

46-
public static function match(string $path, FileMatcherPattern $pattern): bool
47-
{
48-
self::assertIsAbsolute($path);
49-
50-
$regex = self::toRegEx($pattern->path);
51-
52-
return preg_match($regex, $path) !== 0;
53-
}
54-
5550
/**
56-
* Based on webmozart/glob.
57-
*
58-
* @return string the regular expression for matching the glob
51+
* Compile a regex for the given glob.
5952
*/
60-
public static function toRegEx($glob, $flags = 0): string
53+
public static function toRegEx(string $glob): FileMatcherRegex
6154
{
62-
self::assertIsAbsolute($glob);
63-
6455
$tokens = self::tokenize($glob);
65-
66-
$regex = '';
56+
$regex = '';
6757

6858
foreach ($tokens as $token) {
6959
$type = $token[0];
@@ -84,24 +74,15 @@ public static function toRegEx($glob, $flags = 0): string
8474
self::T_BRACKET_CLOSE => ']',
8575
self::T_HYPHEN => '-',
8676
self::T_CHAR_CLASS => '[:' . $token[1] . ':]',
87-
default => '',
77+
default => throw new RuntimeException(sprintf(
78+
'Unhandled token type: %s - this should not happen',
79+
$type,
80+
)),
8881
};
8982
}
9083
$regex .= '(/|$)';
91-
dump($tokens);
92-
dump($regex);
9384

94-
return '{^' . $regex . '}';
95-
}
96-
97-
private static function assertIsAbsolute(string $path): void
98-
{
99-
if (substr($path, 0, 1) !== '/') {
100-
throw new RuntimeException(sprintf(
101-
'Path "%s" must be absolute',
102-
$path,
103-
));
104-
}
85+
return new FileMatcherRegex('{^' . $regex . '}');
10586
}
10687

10788
/**

src/Util/FileMatcherRegex.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Util;
11+
12+
use function preg_match;
13+
use function sprintf;
14+
use function substr;
15+
use RuntimeException;
16+
17+
final class FileMatcherRegex
18+
{
19+
public function __construct(private string $regex)
20+
{
21+
}
22+
23+
public function matches(string $path): bool
24+
{
25+
self::assertIsAbsolute($path);
26+
27+
return preg_match($this->regex, $path) !== 0;
28+
}
29+
30+
private static function assertIsAbsolute(string $path): void
31+
{
32+
if (substr($path, 0, 1) !== '/') {
33+
throw new RuntimeException(sprintf(
34+
'Path "%s" must be absolute',
35+
$path,
36+
));
37+
}
38+
}
39+
}

tests/unit/Util/FileMatcherTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ public function testExceptionIfPathIsNotAbsolute(): void
612612
{
613613
$this->expectException(RuntimeException::class);
614614
$this->expectExceptionMessage('Path "foo/bar" must be absolute');
615-
FileMatcher::match('foo/bar', new FileMatcherPattern(''));
615+
FileMatcher::toRegEx('/a')->matches('foo/bar');
616616
}
617617

618618
/**
@@ -621,7 +621,7 @@ public function testExceptionIfPathIsNotAbsolute(): void
621621
private static function assertMap(FileMatcherPattern $pattern, array $matchMap): void
622622
{
623623
foreach ($matchMap as $candidate => $shouldMatch) {
624-
$matches = FileMatcher::match($candidate, $pattern);
624+
$matches = FileMatcher::toRegEx($pattern->path)->matches($candidate);
625625

626626
if ($matches === $shouldMatch) {
627627
self::assertTrue(true);

0 commit comments

Comments
 (0)