Failed Conditions
Push — master ( ddb3cd...4476ec )
by Marco
11:47
created

setCustomRepositoryClassName()   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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Mapping;
6
7
/**
8
 * Class EntityClassMetadata
9
 *
10
 */
11
abstract class EntityClassMetadata extends ComponentMetadata
12
{
13
    /** @var string The name of the Entity */
14
    protected $entityName;
15
16
    /**
17
     * @var string|null The name of the custom repository class used for the entity class.
18
     */
19
    protected $customRepositoryClassName;
20
21
    /**
22
     * @var Property|null The field which is used for versioning in optimistic locking (if any).
23
     */
24
    protected $declaredVersion;
25
26
    /**
27
     * Whether this class describes the mapping of a read-only class.
28
     * That means it is never considered for change-tracking in the UnitOfWork.
29
     * It is a very helpful performance optimization for entities that are immutable,
30
     * either in your domain or through the relation database (coming from a view,
31
     * or a history table for example).
32
     *
33
     * @var bool
34
     */
35
    protected $readOnly = false;
36
37
    /**
38
     * List of all sub-classes (descendants) metadata.
39
     *
40
     * @var SubClassMetadata[]
41
     */
42
    protected $subClasses = [];
43
44
    /**
45
     * The named queries allowed to be called directly from Repository.
46
     *
47
     * @var string[]
48
     */
49
    protected $namedQueries = [];
50
51
    /**
52
     * READ-ONLY: The named native queries allowed to be called directly from Repository.
53
     *
54
     * A native SQL named query definition has the following structure:
55
     * <pre>
56
     * array(
57
     *     'name'               => <query name>,
58
     *     'query'              => <sql query>,
59
     *     'resultClass'        => <class of the result>,
60
     *     'resultSetMapping'   => <name of a SqlResultSetMapping>
61
     * )
62
     * </pre>
63
     *
64
     * @var string[][]
65
     */
66
    protected $namedNativeQueries = [];
67
68
    /**
69
     * READ-ONLY: The mappings of the results of native SQL queries.
70
     *
71
     * A native result mapping definition has the following structure:
72
     * <pre>
73
     * array(
74
     *     'name'               => <result name>,
75
     *     'entities'           => array(<entity result mapping>),
76
     *     'columns'            => array(<column result mapping>)
77
     * )
78
     * </pre>
79
     *
80
     * @var mixed[][]
81
     */
82
    protected $sqlResultSetMappings = [];
83
84
    /**
85
     * READ-ONLY: The registered lifecycle callbacks for entities of this class.
86
     *
87
     * @var string[][]
88
     */
89
    protected $lifecycleCallbacks = [];
90
91
    /**
92
     * READ-ONLY: The registered entity listeners.
93
     *
94
     * @var string[][]
95
     */
96
    protected $entityListeners = [];
97
98
    /**
99
     * READ-ONLY: The field names of all fields that are part of the identifier/primary key
100
     * of the mapped entity class.
101
     *
102
     * @var string[]
103
     */
104
    protected $identifier = [];
105
106
    /**
107
     * READ-ONLY: The primary table metadata.
108
     *
109
     * @var TableMetadata
110
     */
111
    protected $table;
112
113
    /**
114
     * MappedSuperClassMetadata constructor.
115
     */
116
    public function __construct(string $className, ClassMetadataBuildingContext $metadataBuildingContext)
117
    {
118
        parent::__construct($className);
0 ignored issues
show
Bug introduced by
The call to Doctrine\ORM\Mapping\Com...Metadata::__construct() has too few arguments starting with metadataBuildingContext. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
        parent::/** @scrutinizer ignore-call */ 
119
                __construct($className);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
119
120
        $this->entityName = $className;
121
    }
122
123
    public function getEntityName() : string
124
    {
125
        return $this->entityName;
126
    }
127
128
    public function setEntityName(string $entityName) : void
129
    {
130
        $this->entityName = $entityName;
131
    }
132
133
    public function getCustomRepositoryClassName() : ?string
134
    {
135
        return $this->customRepositoryClassName;
136
    }
137
138
    public function setCustomRepositoryClassName(?string $customRepositoryClassName) : void
139
    {
140
        $this->customRepositoryClassName = $customRepositoryClassName;
141
    }
142
143
    public function getDeclaredVersion() : ?Property
144
    {
145
        return $this->declaredVersion;
146
    }
147
148
    public function setDeclaredVersion(Property $property) : void
149
    {
150
        $this->declaredVersion = $property;
151
    }
152
153
    public function getVersion() : ?Property
154
    {
155
        /** @var ComponentMetadata|null $parent */
156
        $parent  = $this->parent;
157
        $version = $this->declaredVersion;
158
159
        if ($parent && ! $version) {
160
            $version = $parent->getVersion();
0 ignored issues
show
Bug introduced by
The method getVersion() does not exist on Doctrine\ORM\Mapping\ComponentMetadata. It seems like you code against a sub-type of Doctrine\ORM\Mapping\ComponentMetadata such as Doctrine\ORM\Mapping\MappedSuperClassMetadata or Doctrine\ORM\Mapping\EntityClassMetadata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

160
            /** @scrutinizer ignore-call */ 
161
            $version = $parent->getVersion();
Loading history...
161
        }
162
163
        return $version;
164
    }
165
166
    public function isVersioned() : bool
167
    {
168
        return $this->getVersion() !== null;
169
    }
170
171
    public function setReadOnly(bool $readOnly) : void
172
    {
173
        $this->readOnly = $readOnly;
174
    }
175
176
    public function isReadOnly() : bool
177
    {
178
        return $this->readOnly;
179
    }
180
181
    /**
182
     *
183
     * @throws MappingException
184
     */
185
    public function addSubClass(SubClassMetadata $subClassMetadata) : void
186
    {
187
        $superClassMetadata = $this->getSuperClass();
0 ignored issues
show
Bug introduced by
The method getSuperClass() does not exist on Doctrine\ORM\Mapping\EntityClassMetadata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

187
        /** @scrutinizer ignore-call */ 
188
        $superClassMetadata = $this->getSuperClass();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
188
189
        while ($superClassMetadata !== null) {
190
            if ($superClassMetadata->entityName === $subClassMetadata->entityName) {
191
                throw new MappingException(
192
                    sprintf(
193
                        'Circular inheritance mapping detected: "%s" have itself as superclass when extending "%s".',
194
                        $subClassMetadata->entityName,
195
                        $superClassMetadata->entityName
196
                    )
197
                );
198
            }
199
200
            $superClassMetadata->subClasses[] = $subClassMetadata;
201
202
            $superClassMetadata = $superClassMetadata->parent;
203
        }
204
205
        $this->subClasses[] = $subClassMetadata;
206
    }
207
208
    public function hasSubClasses() : bool
209
    {
210
        return (bool) $this->subClasses;
211
    }
212
213
    public function getSubClassIterator() : \Iterator
214
    {
215
        $iterator = new \AppendIterator();
216
217
        foreach ($this->subClasses as $subClassMetadata) {
218
            $iterator->append($subClassMetadata->getSubClassIterator());
219
        }
220
221
        $iterator->append(new \ArrayIterator($this->subClasses));
222
223
        return $iterator;
224
    }
225
226
    /**
227
     * Adds a named query.
228
     *
229
     * @throws MappingException
230
     */
231
    public function addNamedQuery(string $name, string $dqlQuery) : void
232
    {
233
        if (isset($this->namedQueries[$name])) {
234
            throw MappingException::duplicateQueryMapping($this->entityName, $name);
235
        }
236
237
        $this->namedQueries[$name] = $dqlQuery;
238
    }
239
240
    /**
241
     * Gets a named query.
242
     *
243
     * @param string $queryName The query name.
244
     *
245
     * @throws MappingException
246
     */
247
    public function getNamedQuery($queryName) : string
248
    {
249
        if (! isset($this->namedQueries[$queryName])) {
250
            throw MappingException::queryNotFound($this->entityName, $queryName);
251
        }
252
253
        return $this->namedQueries[$queryName];
254
    }
255
256
    /**
257
     * Gets all named queries of the class.
258
     *
259
     * @return string[]
260
     */
261
    public function getNamedQueries() : array
262
    {
263
        return $this->namedQueries;
264
    }
265
266
    /**
267
     * Gets a named native query.
268
     *
269
     * @param string $queryName The native query name.
270
     *
271
     * @return string[]
272
     *
273
     * @throws MappingException
274
     *
275
     * @todo guilhermeblanco This should return an object instead
276
     */
277
    public function getNamedNativeQuery($queryName) : array
278
    {
279
        if (! isset($this->namedNativeQueries[$queryName])) {
280
            throw MappingException::queryNotFound($this->entityName, $queryName);
281
        }
282
283
        return $this->namedNativeQueries[$queryName];
284
    }
285
286
    /**
287
     * Gets all named native queries of the class.
288
     *
289
     * @return string[][]
290
     */
291
    public function getNamedNativeQueries() : array
292
    {
293
        return $this->namedNativeQueries;
294
    }
295
296
    /**
297
     * Gets the result set mapping.
298
     *
299
     * @param string $name The result set mapping name.
300
     *
301
     * @return mixed[]
302
     *
303
     * @throws MappingException
304
     *
305
     * @todo guilhermeblanco This should return an object instead
306
     */
307
    public function getSqlResultSetMapping($name) : array
308
    {
309
        if (! isset($this->sqlResultSetMappings[$name])) {
310
            throw MappingException::resultMappingNotFound($this->entityName, $name);
311
        }
312
313
        return $this->sqlResultSetMappings[$name];
314
    }
315
316
    /**
317
     * Gets all sql result set mappings of the class.
318
     *
319
     * @return mixed[][]
320
     */
321
    public function getSqlResultSetMappings() : array
322
    {
323
        return $this->sqlResultSetMappings;
324
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329
    public function addDeclaredProperty(Property $property) : void
330
    {
331
        parent::addDeclaredProperty($property);
332
333
        if ($property instanceof VersionFieldMetadata) {
334
            $this->setDeclaredVersion($property);
335
        }
336
    }
337
338
    abstract public function getRootClass() : RootClassMetadata;
339
}
340