/
Filter.php
207 lines (177 loc) · 6.66 KB
/
Filter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
declare(strict_types=1);
namespace Codeception\Coverage;
use Codeception\Configuration;
use Codeception\Exception\ConfigurationException;
use Codeception\Exception\ModuleException;
use PHPUnit\Runner\Version as PHPUnitVersion;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Filter as PhpUnitFilter;
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
use Symfony\Component\Finder\Finder;
use function array_pop;
use function explode;
use function implode;
use function is_array;
use function iterator_to_array;
use function str_replace;
class Filter
{
protected static ?self $codeceptionFilter = null;
protected ?PhpUnitFilter $phpUnitFilter = null;
public function __construct(protected ?CodeCoverage $phpCodeCoverage)
{
$this->phpUnitFilter = $this->phpCodeCoverage->filter();
}
public static function setup(CodeCoverage $phpCoverage): self
{
self::$codeceptionFilter = new self($phpCoverage);
return self::$codeceptionFilter;
}
/**
* @throws ConfigurationException
*/
public function whiteList(array $config): self
{
$filter = $this->phpUnitFilter;
if (!isset($config['coverage'])) {
return $this;
}
$coverage = $config['coverage'];
if (!isset($coverage['whitelist'])) {
$coverage['whitelist'] = [];
if (isset($coverage['include'])) {
$coverage['whitelist']['include'] = $coverage['include'];
}
if (isset($coverage['exclude'])) {
$coverage['whitelist']['exclude'] = $coverage['exclude'];
}
}
if (PHPUnitVersion::series() >= 11) {
return $this->newWhiteList($coverage['whitelist']);
}
if (isset($coverage['whitelist']['include'])) {
if (!is_array($coverage['whitelist']['include'])) {
throw new ConfigurationException('Error parsing yaml. Config `whitelist: include:` should be an array');
}
foreach ($coverage['whitelist']['include'] as $fileOrDir) {
$finder = !str_contains($fileOrDir, '*')
? [Configuration::projectDir() . DIRECTORY_SEPARATOR . $fileOrDir]
: $this->matchWildcardPattern($fileOrDir);
foreach ($finder as $file) {
$filter->includeFile((string)$file);
}
}
}
if (isset($coverage['whitelist']['exclude'])) {
if (!is_array($coverage['whitelist']['exclude'])) {
throw new ConfigurationException('Error parsing yaml. Config `whitelist: exclude:` should be an array');
}
foreach ($coverage['whitelist']['exclude'] as $fileOrDir) {
try {
$finder = !str_contains($fileOrDir, '*')
? [Configuration::projectDir() . DIRECTORY_SEPARATOR . $fileOrDir]
: $this->matchWildcardPattern($fileOrDir);
foreach ($finder as $file) {
$filter->excludeFile((string)$file);
}
} catch (DirectoryNotFoundException) {
continue;
}
}
}
return $this;
}
private function newWhiteList(array $whitelist): self
{
$include = $whitelist['include'] ?? [];
$exclude = $whitelist['exclude'] ?? [];
if (!is_array($include)) {
throw new ConfigurationException('Error parsing yaml. Config `whitelist: include:` should be an array');
}
if (!is_array($exclude)) {
throw new ConfigurationException('Error parsing yaml. Config `whitelist: exclude:` should be an array');
}
if (count($exclude) === 0 && count($include) === 0) {
return $this;
}
if (count($include) === 0) {
$include = [
Configuration::projectDir() . DIRECTORY_SEPARATOR . '*'
];
}
$allIncludedFiles = [];
foreach ($include as $fileOrDir) {
$finder = !str_contains($fileOrDir, '*')
? $this->matchFileOrDirectory($fileOrDir)
: $this->matchWildcardPattern($fileOrDir);
$allIncludedFiles += iterator_to_array($finder->getIterator());
}
$allExcludedFiles = [];
foreach ($exclude as $fileOrDir) {
try {
$finder = !str_contains($fileOrDir, '*')
? $this->matchFileOrDirectory($fileOrDir)
: $this->matchWildcardPattern($fileOrDir);
$allExcludedFiles += iterator_to_array($finder->getIterator());
} catch (DirectoryNotFoundException) {
continue;
}
}
$coveredFiles = array_diff($allIncludedFiles, $allExcludedFiles);
foreach ($coveredFiles as $coveredFile) {
$this->phpUnitFilter->includeFile((string)$coveredFile);
}
return $this;
}
/**
* @throws ModuleException
*/
public function blackList(array $config): self
{
if (isset($config['coverage']['blacklist'])) {
throw new ModuleException($this, 'The blacklist functionality has been removed from PHPUnit 5,'
. ' please remove blacklist section from configuration.');
}
return $this;
}
private function matchFileOrDirectory(string $fileOrDir): Finder
{
$fullPath = Configuration::projectDir() . $fileOrDir;
$finder = Finder::create();
if (is_dir($fullPath)) {
$finder->in($fullPath);
$finder->name('*.php');
} else {
$finder->in(dirname($fullPath));
$finder->name(basename($fullPath));
}
$finder->ignoreVCS(true)->files();
return $finder;
}
protected function matchWildcardPattern(string $pattern): Finder
{
$finder = Finder::create();
$fileOrDir = str_replace('\\', '/', $pattern);
$parts = explode('/', $fileOrDir);
$file = array_pop($parts);
if ($file === '*') {
$file = '*.php';
}
$finder->name($file);
if ($parts !== []) {
$lastPath = array_pop($parts);
if ($lastPath === '*') {
$finder->in(Configuration::projectDir() . implode('/', $parts));
} else {
$finder->in(Configuration::projectDir() . implode('/', [...$parts, $lastPath]));
}
}
$finder->ignoreVCS(true)->files();
return $finder;
}
public function getFilter(): PhpUnitFilter
{
return $this->phpUnitFilter;
}
}