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

AnnotationDriver::isTransient()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
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 11
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
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 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 1
    public function addPaths(array $paths) : void
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));
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