Skip to content

Commit 054bc67

Browse files
committed
Initial implementation in SourceFilter
1 parent 4e3930a commit 054bc67

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/TextUI/Configuration/SourceFilter.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
*/
1010
namespace PHPUnit\TextUI\Configuration;
1111

12+
use function array_map;
1213
use PHPUnit\Util\FileMatcher;
1314
use PHPUnit\Util\FileMatcherRegex;
1415

15-
1616
/**
17+
* TODO: Does not take into account suffixes and prefixes - and tests don't cover it.
18+
*
1719
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
1820
*
1921
* @internal This class is not covered by the backward compatibility promise for PHPUnit
2022
*/
2123
final class SourceFilter
2224
{
2325
private static ?self $instance = null;
24-
2526
private Source $source;
2627

2728
/**
@@ -38,30 +39,54 @@ public static function instance(): self
3839
{
3940
if (self::$instance === null) {
4041
$source = Registry::get()->source();
42+
4143
return new self($source);
4244
}
4345

4446
return self::$instance;
4547
}
4648

47-
/**
48-
* @param array<non-empty-string, true> $map
49-
*/
5049
public function __construct(Source $source)
5150
{
52-
$this->source = $source;
53-
$this->includeDirectoryRegexes = array_map(function (FilterDirectory $directory) {
51+
$this->source = $source;
52+
$this->includeDirectoryRegexes = array_map(static function (FilterDirectory $directory)
53+
{
5454
return FileMatcher::toRegEx($directory->path());
5555
}, $source->includeDirectories()->asArray());
56-
$this->excludeDirectoryRegexes = array_map(function (FilterDirectory $directory) {
56+
$this->excludeDirectoryRegexes = array_map(static function (FilterDirectory $directory)
57+
{
5758
return FileMatcher::toRegEx($directory->path());
5859
}, $source->excludeDirectories()->asArray());
5960
}
6061

6162
public function includes(string $path): bool
6263
{
63-
foreach ($this->source->includeDirectories() as $directory) {
64+
$included = false;
65+
66+
foreach ($this->source->includeFiles() as $file) {
67+
if ($file->path() === $path) {
68+
$included = true;
69+
}
6470
}
65-
return isset($this->map[$path]);
71+
72+
foreach ($this->includeDirectoryRegexes as $directoryRegex) {
73+
if ($directoryRegex->matches($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 ($directoryRegex->matches($path)) {
86+
$included = false;
87+
}
88+
}
89+
90+
return $included;
6691
}
6792
}

0 commit comments

Comments
 (0)