Passed
Pull Request — master (#271)
by Andreas
02:28
created

AnnotationRegistry::loadAnnotationClass()   C

Complexity

Conditions 15
Paths 29

Size

Total Lines 43
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
cc 15
eloc 23
c 8
b 0
f 0
nc 29
nop 1
dl 0
loc 43
rs 5.9166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\Common\Annotations;
21
22
final class AnnotationRegistry
23
{
24
    /**
25
     * A map of namespaces to use for autoloading purposes based on a PSR-0 convention.
26
     *
27
     * Contains the namespace as key and an array of directories as value. If the value is NULL
28
     * the include path is used for checking for the corresponding file.
29
     *
30
     * This autoloading mechanism does not utilize the PHP autoloading but implements autoloading on its own.
31
     *
32
     * @var string[][]|string[]|null[]
33
     */
34
    static private $autoloadNamespaces = [];
35
36
    /**
37
     * A map of autoloader callables.
38
     *
39
     * @var callable[]
40
     */
41
    static private $loaders = [];
42
43
    /**
44
     * An array of classes which cannot be found
45
     *
46
     * @var null[] indexed by class name
47
     */
48
    static private $failedToAutoload = [];
49
50
    /**
51
     * Whenever registerFile() was used. Disables use of standard autoloader.
52
     *
53
     * @var bool
54
     */
55
    static private $registerFileUsed = false;
56
57
    public static function reset() : void
58
    {
59
        self::$autoloadNamespaces = [];
60
        self::$loaders            = [];
61
        self::$failedToAutoload   = [];
62
    }
63
64
    /**
65
     * Registers file.
66
     *
67
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
68
     */
69
    public static function registerFile(string $file) : void
70
    {
71
        self::$registerFileUsed = true;
72
73
        require_once $file;
74
    }
75
76
    /**
77
     * Adds a namespace with one or many directories to look for files or null for the include path.
78
     *
79
     * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
80
     *
81
     * @param string            $namespace
82
     * @param string|array|null $dirs
83
     *
84
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
85
     */
86
    public static function registerAutoloadNamespace(string $namespace, $dirs = null) : void
87
    {
88
        self::$autoloadNamespaces[$namespace] = $dirs;
89
    }
90
91
    /**
92
     * Registers multiple namespaces.
93
     *
94
     * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
95
     *
96
     * @param string[][]|string[]|null[] $namespaces indexed by namespace name
97
     *
98
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
99
     */
100
    public static function registerAutoloadNamespaces(array $namespaces) : void
101
    {
102
        self::$autoloadNamespaces = \array_merge(self::$autoloadNamespaces, $namespaces);
103
    }
104
105
    /**
106
     * Registers an autoloading callable for annotations, much like spl_autoload_register().
107
     *
108
     * NOTE: These class loaders HAVE to be silent when a class was not found!
109
     * IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
110
     *
111
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
112
     */
113
    public static function registerLoader(callable $callable) : void
114
    {
115
        // Reset our static cache now that we have a new loader to work with
116
        self::$failedToAutoload   = [];
117
        self::$loaders[]          = $callable;
118
    }
119
120
    /**
121
     * Registers an autoloading callable for annotations, if it is not already registered
122
     *
123
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
124
     */
125
    public static function registerUniqueLoader(callable $callable) : void
126
    {
127
        if ( ! in_array($callable, self::$loaders, true) ) {
128
            self::registerLoader($callable);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

128
            /** @scrutinizer ignore-deprecated */ self::registerLoader($callable);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
129
        }
130
    }
131
132
    /**
133
     * Autoloads an annotation class silently.
134
     */
135
    public static function loadAnnotationClass(string $class) : bool
136
    {
137
        if (\class_exists($class, false)) {
138
            return true;
139
        }
140
141
        if (\array_key_exists($class, self::$failedToAutoload)) {
142
            return false;
143
        }
144
145
        foreach (self::$autoloadNamespaces AS $namespace => $dirs) {
146
            if (\strpos($class, $namespace) === 0) {
147
                $file = \str_replace('\\', \DIRECTORY_SEPARATOR, $class) . '.php';
148
149
                if ($dirs === null) {
150
                    if ($path = stream_resolve_include_path($file)) {
151
                        require $path;
152
                        return true;
153
                    }
154
                } else {
155
                    foreach((array) $dirs AS $dir) {
156
                        if (is_file($dir . \DIRECTORY_SEPARATOR . $file)) {
157
                            require $dir . \DIRECTORY_SEPARATOR . $file;
158
                            return true;
159
                        }
160
                    }
161
                }
162
            }
163
        }
164
165
        foreach (self::$loaders AS $loader) {
166
            if ($loader($class) === true) {
167
                return true;
168
            }
169
        }
170
171
        if (self::$loaders === [] && self::$autoloadNamespaces === [] && self::$registerFileUsed === false && \class_exists($class)) {
172
            return true;
173
        }
174
175
        self::$failedToAutoload[$class] = null;
176
177
        return false;
178
    }
179
}
180