1<?php
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
11/**
12 * Utility class for blacklisting PHPUnit's own source code files.
13 */
14class PHPUnit_Util_Blacklist
15{
16    /**
17     * @var array
18     */
19    public static $blacklistedClassNames = [
20        'File_Iterator'                               => 1,
21        'PHP_Invoker'                                 => 1,
22        'PHP_Timer'                                   => 1,
23        'PHP_Token'                                   => 1,
24        'PHPUnit_Framework_TestCase'                  => 2,
25        'PHPUnit_Extensions_Database_TestCase'        => 2,
26        'PHPUnit_Framework_MockObject_Generator'      => 2,
27        'Text_Template'                               => 1,
28        'Symfony\Component\Yaml\Yaml'                 => 1,
29        'SebastianBergmann\CodeCoverage\CodeCoverage' => 1,
30        'SebastianBergmann\Diff\Diff'                 => 1,
31        'SebastianBergmann\Environment\Runtime'       => 1,
32        'SebastianBergmann\Comparator\Comparator'     => 1,
33        'SebastianBergmann\Exporter\Exporter'         => 1,
34        'SebastianBergmann\GlobalState\Snapshot'      => 1,
35        'SebastianBergmann\RecursionContext\Context'  => 1,
36        'SebastianBergmann\Version'                   => 1,
37        'Composer\Autoload\ClassLoader'               => 1,
38        'Doctrine\Instantiator\Instantiator'          => 1,
39        'phpDocumentor\Reflection\DocBlock'           => 1,
40        'Prophecy\Prophet'                            => 1,
41        'DeepCopy\DeepCopy'                           => 1
42    ];
43
44    /**
45     * @var array
46     */
47    private static $directories;
48
49    /**
50     * @return array
51     */
52    public function getBlacklistedDirectories()
53    {
54        $this->initialize();
55
56        return self::$directories;
57    }
58
59    /**
60     * @param string $file
61     *
62     * @return bool
63     */
64    public function isBlacklisted($file)
65    {
66        if (defined('PHPUNIT_TESTSUITE')) {
67            return false;
68        }
69
70        $this->initialize();
71
72        foreach (self::$directories as $directory) {
73            if (strpos($file, $directory) === 0) {
74                return true;
75            }
76        }
77
78        return false;
79    }
80
81    private function initialize()
82    {
83        if (self::$directories === null) {
84            self::$directories = [];
85
86            foreach (self::$blacklistedClassNames as $className => $parent) {
87                if (!class_exists($className)) {
88                    continue;
89                }
90
91                $reflector = new ReflectionClass($className);
92                $directory = $reflector->getFileName();
93
94                for ($i = 0; $i < $parent; $i++) {
95                    $directory = dirname($directory);
96                }
97
98                self::$directories[] = $directory;
99            }
100
101            // Hide process isolation workaround on Windows.
102            // @see PHPUnit_Util_PHP::factory()
103            // @see PHPUnit_Util_PHP_Windows::process()
104            if (DIRECTORY_SEPARATOR === '\\') {
105                // tempnam() prefix is limited to first 3 chars.
106                // @see http://php.net/manual/en/function.tempnam.php
107                self::$directories[] = sys_get_temp_dir() . '\\PHP';
108            }
109        }
110    }
111}
112