Completed
Pull Request — master (#50)
by Jonathan
04:13 queued 01:53
created

AnnotationDriver::getAllClassNames()   C

Complexity

Conditions 12
Paths 27

Size

Total Lines 67
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 13.912

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 67
ccs 29
cts 38
cp 0.7632
rs 6.9666
c 0
b 0
f 0
cc 12
nc 27
nop 0
crap 13.912

How to fix   Long Method    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
namespace Doctrine\Persistence\Mapping\Driver;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Doctrine\Persistence\Mapping\MappingException;
7
use FilesystemIterator;
8
use RecursiveDirectoryIterator;
9
use RecursiveIteratorIterator;
10
use RecursiveRegexIterator;
11
use ReflectionClass;
12
use RegexIterator;
13
use function array_merge;
14
use function array_unique;
15
use function assert;
16
use function get_class;
17
use function get_declared_classes;
18
use function in_array;
19
use function is_dir;
20
use function is_string;
21
use function preg_match;
22
use function preg_quote;
23
use function realpath;
24
use function str_replace;
25
use function strpos;
26
27
/**
28
 * The AnnotationDriver reads the mapping metadata from docblock annotations.
29
 */
30
abstract class AnnotationDriver implements MappingDriver
31
{
32
    /**
33
     * The annotation reader.
34
     *
35
     * @var Reader
36
     */
37
    protected $reader;
38
39
    /**
40
     * The paths where to look for mapping files.
41
     *
42
     * @var string[]
43
     */
44
    protected $paths = [];
45
46
    /**
47
     * The paths excluded from path where to look for mapping files.
48
     *
49
     * @var string[]
50
     */
51
    protected $excludePaths = [];
52
53
    /**
54
     * The file extension of mapping documents.
55
     *
56
     * @var string
57
     */
58
    protected $fileExtension = '.php';
59
60
    /**
61
     * Cache for AnnotationDriver#getAllClassNames().
62
     *
63
     * @var string[]|null
64
     */
65
    protected $classNames;
66
67
    /**
68
     * Name of the entity annotations as keys.
69
     *
70
     * @var string[]
71
     */
72
    protected $entityAnnotationClasses = [];
73
74
    /**
75
     * Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
76
     * docblock annotations.
77
     *
78
     * @param Reader               $reader The AnnotationReader to use, duck-typed.
79
     * @param string|string[]|null $paths  One or multiple paths where mapping classes can be found.
80
     */
81 1
    public function __construct(Reader $reader, $paths = null)
82
    {
83 1
        $this->reader = $reader;
84
85 1
        if (! $paths) {
86
            return;
87
        }
88
89 1
        $this->addPaths((array) $paths);
90 1
    }
91
92
    /**
93
     * Appends lookup paths to metadata driver.
94
     *
95
     * @param string[] $paths
96
     *
97
     * @return void
98
     */
99 1
    public function addPaths(array $paths)
100
    {
101 1
        $this->paths = array_unique(array_merge($this->paths, $paths));
102 1
    }
103
104
    /**
105
     * Retrieves the defined metadata lookup paths.
106
     *
107
     * @return string[]
108
     */
109
    public function getPaths() : array
110
    {
111
        return $this->paths;
112
    }
113
114
    /**
115
     * Append exclude lookup paths to metadata driver.
116
     *
117
     * @param string[] $paths
118
     */
119
    public function addExcludePaths(array $paths) : void
120
    {
121
        $this->excludePaths = array_unique(array_merge($this->excludePaths, $paths));
122
    }
123
124
    /**
125
     * Retrieve the defined metadata lookup exclude paths.
126
     *
127
     * @return string[]
128
     */
129
    public function getExcludePaths() : array
130
    {
131
        return $this->excludePaths;
132
    }
133
134
    /**
135
     * Retrieve the current annotation reader
136
     */
137
    public function getReader() : Reader
138
    {
139
        return $this->reader;
140
    }
141
142
    /**
143
     * Gets the file extension used to look for mapping files under.
144
     */
145
    public function getFileExtension() : string
146
    {
147
        return $this->fileExtension;
148
    }
149
150
    /**
151
     * Sets the file extension used to look for mapping files under.
152
     *
153
     * @param string $fileExtension The file extension to set.
154
     */
155
    public function setFileExtension(string $fileExtension) : void
156
    {
157
        $this->fileExtension = $fileExtension;
158
    }
159
160
    /**
161
     * Returns whether the class with the specified name is transient. Only non-transient
162
     * classes, that is entities and mapped superclasses, should have their metadata loaded.
163
     *
164
     * A class is non-transient if it is annotated with an annotation
165
     * from the {@see AnnotationDriver::entityAnnotationClasses}.
166
     */
167 1
    public function isTransient(string $className) : bool
168
    {
169 1
        $classAnnotations = $this->reader->getClassAnnotations(new ReflectionClass($className));
170
171 1
        foreach ($classAnnotations as $annot) {
172 1
            if (isset($this->entityAnnotationClasses[get_class($annot)])) {
173 1
                return false;
174
            }
175
        }
176
177 1
        return true;
178
    }
179
180
    /**
181
     * {@inheritDoc}
182
     */
183 1
    public function getAllClassNames() : array
184
    {
185 1
        if ($this->classNames !== null) {
186
            return $this->classNames;
187
        }
188
189 1
        if (! $this->paths) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->paths of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
190
            throw MappingException::pathRequired();
191
        }
192
193 1
        $classes       = [];
194 1
        $includedFiles = [];
195
196 1
        foreach ($this->paths as $path) {
197 1
            if (! is_dir($path)) {
198
                throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
199
            }
200
201 1
            $iterator = new RegexIterator(
202 1
                new RecursiveIteratorIterator(
203 1
                    new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
204 1
                    RecursiveIteratorIterator::LEAVES_ONLY
205
                ),
206 1
                '/^.+' . preg_quote($this->fileExtension) . '$/i',
207 1
                RecursiveRegexIterator::GET_MATCH
208
            );
209
210 1
            foreach ($iterator as $file) {
211 1
                $sourceFile = $file[0];
212
213 1
                if (! preg_match('(^phar:)i', $sourceFile)) {
214 1
                    $sourceFile = realpath($sourceFile);
215
                }
216
217 1
                foreach ($this->excludePaths as $excludePath) {
218
                    $realpath = realpath($excludePath);
219
                    assert(is_string($realpath));
220
221
                    $exclude = str_replace('\\', '/', $realpath);
222
                    $current = str_replace('\\', '/', $sourceFile);
223
224
                    if (strpos($current, $exclude) !== false) {
225
                        continue 2;
226
                    }
227
                }
228
229 1
                require_once $sourceFile;
230
231 1
                $includedFiles[] = $sourceFile;
232
            }
233
        }
234
235 1
        $declared = get_declared_classes();
236
237 1
        foreach ($declared as $className) {
238 1
            $rc         = new ReflectionClass($className);
239 1
            $sourceFile = $rc->getFileName();
240 1
            if (! in_array($sourceFile, $includedFiles) || $this->isTransient($className)) {
241 1
                continue;
242
            }
243
244 1
            $classes[] = $className;
245
        }
246
247 1
        $this->classNames = $classes;
248
249 1
        return $classes;
250
    }
251
}
252