Failed Conditions
Push — master ( 3a6c46...2fbe69 )
by Luís
36s queued 11s
created

AnnotationDriver::isTransient()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 3
nc 3
nop 1
crap 3
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, $paths = null)
80
    {
81 1
        $this->reader = $reader;
82 1
        if (! $paths) {
83
            return;
84
        }
85
86 1
        $this->addPaths((array) $paths);
87 1
    }
88
89
    /**
90
     * Appends lookup paths to metadata driver.
91
     *
92
     * @param string[] $paths
93
     *
94
     * @return void
95
     */
96 1
    public function addPaths(array $paths)
97
    {
98 1
        $this->paths = array_unique(array_merge($this->paths, $paths));
99 1
    }
100
101
    /**
102
     * Retrieves the defined metadata lookup paths.
103
     *
104
     * @return string[]
105
     */
106
    public function getPaths()
107
    {
108
        return $this->paths;
109
    }
110
111
    /**
112
     * Append exclude lookup paths to metadata driver.
113
     *
114
     * @param string[] $paths
115
     */
116
    public function addExcludePaths(array $paths)
117
    {
118
        $this->excludePaths = array_unique(array_merge($this->excludePaths, $paths));
119
    }
120
121
    /**
122
     * Retrieve the defined metadata lookup exclude paths.
123
     *
124
     * @return string[]
125
     */
126
    public function getExcludePaths()
127
    {
128
        return $this->excludePaths;
129
    }
130
131
    /**
132
     * Retrieve the current annotation reader
133
     *
134
     * @return Reader
135
     */
136
    public function getReader()
137
    {
138
        return $this->reader;
139
    }
140
141
    /**
142
     * Gets the file extension used to look for mapping files under.
143
     *
144
     * @return string
145
     */
146
    public function getFileExtension()
147
    {
148
        return $this->fileExtension;
149
    }
150
151
    /**
152
     * Sets the file extension used to look for mapping files under.
153
     *
154
     * @param string $fileExtension The file extension to set.
155
     *
156
     * @return void
157
     */
158
    public function setFileExtension($fileExtension)
159
    {
160
        $this->fileExtension = $fileExtension;
161
    }
162
163
    /**
164
     * Returns whether the class with the specified name is transient. Only non-transient
165
     * classes, that is entities and mapped superclasses, should have their metadata loaded.
166
     *
167
     * A class is non-transient if it is annotated with an annotation
168
     * from the {@see AnnotationDriver::entityAnnotationClasses}.
169
     *
170
     * @param string $className
171
     *
172
     * @return bool
173
     */
174 1
    public function isTransient($className)
175
    {
176 1
        $classAnnotations = $this->reader->getClassAnnotations(new ReflectionClass($className));
177
178 1
        foreach ($classAnnotations as $annot) {
179 1
            if (isset($this->entityAnnotationClasses[get_class($annot)])) {
180 1
                return false;
181
            }
182
        }
183 1
        return true;
184
    }
185
186
    /**
187
     * {@inheritDoc}
188
     */
189 1
    public function getAllClassNames()
190
    {
191 1
        if ($this->classNames !== null) {
192
            return $this->classNames;
193
        }
194
195 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...
196
            throw MappingException::pathRequired();
197
        }
198
199 1
        $classes       = [];
200 1
        $includedFiles = [];
201
202 1
        foreach ($this->paths as $path) {
203 1
            if (! is_dir($path)) {
204
                throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
205
            }
206
207 1
            $iterator = new RegexIterator(
208 1
                new RecursiveIteratorIterator(
209 1
                    new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
210 1
                    RecursiveIteratorIterator::LEAVES_ONLY
211
                ),
212 1
                '/^.+' . preg_quote($this->fileExtension) . '$/i',
213 1
                RecursiveRegexIterator::GET_MATCH
214
            );
215
216 1
            foreach ($iterator as $file) {
217 1
                $sourceFile = $file[0];
218
219 1
                if (! preg_match('(^phar:)i', $sourceFile)) {
220 1
                    $sourceFile = realpath($sourceFile);
221
                }
222
223 1
                foreach ($this->excludePaths as $excludePath) {
224
                    $exclude = str_replace('\\', '/', realpath($excludePath));
225
                    $current = str_replace('\\', '/', $sourceFile);
226
227
                    if (strpos($current, $exclude) !== false) {
228
                        continue 2;
229
                    }
230
                }
231
232 1
                require_once $sourceFile;
233
234 1
                $includedFiles[] = $sourceFile;
235
            }
236
        }
237
238 1
        $declared = get_declared_classes();
239
240 1
        foreach ($declared as $className) {
241 1
            $rc         = new ReflectionClass($className);
242 1
            $sourceFile = $rc->getFileName();
243 1
            if (! in_array($sourceFile, $includedFiles) || $this->isTransient($className)) {
244 1
                continue;
245
            }
246
247 1
            $classes[] = $className;
248
        }
249
250 1
        $this->classNames = $classes;
251
252 1
        return $classes;
253
    }
254
}
255