Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 150 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Util;
11
 
12
use const DIRECTORY_SEPARATOR;
13
use function class_exists;
14
use function defined;
15
use function dirname;
16
use function is_dir;
17
use function realpath;
18
use function sprintf;
19
use function strpos;
20
use function sys_get_temp_dir;
21
use Composer\Autoload\ClassLoader;
22
use DeepCopy\DeepCopy;
23
use Doctrine\Instantiator\Instantiator;
24
use PharIo\Manifest\Manifest;
25
use PharIo\Version\Version as PharIoVersion;
26
use PhpParser\Parser;
27
use PHPUnit\Framework\TestCase;
28
use ReflectionClass;
29
use SebastianBergmann\CliParser\Parser as CliParser;
30
use SebastianBergmann\CodeCoverage\CodeCoverage;
31
use SebastianBergmann\CodeUnit\CodeUnit;
32
use SebastianBergmann\CodeUnitReverseLookup\Wizard;
33
use SebastianBergmann\Comparator\Comparator;
34
use SebastianBergmann\Complexity\Calculator;
35
use SebastianBergmann\Diff\Diff;
36
use SebastianBergmann\Environment\Runtime;
37
use SebastianBergmann\Exporter\Exporter;
38
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
39
use SebastianBergmann\GlobalState\Snapshot;
40
use SebastianBergmann\Invoker\Invoker;
41
use SebastianBergmann\LinesOfCode\Counter;
42
use SebastianBergmann\ObjectEnumerator\Enumerator;
43
use SebastianBergmann\RecursionContext\Context;
44
use SebastianBergmann\ResourceOperations\ResourceOperations;
45
use SebastianBergmann\Template\Template;
46
use SebastianBergmann\Timer\Timer;
47
use SebastianBergmann\Type\TypeName;
48
use SebastianBergmann\Version;
49
use TheSeer\Tokenizer\Tokenizer;
50
 
51
/**
52
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
53
 */
54
final class ExcludeList
55
{
56
    /**
57
     * @var array<string,int>
58
     */
59
    private const EXCLUDED_CLASS_NAMES = [
60
        // composer
621 lars 61
        ClassLoader::class => 1,
148 lars 62
 
63
        // doctrine/instantiator
621 lars 64
        Instantiator::class => 1,
148 lars 65
 
66
        // myclabs/deepcopy
621 lars 67
        DeepCopy::class => 1,
148 lars 68
 
69
        // nikic/php-parser
621 lars 70
        Parser::class => 1,
148 lars 71
 
72
        // phar-io/manifest
621 lars 73
        Manifest::class => 1,
148 lars 74
 
75
        // phar-io/version
621 lars 76
        PharIoVersion::class => 1,
148 lars 77
 
78
        // phpdocumentor/type-resolver
621 lars 79
        Type::class => 1,
148 lars 80
 
81
        // phpunit/phpunit
621 lars 82
        TestCase::class => 2,
148 lars 83
 
84
        // phpunit/php-code-coverage
621 lars 85
        CodeCoverage::class => 1,
148 lars 86
 
87
        // phpunit/php-file-iterator
88
        FileIteratorFacade::class => 1,
89
 
90
        // phpunit/php-invoker
621 lars 91
        Invoker::class => 1,
148 lars 92
 
93
        // phpunit/php-text-template
621 lars 94
        Template::class => 1,
148 lars 95
 
96
        // phpunit/php-timer
621 lars 97
        Timer::class => 1,
148 lars 98
 
99
        // sebastian/cli-parser
621 lars 100
        CliParser::class => 1,
148 lars 101
 
102
        // sebastian/code-unit
621 lars 103
        CodeUnit::class => 1,
148 lars 104
 
105
        // sebastian/code-unit-reverse-lookup
621 lars 106
        Wizard::class => 1,
148 lars 107
 
108
        // sebastian/comparator
621 lars 109
        Comparator::class => 1,
148 lars 110
 
111
        // sebastian/complexity
621 lars 112
        Calculator::class => 1,
148 lars 113
 
114
        // sebastian/diff
621 lars 115
        Diff::class => 1,
148 lars 116
 
117
        // sebastian/environment
621 lars 118
        Runtime::class => 1,
148 lars 119
 
120
        // sebastian/exporter
621 lars 121
        Exporter::class => 1,
148 lars 122
 
123
        // sebastian/global-state
621 lars 124
        Snapshot::class => 1,
148 lars 125
 
126
        // sebastian/lines-of-code
621 lars 127
        Counter::class => 1,
148 lars 128
 
129
        // sebastian/object-enumerator
621 lars 130
        Enumerator::class => 1,
148 lars 131
 
132
        // sebastian/recursion-context
621 lars 133
        Context::class => 1,
148 lars 134
 
135
        // sebastian/resource-operations
136
        ResourceOperations::class => 1,
137
 
138
        // sebastian/type
621 lars 139
        TypeName::class => 1,
148 lars 140
 
141
        // sebastian/version
621 lars 142
        Version::class => 1,
148 lars 143
 
144
        // theseer/tokenizer
621 lars 145
        Tokenizer::class => 1,
148 lars 146
    ];
147
 
148
    /**
149
     * @var string[]
150
     */
151
    private static $directories = [];
152
 
153
    /**
154
     * @var bool
155
     */
156
    private static $initialized = false;
157
 
158
    public static function addDirectory(string $directory): void
159
    {
160
        if (!is_dir($directory)) {
161
            throw new Exception(
162
                sprintf(
163
                    '"%s" is not a directory',
164
                    $directory
165
                )
166
            );
167
        }
168
 
169
        self::$directories[] = realpath($directory);
170
    }
171
 
172
    /**
173
     * @throws Exception
174
     *
175
     * @return string[]
176
     */
177
    public function getExcludedDirectories(): array
178
    {
179
        $this->initialize();
180
 
181
        return self::$directories;
182
    }
183
 
184
    /**
185
     * @throws Exception
186
     */
187
    public function isExcluded(string $file): bool
188
    {
189
        if (defined('PHPUNIT_TESTSUITE')) {
190
            return false;
191
        }
192
 
193
        $this->initialize();
194
 
195
        foreach (self::$directories as $directory) {
196
            if (strpos($file, $directory) === 0) {
197
                return true;
198
            }
199
        }
200
 
201
        return false;
202
    }
203
 
204
    /**
205
     * @throws Exception
206
     */
207
    private function initialize(): void
208
    {
209
        if (self::$initialized) {
210
            return;
211
        }
212
 
213
        foreach (self::EXCLUDED_CLASS_NAMES as $className => $parent) {
214
            if (!class_exists($className)) {
215
                continue;
216
            }
217
 
218
            $directory = (new ReflectionClass($className))->getFileName();
219
 
220
            for ($i = 0; $i < $parent; $i++) {
221
                $directory = dirname($directory);
222
            }
223
 
224
            self::$directories[] = $directory;
225
        }
226
 
227
        // Hide process isolation workaround on Windows.
228
        if (DIRECTORY_SEPARATOR === '\\') {
229
            // tempnam() prefix is limited to first 3 chars.
230
            // @see https://php.net/manual/en/function.tempnam.php
231
            self::$directories[] = sys_get_temp_dir() . '\\PHP';
232
        }
233
 
234
        self::$initialized = true;
235
    }
236
}