Failed Conditions
Pull Request — master (#2)
by Jonathan
03:29
created

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