|
9 | 9 | */ |
10 | 10 | namespace PHPUnit\TextUI\Configuration; |
11 | 11 |
|
| 12 | +use Webmozart\Glob\Glob; |
| 13 | +use function array_map; |
| 14 | + |
12 | 15 | /** |
13 | 16 | * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit |
14 | 17 | * |
|
17 | 20 | final class SourceFilter |
18 | 21 | { |
19 | 22 | private static ?self $instance = null; |
| 23 | + private Source $source; |
20 | 24 |
|
21 | 25 | /** |
22 | | - * @var array<non-empty-string, true> |
| 26 | + * @var list<string> |
23 | 27 | */ |
24 | | - private readonly array $map; |
| 28 | + private array $includeDirectoryRegexes; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var list<string> |
| 32 | + */ |
| 33 | + private array $excludeDirectoryRegexes; |
25 | 34 |
|
26 | 35 | public static function instance(): self |
27 | 36 | { |
28 | 37 | if (self::$instance === null) { |
29 | | - self::$instance = new self( |
30 | | - (new SourceMapper)->map( |
31 | | - Registry::get()->source(), |
32 | | - ), |
33 | | - ); |
| 38 | + $source = Registry::get()->source(); |
| 39 | + self::$instance = new self($source); |
| 40 | + |
| 41 | + return self::$instance; |
34 | 42 | } |
35 | 43 |
|
36 | 44 | return self::$instance; |
37 | 45 | } |
38 | 46 |
|
| 47 | + public function __construct(Source $source) |
| 48 | + { |
| 49 | + $this->source = $source; |
| 50 | + $this->includeDirectoryRegexes = array_map(static function (FilterDirectory $directory) |
| 51 | + { |
| 52 | + return Glob::toRegEx(self::toGlob($directory)); |
| 53 | + }, $source->includeDirectories()->asArray()); |
| 54 | + $this->excludeDirectoryRegexes = array_map(static function (FilterDirectory $directory) |
| 55 | + { |
| 56 | + return Glob::toRegEx(self::toGlob($directory)); |
| 57 | + }, $source->excludeDirectories()->asArray()); |
| 58 | + } |
| 59 | + |
39 | 60 | /** |
40 | | - * @param array<non-empty-string, true> $map |
| 61 | + * @see https://docs.phpunit.de/en/12.4/configuration.html#the-include-element |
41 | 62 | */ |
42 | | - public function __construct(array $map) |
| 63 | + public function includes(string $path): bool |
43 | 64 | { |
44 | | - $this->map = $map; |
| 65 | + $included = false; |
| 66 | + foreach ($this->source->includeFiles() as $file) { |
| 67 | + if ($file->path() === $path) { |
| 68 | + $included = true; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + foreach ($this->includeDirectoryRegexes as $directoryRegex) { |
| 73 | + if (preg_match($directoryRegex, $path)) { |
| 74 | + $included = true; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + foreach ($this->source->excludeFiles() as $file) { |
| 79 | + if ($file->path() === $path) { |
| 80 | + $included = false; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + foreach ($this->excludeDirectoryRegexes as $directoryRegex) { |
| 85 | + if (preg_match($directoryRegex, $path)) { |
| 86 | + $included = false; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return $included; |
45 | 91 | } |
46 | 92 |
|
47 | | - public function includes(string $path): bool |
| 93 | + public static function toGlob(FilterDirectory $directory): string |
48 | 94 | { |
49 | | - return isset($this->map[$path]); |
| 95 | + $glob = sprintf('%s/**/%s*%s', $directory->path(), $directory->prefix(),$directory->suffix()); |
| 96 | + return $glob; |
50 | 97 | } |
51 | 98 | } |
0 commit comments