Failed Conditions
Push — master ( 906c14...99b7d1 )
by Guilherme
09:01
created

EntityClassMetadata::addProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Mapping;
6
7
use AppendIterator;
8
use ArrayIterator;
9
use Iterator;
10
use function sprintf;
11
12
abstract class EntityClassMetadata extends ComponentMetadata
13
{
14
    /** @var string The name of the Entity */
15
    protected $entityName;
16
17
    /** @var string|null The name of the custom repository class used for the entity class. */
18
    protected $customRepositoryClassName;
19
20
    /** @var Property|null The field which is used for versioning in optimistic locking (if any). */
21
    protected $declaredVersion;
22
23
    /**
24
     * Whether this class describes the mapping of a read-only class.
25
     * That means it is never considered for change-tracking in the UnitOfWork.
26
     * It is a very helpful performance optimization for entities that are immutable,
27
     * either in your domain or through the relation database (coming from a view,
28
     * or a history table for example).
29
     *
30
     * @var bool
31
     */
32
    protected $readOnly = false;
33
34
    /**
35
     * List of all sub-classes (descendants) metadata.
36
     *
37
     * @var SubClassMetadata[]
38
     */
39
    protected $subClasses = [];
40
41
    /**
42
     * READ-ONLY: The registered lifecycle callbacks for entities of this class.
43
     *
44
     * @var string[][]
45
     */
46
    protected $lifecycleCallbacks = [];
47
48
    /**
49
     * READ-ONLY: The registered entity listeners.
50
     *
51
     * @var string[][]
52
     */
53
    protected $entityListeners = [];
54
55
    /**
56
     * READ-ONLY: The field names of all fields that are part of the identifier/primary key
57
     * of the mapped entity class.
58
     *
59
     * @var string[]
60
     */
61
    protected $identifier = [];
62
63
    /**
64
     * READ-ONLY: The primary table metadata.
65
     *
66
     * @var TableMetadata
67
     */
68
    protected $table;
69
70
    public function __construct(string $className, ClassMetadataBuildingContext $metadataBuildingContext)
71
    {
72
        parent::__construct($className, $metadataBuildingContext);
73
74
        $this->entityName = $className;
75
    }
76
77
    public function getEntityName() : string
78
    {
79
        return $this->entityName;
80
    }
81
82
    public function setEntityName(string $entityName) : void
83
    {
84
        $this->entityName = $entityName;
85
    }
86
87
    public function getCustomRepositoryClassName() : ?string
88
    {
89
        return $this->customRepositoryClassName;
90
    }
91
92
    public function setCustomRepositoryClassName(?string $customRepositoryClassName) : void
93
    {
94
        $this->customRepositoryClassName = $customRepositoryClassName;
95
    }
96
97
    public function getDeclaredVersion() : ?Property
98
    {
99
        return $this->declaredVersion;
100
    }
101
102
    public function setDeclaredVersion(Property $property) : void
103
    {
104
        $this->declaredVersion = $property;
105
    }
106
107
    public function getVersion() : ?Property
108
    {
109
        /** @var ComponentMetadata|null $parent */
110
        $parent  = $this->parent;
111
        $version = $this->declaredVersion;
112
113
        if ($parent && ! $version) {
114
            $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

114
            /** @scrutinizer ignore-call */ 
115
            $version = $parent->getVersion();
Loading history...
115
        }
116
117
        return $version;
118
    }
119
120
    public function isVersioned() : bool
121
    {
122
        return $this->getVersion() !== null;
123
    }
124
125
    public function setReadOnly(bool $readOnly) : void
126
    {
127
        $this->readOnly = $readOnly;
128
    }
129
130
    public function isReadOnly() : bool
131
    {
132
        return $this->readOnly;
133
    }
134
135
    /**
136
     * @throws MappingException
137
     */
138
    public function addSubClass(SubClassMetadata $subClassMetadata) : void
139
    {
140
        /** @var EntityClassMetadata|null $superClassMetadata */
141
        $superClassMetadata = $this->getParent();
142
143
        while ($superClassMetadata !== null) {
144
            if ($superClassMetadata->entityName === $subClassMetadata->entityName) {
145
                throw new MappingException(
146
                    sprintf(
147
                        'Circular inheritance mapping detected: "%s" have itself as superclass when extending "%s".',
148
                        $subClassMetadata->entityName,
149
                        $superClassMetadata->entityName
150
                    )
151
                );
152
            }
153
154
            $superClassMetadata->subClasses[] = $subClassMetadata;
155
156
            $superClassMetadata = $superClassMetadata->parent;
157
        }
158
159
        $this->subClasses[] = $subClassMetadata;
160
    }
161
162
    public function hasSubClasses() : bool
163
    {
164
        return (bool) $this->subClasses;
165
    }
166
167
    public function getSubClassIterator() : Iterator
168
    {
169
        $iterator = new AppendIterator();
170
171
        foreach ($this->subClasses as $subClassMetadata) {
172
            $iterator->append($subClassMetadata->getSubClassIterator());
173
        }
174
175
        $iterator->append(new ArrayIterator($this->subClasses));
176
177
        return $iterator;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function addProperty(Property $property) : void
184
    {
185
        parent::addProperty($property);
186
187
        if ($property->isVersioned()) {
0 ignored issues
show
Bug introduced by
The method isVersioned() does not exist on Doctrine\ORM\Mapping\Property. It seems like you code against a sub-type of Doctrine\ORM\Mapping\Property such as Doctrine\ORM\Mapping\FieldMetadata. ( Ignorable by Annotation )

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

187
        if ($property->/** @scrutinizer ignore-call */ isVersioned()) {
Loading history...
188
            $this->setDeclaredVersion($property);
189
        }
190
    }
191
192
    abstract public function getRootClass() : RootClassMetadata;
193
}
194