Failed Conditions
Push — master ( 2ccf23...d791f7 )
by Michael
10:40
created

ComponentMetadata::addDeclaredProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

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

167
                    $iterator->offsetSet($property->getColumnName(), /** @scrutinizer ignore-type */ $property);
Loading history...
168
                    break;
169
170
                case ($property instanceof ToOneAssociationMetadata && $property->isOwningSide()):
171
                    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

171
                    foreach ($property->/** @scrutinizer ignore-call */ getJoinColumns() as $joinColumn) {
Loading history...
172
                        /** @var JoinColumnMetadata $joinColumn */
173
                        $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

173
                        $iterator->offsetSet($joinColumn->getColumnName(), /** @scrutinizer ignore-type */ $joinColumn);
Loading history...
174
                    }
175
176
                    break;
177
            }
178
        }
179
180
        return $iterator;
181
    }
182
}
183