forked from sebastianbergmann/phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcludeList.php
More file actions
235 lines (188 loc) · 5.77 KB
/
Copy pathExcludeList.php
File metadata and controls
235 lines (188 loc) · 5.77 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Util;
use const PHP_OS_FAMILY;
use function class_exists;
use function defined;
use function dirname;
use function is_dir;
use function realpath;
use function str_starts_with;
use function sys_get_temp_dir;
use Composer\Autoload\ClassLoader;
use DeepCopy\DeepCopy;
use PharIo\Manifest\Manifest;
use PharIo\Version\Version as PharIoVersion;
use PhpParser\Parser;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use SebastianBergmann\CliParser\Parser as CliParser;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeUnit\CodeUnit;
use SebastianBergmann\CodeUnitReverseLookup\Wizard;
use SebastianBergmann\Comparator\Comparator;
use SebastianBergmann\Complexity\Calculator;
use SebastianBergmann\Diff\Diff;
use SebastianBergmann\Environment\Runtime;
use SebastianBergmann\Exporter\Exporter;
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
use SebastianBergmann\GlobalState\Snapshot;
use SebastianBergmann\Invoker\Invoker;
use SebastianBergmann\LinesOfCode\Counter;
use SebastianBergmann\ObjectEnumerator\Enumerator;
use SebastianBergmann\ObjectReflector\ObjectReflector;
use SebastianBergmann\RecursionContext\Context;
use SebastianBergmann\Template\Template;
use SebastianBergmann\Timer\Timer;
use SebastianBergmann\Type\TypeName;
use SebastianBergmann\Version;
use staabm\SideEffectsDetector\SideEffectsDetector;
use TheSeer\Tokenizer\Tokenizer;
/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
final class ExcludeList
{
/**
* @var array<string,int>
*/
private const EXCLUDED_CLASS_NAMES = [
// composer
ClassLoader::class => 1,
// myclabs/deepcopy
DeepCopy::class => 1,
// nikic/php-parser
Parser::class => 1,
// phar-io/manifest
Manifest::class => 1,
// phar-io/version
PharIoVersion::class => 1,
// phpunit/phpunit
TestCase::class => 2,
// phpunit/php-code-coverage
CodeCoverage::class => 1,
// phpunit/php-file-iterator
FileIteratorFacade::class => 1,
// phpunit/php-invoker
Invoker::class => 1,
// phpunit/php-text-template
Template::class => 1,
// phpunit/php-timer
Timer::class => 1,
// sebastian/cli-parser
CliParser::class => 1,
// sebastian/code-unit
CodeUnit::class => 1,
// sebastian/code-unit-reverse-lookup
Wizard::class => 1,
// sebastian/comparator
Comparator::class => 1,
// sebastian/complexity
Calculator::class => 1,
// sebastian/diff
Diff::class => 1,
// sebastian/environment
Runtime::class => 1,
// sebastian/exporter
Exporter::class => 1,
// sebastian/global-state
Snapshot::class => 1,
// sebastian/lines-of-code
Counter::class => 1,
// sebastian/object-enumerator
Enumerator::class => 1,
// sebastian/object-reflector
ObjectReflector::class => 1,
// sebastian/recursion-context
Context::class => 1,
// sebastian/type
TypeName::class => 1,
// sebastian/version
Version::class => 1,
// staabm/side-effects-detector
SideEffectsDetector::class => 1,
// theseer/tokenizer
Tokenizer::class => 1,
];
/**
* @var list<string>
*/
private static array $directories = [];
private static bool $initialized = false;
private readonly bool $enabled;
/**
* @param non-empty-string $directory
*
* @throws InvalidDirectoryException
*/
public static function addDirectory(string $directory): void
{
if (!is_dir($directory)) {
throw new InvalidDirectoryException($directory);
}
self::$directories[] = realpath($directory);
}
public function __construct(?bool $enabled = null)
{
if ($enabled === null) {
$enabled = !defined('PHPUNIT_TESTSUITE');
}
$this->enabled = $enabled;
}
/**
* @return list<string>
*/
public function getExcludedDirectories(): array
{
self::initialize();
return self::$directories;
}
public function isExcluded(string $file): bool
{
if (!$this->enabled) {
return false;
}
self::initialize();
foreach (self::$directories as $directory) {
if (str_starts_with($file, $directory)) {
return true;
}
}
return false;
}
private static function initialize(): void
{
if (self::$initialized) {
return;
}
foreach (self::EXCLUDED_CLASS_NAMES as $className => $parent) {
if (!class_exists($className)) {
continue;
}
$directory = (new ReflectionClass($className))->getFileName();
for ($i = 0; $i < $parent; $i++) {
$directory = dirname($directory);
}
self::$directories[] = $directory;
}
/**
* Hide process isolation workaround on Windows:
* tempnam() prefix is limited to first 3 characters.
*
* @see https://php.net/manual/en/function.tempnam.php
*/
if (PHP_OS_FAMILY === 'Windows') {
// @codeCoverageIgnoreStart
self::$directories[] = sys_get_temp_dir() . '\\PHP';
// @codeCoverageIgnoreEnd
}
self::$initialized = true;
}
}