Failed Conditions
Pull Request — develop (#6935)
by Michael
167:08 queued 149:28
created

ComponentMetadata::fullyQualifiedClassName()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.0702

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 7
nop 1
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 6.0702
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
4
declare(strict_types=1);
5
6
namespace Doctrine\ORM\Mapping;
7
8
/**
9
 * A <tt>ComponentMetadata</tt> instance holds object-relational property mapping.
10
 *
11
 * @package Doctrine\ORM\Mapping
12
 * @since 3.0
13
 *
14
 * @author Guilherme Blanco <[email protected]>
15
 */
16
abstract class ComponentMetadata
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $className;
22
23
    /**
24
     * @var ComponentMetadata|null
25
     */
26
    protected $parent;
27
28
    /**
29
     * The ReflectionClass instance of the component class.
30
     *
31
     * @var \ReflectionClass|null
32
     */
33
    protected $reflectionClass;
34
35
    /**
36
     * @var CacheMetadata|null
37
     */
38
    protected $cache;
39
40
    /**
41
     * @var array<string, Property>
42
     */
43
    protected $declaredProperties = [];
44
45
    /**
46
     * ComponentMetadata constructor.
47
     *
48
     * @param string                       $className
49
     * @param ClassMetadataBuildingContext $metadataBuildingContext
50
     */
51 470
    public function __construct(string $className, ClassMetadataBuildingContext $metadataBuildingContext)
52
    {
53 470
        $reflectionService = $metadataBuildingContext->getReflectionService();
54
55 470
        $this->reflectionClass = $reflectionService->getClass($className);
56 470
        $this->className       = $this->reflectionClass ? $this->reflectionClass->getName() : $className;
57 470
    }
58
59
    /**
60
     * @return string
61
     */
62 2020
    public function getClassName() : string
63
    {
64 2020
        return $this->className;
65
    }
66
67
    /**
68
     * @param ComponentMetadata $parent
69
     */
70 101
    public function setParent(ComponentMetadata $parent) : void
71
    {
72 101
        $this->parent = $parent;
73 101
    }
74
75
    /**
76
     * @return ComponentMetadata|null
77
     */
78 1208
    public function getParent() : ?ComponentMetadata
79
    {
80 1208
        return $this->parent;
81
    }
82
83
    /**
84
     * @return \ReflectionClass|null
85
     */
86 561
    public function getReflectionClass() : ?\ReflectionClass
87
    {
88 561
        return $this->reflectionClass;
89
    }
90
91
    /**
92
     * @param CacheMetadata|null $cache
93
     *
94
     * @return void
95
     */
96 24
    public function setCache(?CacheMetadata $cache = null) : void
97
    {
98 24
        $this->cache = $cache;
99 24
    }
100
101
    /**
102
     * @return CacheMetadata|null
103
     */
104 235
    public function getCache(): ?CacheMetadata
105
    {
106 235
        return $this->cache;
107
    }
108
109
    /**
110
     * @return iterable
111
     */
112 1485
    public function getDeclaredPropertiesIterator() : iterable
113
    {
114 1485
        foreach ($this->declaredProperties as $name => $property) {
115 1481
            yield $name => $property;
116
        }
117 1484
    }
118
119
    /**
120
     * @param Property $property
121
     *
122
     * @throws \ReflectionException
123
     * @throws MappingException
124
     */
125 419
    public function addDeclaredProperty(Property $property) : void
126
    {
127 419
        $className    = $this->getClassName();
128 419
        $propertyName = $property->getName();
129
130
        // @todo guilhermeblanco Switch to hasProperty once inherited properties are not being mapped on child classes
131 419
        if ($this->hasDeclaredProperty($propertyName)) {
132 2
            throw MappingException::duplicateProperty($className, $this->getProperty($propertyName));
133
        }
134
135 419
        $property->setDeclaringClass($this);
136
137 419
        if ($this->reflectionClass) {
138 417
            $reflectionProperty = new \ReflectionProperty($className, $propertyName);
139
140 417
            $reflectionProperty->setAccessible(true);
141
142 417
            $property->setReflectionProperty($reflectionProperty);
143
        }
144
145 419
        $this->declaredProperties[$propertyName] = $property;
146 419
    }
147
148
    /**
149
     * @param string $propertyName
150
     *
151
     * @return bool
152
     */
153 419
    public function hasDeclaredProperty(string $propertyName) : bool
154
    {
155 419
        return isset($this->declaredProperties[$propertyName]);
156
    }
157
158
    /**
159
     * @return iterable
160
     */
161
    public function getPropertiesIterator() : iterable
162
    {
163
        if ($this->parent) {
164
            yield from $this->parent->getPropertiesIterator();
165
        }
166
167
        yield from $this->getDeclaredPropertiesIterator();
168
    }
169
170
    /**
171
     * @param string $propertyName
172
     *
173
     * @return null|Property
174
     */
175 1798
    public function getProperty(string $propertyName) : ?Property
176
    {
177 1798
        if (isset($this->declaredProperties[$propertyName])) {
178 1791
            return $this->declaredProperties[$propertyName];
179
        }
180
181 525
        if ($this->parent) {
182 82
            return $this->parent->getProperty($propertyName);
183
        }
184
185 525
        return null;
186
    }
187
188
    /**
189
     * @param string $propertyName
190
     *
191
     * @return bool
192
     */
193
    public function hasProperty(string $propertyName) : bool
194
    {
195
        if (isset($this->declaredProperties[$propertyName])) {
196
            return true;
197
        }
198
199
        return $this->parent && $this->parent->hasProperty($propertyName);
200
    }
201
202
    /**
203
     * @return \ArrayIterator
204
     */
205
    public function getColumnsIterator() : \ArrayIterator
206
    {
207
        $iterator = new \ArrayIterator();
208
209
        // @todo guilhermeblanco Must be switched to getPropertiesIterator once class only has its declared properties
210
        foreach ($this->getDeclaredPropertiesIterator() as $property) {
211
            switch (true) {
212
                case ($property instanceof FieldMetadata):
213
                    $iterator->offsetSet($property->getColumnName(), $property);
0 ignored issues
show
Bug introduced by
$property of type Doctrine\ORM\Mapping\FieldMetadata is incompatible with the type string expected by parameter $newval of ArrayIterator::offsetSet(). ( Ignorable by Annotation )

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

213
                    $iterator->offsetSet($property->getColumnName(), /** @scrutinizer ignore-type */ $property);
Loading history...
214
                    break;
215
216
                case ($property instanceof ToOneAssociationMetadata && $property->isOwningSide()):
217
                    foreach ($property->getJoinColumns() as $joinColumn) {
0 ignored issues
show
Bug introduced by
The method getJoinColumns() 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\ToOneAssociationMetadata. ( Ignorable by Annotation )

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

217
                    foreach ($property->/** @scrutinizer ignore-call */ getJoinColumns() as $joinColumn) {
Loading history...
218
                        /** @var JoinColumnMetadata $joinColumn */
219
                        $iterator->offsetSet($joinColumn->getColumnName(), $joinColumn);
0 ignored issues
show
Bug introduced by
$joinColumn of type Doctrine\ORM\Mapping\JoinColumnMetadata is incompatible with the type string expected by parameter $newval of ArrayIterator::offsetSet(). ( Ignorable by Annotation )

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

219
                        $iterator->offsetSet($joinColumn->getColumnName(), /** @scrutinizer ignore-type */ $joinColumn);
Loading history...
220
                    }
221
222
                    break;
223
            }
224
        }
225
226
        return $iterator;
227
    }
228
}
229