Passed
Push — types ( 30d5cd )
by Jonathan
04:47
created

AnnotationDriver::getFileExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
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 get_class;
16
use function get_declared_classes;
17
use function in_array;
18
use function is_dir;
19
use function preg_match;
20
use function preg_quote;
21
use function realpath;
22
use function str_replace;
23
use function strpos;
24
25
/**
26
 * The AnnotationDriver reads the mapping metadata from docblock annotations.
27
 */
28
abstract class AnnotationDriver implements MappingDriver
29
{
30
    /**
31
     * The annotation reader.
32
     *
33
     * @var Reader
34
     */
35
    protected $reader;
36
37
    /**
38
     * The paths where to look for mapping files.
39
     *
40
     * @var string[]
41
     */
42
    protected $paths = [];
43
44
    /**
45
     * The paths excluded from path where to look for mapping files.
46
     *
47
     * @var string[]
48
     */
49
    protected $excludePaths = [];
50
51
    /**
52
     * The file extension of mapping documents.
53
     *
54
     * @var string
55
     */
56
    protected $fileExtension = '.php';
57
58
    /**
59
     * Cache for AnnotationDriver#getAllClassNames().
60
     *
61
     * @var string[]|null
62
     */
63
    protected $classNames;
64
65
    /**
66
     * Name of the entity annotations as keys.
67
     *
68
     * @var string[]
69
     */
70
    protected $entityAnnotationClasses = [];
71
72
    /**
73
     * Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
74
     * docblock annotations.
75
     *
76
     * @param Reader               $reader The AnnotationReader to use, duck-typed.
77
     * @param string|string[]|null $paths  One or multiple paths where mapping classes can be found.
78
     */
79 1
    public function __construct(Reader $reader, $paths = null)
80
    {
81 1
        $this->reader = $reader;
82
83 1
        if (! $paths) {
84
            return;
85
        }
86
87 1
        $this->addPaths((array) $paths);
88 1
    }
89
90
    /**
91
     * Appends lookup paths to metadata driver.
92
     *
93
     * @param string[] $paths
94
     *
95
     * @return void
96
     */
97 1
    public function addPaths(array $paths)
98
    {
99 1
        $this->paths = array_unique(array_merge($this->paths, $paths));
100 1
    }
101
102
    /**
103
     * Retrieves the defined metadata lookup paths.
104
     *
105
     * @return string[]
106
     */
107
    public function getPaths() : array
108
    {
109
        return $this->paths;
110
    }
111
112
    /**
113
     * Append exclude lookup paths to metadata driver.
114
     *
115
     * @param string[] $paths
116
     */
117
    public function addExcludePaths(array $paths) : void
118
    {
119
        $this->excludePaths = array_unique(array_merge($this->excludePaths, $paths));
120
    }
121
122
    /**
123
     * Retrieve the defined metadata lookup exclude paths.
124
     *
125
     * @return string[]
126
     */
127
    public function getExcludePaths() : array
128
    {
129
        return $this->excludePaths;
130
    }
131
132
    /**
133
     * Retrieve the current annotation reader
134
     */
135
    public function getReader() : Reader
136
    {
137
        return $this->reader;
138
    }
139
140
    /**
141
     * Gets the file extension used to look for mapping files under.
142
     */
143
    public function getFileExtension() : string
144
    {
145
        return $this->fileExtension;
146
    }
147
148
    /**
149
     * Sets the file extension used to look for mapping files under.
150
     *
151
     * @param string $fileExtension The file extension to set.
152
     */
153
    public function setFileExtension(string $fileExtension) : void
154
    {
155
        $this->fileExtension = $fileExtension;
156
    }
157
158
    /**
159
     * Returns whether the class with the specified name is transient. Only non-transient
160
     * classes, that is entities and mapped superclasses, should have their metadata loaded.
161
     *
162
     * A class is non-transient if it is annotated with an annotation
163
     * from the {@see AnnotationDriver::entityAnnotationClasses}.
164
     */
165 1
    public function isTransient(string $className) : bool
166
    {
167 1
        $classAnnotations = $this->reader->getClassAnnotations(new ReflectionClass($className));
168
169 1
        foreach ($classAnnotations as $annot) {
170 1
            if (isset($this->entityAnnotationClasses[get_class($annot)])) {
171 1
                return false;
172
            }
173
        }
174
175 1
        return true;
176
    }
177
178
    /**
179
     * {@inheritDoc}
180
     */
181 1
    public function getAllClassNames() : array
182
    {
183 1
        if ($this->classNames !== null) {
184
            return $this->classNames;
185
        }
186
187 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...
188
            throw MappingException::pathRequired();
189
        }
190
191 1
        $classes       = [];
192 1
        $includedFiles = [];
193
194 1
        foreach ($this->paths as $path) {
195 1
            if (! is_dir($path)) {
196
                throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
197
            }
198
199 1
            $iterator = new RegexIterator(
200 1
                new RecursiveIteratorIterator(
201 1
                    new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
202 1
                    RecursiveIteratorIterator::LEAVES_ONLY
203
                ),
204 1
                '/^.+' . preg_quote($this->fileExtension) . '$/i',
205 1
                RecursiveRegexIterator::GET_MATCH
206
            );
207
208 1
            foreach ($iterator as $file) {
209 1
                $sourceFile = $file[0];
210
211 1
                if (! preg_match('(^phar:)i', $sourceFile)) {
212 1
                    $sourceFile = realpath($sourceFile);
213
                }
214
215 1
                foreach ($this->excludePaths as $excludePath) {
216
                    $realpath = realpath($excludePath);
217
                    assert(is_string($realpath));
0 ignored issues
show
introduced by
Function assert() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function is_string() should not be referenced via a fallback global name, but via a use statement.
Loading history...
218
219
                    $exclude = str_replace('\\', '/', $realpath);
220
                    $current = str_replace('\\', '/', $sourceFile);
221
222
                    if (strpos($current, $exclude) !== false) {
223
                        continue 2;
224
                    }
225
                }
226
227 1
                require_once $sourceFile;
228
229 1
                $includedFiles[] = $sourceFile;
230
            }
231
        }
232
233 1
        $declared = get_declared_classes();
234
235 1
        foreach ($declared as $className) {
236 1
            $rc         = new ReflectionClass($className);
237 1
            $sourceFile = $rc->getFileName();
238 1
            if (! in_array($sourceFile, $includedFiles) || $this->isTransient($className)) {
239 1
                continue;
240
            }
241
242 1
            $classes[] = $className;
243
        }
244
245 1
        $this->classNames = $classes;
246
247 1
        return $classes;
248
    }
249
}
250