8000 Initial work on removing the blacklist/whitelist terminology · sebastianbergmann/phpunit@8e9c76d · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e9c76d

Browse files
Initial work on removing the blacklist/whitelist terminology
1 parent 9855caf commit 8e9c76d

File tree

8 files changed

+264
-224
lines changed

8 files changed

+264
-224
lines changed

ChangeLog-9.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ All notable changes of the PHPUnit 9.3 release series are documented in this fil
77
### Changed
88

99
* [#4264](https://github.com/sebastianbergmann/phpunit/pull/4264): Refactor logical operator constraints
10+
* `PHPUnit\Util\Blacklist` is now deprecated, please use `PHPUnit\Util\ExcludeList` instead
1011

1112
[9.3.0]: https://github.com/sebastianbergmann/phpunit/compare/9.2...master

src/Framework/MockObject/Generator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class Generator
2222
/**
2323
* @var array
2424
*/
25-
private const BLACKLISTED_METHOD_NAMES = [
25+
private const EXCLUDED_METHOD_NAMES = [
2626
'__CLASS__' => true,
2727
'__DIR__' => true,
2828
'__FILE__' => true,
@@ -899,12 +899,12 @@ private function generateMockClassDeclaration(array $mockClassName, bool $isInte
899899

900900
private function canMockMethod(\ReflectionMethod $method): bool
901901
{
902-
return !($this->isConstructor($method) || $method->isFinal() || $method->isPrivate() || $this->isMethodNameBlacklisted($method->getName()));
902+
return !($this->isConstructor($method) || $method->isFinal() || $method->isPrivate() || $this->isMethodNameExcluded($method->getName()));
903903
}
904904

905-
private function isMethodNameBlacklisted(string $name): bool
905+
private function isMethodNameExcluded(string $name): bool
906906
{
907-
return isset(self::BLACKLISTED_METHOD_NAMES[$name]);
907+
return isset(self::EXCLUDED_METHOD_NAMES[$name]);
908908
}
909909

910910
private function getTemplate(string $template): Template

src/Framework/TestResult.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
namespace PHPUnit\Framework;
1111

1212
use PHPUnit\Framework\MockObject\Exception as MockObjectException;
13-
use PHPUnit\Util\Blacklist;
1413
use PHPUnit\Util\ErrorHandler;
14+
use PHPUnit\Util\ExcludeList;
1515
use PHPUnit\Util\Printer;
1616
use PHPUnit\Util\Test as TestUtil;
1717
use SebastianBergmann\CodeCoverage\CodeCoverage;
@@ -744,7 +744,7 @@ public function run(Test $test): void
744744
$test->addToAssertionCount(Assert::getCount());
745745

746746
if ($monitorFunctions) {
747-
$blacklist = new Blacklist;
747+
$excludeList = new ExcludeList;
748748

749749
/** @noinspection ForgottenDebugOutputInspection */
750750
$functions = \xdebug_get_monitored_functions();
@@ -753,7 +753,7 @@ public function run(Test $test): void
753753
\xdebug_stop_function_monitor();
754754

755755
foreach ($functions as $function) {
756-
if (!$blacklist->isBlacklisted($function['filename'])) {
756+
if (!$excludeList->isExcluded($function['filename'])) {
757757
$this->addFailure(
758758
$test,
759759
new RiskyTestError(

src/Util/Blacklist.php

Lines changed: 6 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -9,151 +9,14 @@
99
*/
1010
namespace PHPUnit\Util;
1111

12-
use Composer\Autoload\ClassLoader;
13-
use DeepCopy\DeepCopy;
14-
use Doctrine\Instantiator\Instantiator;
15-
use PharIo\Manifest\Manifest;
16-
use PharIo\Version\Version as PharIoVersion;
17-
use PHP_Token;
18-
use phpDocumentor\Reflection\DocBlock;
19-
use phpDocumentor\Reflection\Project;
20-
use phpDocumentor\Reflection\Type;
21-
use PHPUnit\Framework\TestCase;
22-
use Prophecy\Prophet;
23-
use SebastianBergmann\CodeCoverage\CodeCoverage;
24-
use SebastianBergmann\CodeUnit\CodeUnit;
25-
use SebastianBergmann\CodeUnitReverseLookup\Wizard;
26-
use SebastianBergmann\Comparator\Comparator;
27-
use SebastianBergmann\Diff\Diff;
28-
use SebastianBergmann\Environment\Runtime;
29-
use SebastianBergmann\Exporter\Exporter;
30-
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
31-
use SebastianBergmann\GlobalState\Snapshot;
32-
use SebastianBergmann\Invoker\Invoker;
33-
use SebastianBergmann\ObjectEnumerator\Enumerator;
34-
use SebastianBergmann\RecursionContext\Context;
35-
use SebastianBergmann\ResourceOperations\ResourceOperations;
36-
use SebastianBergmann\Template\Template;
37-
use SebastianBergmann\Timer\Timer;
38-
use SebastianBergmann\Type\TypeName;
39-
use SebastianBergmann\Version;
40-
use TheSeer\Tokenizer\Tokenizer;
41-
use Webmozart\Assert\Assert;
42-
12+
/**
13+
* @deprecated Use ExcludeList instead
14+
*/
4315
final class Blacklist
4416
{
45-
/**
46-
* @var array<string,int>
47-
*/
48-
private const BLACKLISTED_CLASS_NAMES = [
49-
// composer
50-
ClassLoader::class => 1,
51-
52-
// doctrine/instantiator
53-
Instantiator::class => 1,
54-
55-
// myclabs/deepcopy
56-
DeepCopy::class => 1,
57-
58-
// phar-io/manifest
59-
Manifest::class => 1,
60-
61-
// phar-io/version
62-
PharIoVersion::class => 1,
63-
64-
// phpdocumentor/reflection-common
65-
Project::class => 1,
66-
67-
// phpdocumentor/reflection-docblock
68-
DocBlock::class => 1,
69-
70-
// phpdocumentor/type-resolver
71-
Type::class => 1,
72-
73-
// phpspec/prophecy
74-
Prophet::class => 1,
75-
76-
// phpunit/phpunit
77-
TestCase::class => 2,
78-
79-
// phpunit/php-code-coverage
80-
CodeCoverage::class => 1,
81-
82-
// phpunit/php-file-iterator
83-
FileIteratorFacade::class => 1,
84-
85-
// phpunit/php-invoker
86-
Invoker::class => 1,
87-
88-
// phpunit/php-text-template
89-
Template::class => 1,
90-
91-
// phpunit/php-timer
92-
Timer::class => 1,
93-
94-
// phpunit/php-token-stream
95-
PHP_Token::class => 1,
96-
97-
// sebastian/code-unit
98-
CodeUnit::class => 1,
99-
100-
// sebastian/code-unit-reverse-lookup
101-
Wizard::class => 1,
102-
103-
// sebastian/comparator
104-
Comparator::class => 1,
105-
106-
// sebastian/diff
107-
Diff::class => 1,
108-
109-
// sebastian/environment
110-
Runtime::class => 1,
111-
112-
// sebastian/exporter
113-
Exporter::class => 1,
114-
115-
// sebastian/global-state
116-
Snapshot::class => 1,
117-
118-
// sebastian/object-enumerator
119-
Enumerator::class => 1,
120-
121-
// sebastian/recursion-context
122-
Context::class => 1,
123-
124-
// sebastian/resource-operations
125-
ResourceOperations::class => 1,
126-
127-
// sebastian/type
128-
TypeName::class => 1,
129-
130-
// sebastian/version
131-
Version::class => 1,
132-
133-
// theseer/tokenizer
134-
Tokenizer::class => 1,
135-
136-
// webmozart/assert
137-
Assert::class => 1,
138-
];
139-
140-
/**
141-
* @var string[]
142-
*/
143-
private static $directories;
144-
14517
public static function addDirectory(string $directory): void
14618
{
147-
if (!\is_dir($directory)) {
148-
throw new Exception(
149-
\sprintf(
150-
'"%s" is not a directory',
151-
$directory
152-
)
153-
);
154-
}
155-
156-
self::$directories[] = \realpath($directory);
19+
ExcludeList::addDirectory($directory);
15720
}
15821

15922
/**
@@ -163,69 +26,14 @@ public static function addDirectory(string $directory): void
16326
*/
16427
public function getBlacklistedDirectories(): array
16528
{
166-
$this->initialize();
167-
168-
return self::$directories;
29+
return (new ExcludeList)->getExcludedDirectories();
16930
}
17031

17132
/**
17233
* @throws Exception
17334
*/
17435
public function isBlacklisted(string $file): bool
17536
{
176-
if (\defined('PHPUNIT_TESTSUITE')) {
177-
return false;
178-
}
179-
180-
$this->initialize();
181-
182-
foreach (self::$directories as $directory) {
183-
if (\strpos($file, $directory) === 0) {
184-
return true;
185-
}
186-
}
187-
188-
return false;
189-
}
190-
191-
/**
192-
* @throws Exception
193-
*/
194-
private function initialize(): void
195-
{
196-
if (self::$directories === null) {
197-
self::$directories = [];
198-
199-
foreach (self::BLACKLISTED_CLASS_NAMES as $className => $parent) {
200-
if (!\class_exists($className)) {
201-
continue;
202-
}
203-
204-
try {
205-
$directory = (new \ReflectionClass($className))->getFileName();
206-
// @codeCoverageIgnoreStart
207-
} catch (\ReflectionException $e) {
208-
throw new Exception(
209-
$e->getMessage(),
210-
(int) $e->getCode(),
211-
$e
212-
);
213-
}
214-
// @codeCoverageIgnoreEnd
215-
216-
for ($i = 0; $i < $parent; $i++) {
217-
$directory = \dirname($directory);
218-
}
219-
220-
self::$directories[] = $directory;
221-
}
222-
223-
// Hide process isolation workaround on Windows.
224-
if (\DIRECTORY_SEPARATOR === '\\') {
225-
// tempnam() prefix is limited to first 3 chars.
226-
// @see https://php.net/manual/en/function.tempnam.php
227-
self::$directories[] = \sys_get_temp_dir() . '\\PHP';
228-
}
229-
}
37+
return (new ExcludeList)->isExcluded($file);
23038
}
23139
}

0 commit comments

Comments
 (0)
0