Failed Conditions
Pull Request — master (#7506)
by
unknown
09:28
created

ComponentMetadata::getCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 ArrayIterator;
8
use ReflectionClass;
9
use ReflectionException;
10
use ReflectionProperty;
11
12
/**
13
 * A <tt>ComponentMetadata</tt> instance holds object-relational property mapping.
14
 */
15
abstract class ComponentMetadata
16
{
17
    /** @var string */
18
    protected $className;
19
20
    /** @var ComponentMetadata|null */
21
    protected $parent;
22
23
    /**
24
     * The ReflectionClass instance of the component class.
25
     *
26
     * @var ReflectionClass|null
27
     */
28
    protected $reflectionClass;
29
30
    /** @var CacheMetadata|null */
31
    protected $cache;
32
33
    /** @var Property[] */
34
    protected $declaredProperties = [];
35
36 462
    public function __construct(string $className, ClassMetadataBuildingContext $metadataBuildingContext)
37
    {
38 462
        $reflectionService = $metadataBuildingContext->getReflectionService();
39
40 462
        $this->reflectionClass = $reflectionService->getClass($className);
41 462
        $this->className       = $this->reflectionClass ? $this->reflectionClass->getName() : $className;
42 462
    }
43
44 2021
    public function getClassName() : string
45
    {
46 2021
        return $this->className;
47
    }
48
49 97
    public function setParent(ComponentMetadata $parent) : void
50
    {
51 97
        $this->parent = $parent;
52 97
    }
53
54 1213
    public function getParent() : ?ComponentMetadata
55
    {
56 1213
        return $this->parent;
57
    }
58
59 563
    public function getReflectionClass() : ?ReflectionClass
60
    {
61 563
        return $this->reflectionClass;
62
    }
63
64 25
    public function setCache(?CacheMetadata $cache = null) : void
65
    {
66 25
        $this->cache = $cache;
67 25
    }
68
69 233
    public function getCache() : ?CacheMetadata
70
    {
71 233
        return $this->cache;
72
    }
73
74
    /**
75
     * @return iterable|Property[]
76
     */
77 1480
    public function getDeclaredPropertiesIterator() : iterable
78
    {
79 1480
        foreach ($this->declaredProperties as $name => $property) {
80 1478
            yield $name => $property;
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $name => $property returns the type Generator which is incompatible with the documented return type Doctrine\ORM\Mapping\Property[]|iterable.
Loading history...
81
        }
82 1479
    }
83
84
    /**
85
     * @throws ReflectionException
86
     * @throws MappingException
87
     */
88 424
    public function addDeclaredProperty(Property $property) : void
89
    {
90 424
        $className    = $this->getClassName();
91 424
        $propertyName = $property->getName();
92
93
        // @todo guilhermeblanco Switch to hasProperty once inherited properties are not being mapped on child classes
94 424
        if ($this->hasDeclaredProperty($propertyName)) {
95 2
            throw MappingException::duplicateProperty($className, $this->getProperty($propertyName));
0 ignored issues
show
Bug introduced by
It seems like $this->getProperty($propertyName) can also be of type null; however, parameter $property of Doctrine\ORM\Mapping\Map...on::duplicateProperty() does only seem to accept Doctrine\ORM\Mapping\Property, maybe add an additional type check? ( Ignorable by Annotation )

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

95
            throw MappingException::duplicateProperty($className, /** @scrutinizer ignore-type */ $this->getProperty($propertyName));
Loading history...
96
        }
97
98 424
        $property->setDeclaringClass($this);
99
100 424
        if ($this->reflectionClass) {
101 422
            $reflectionProperty = new ReflectionProperty($className, $propertyName);
102
103 422
            $reflectionProperty->setAccessible(true);
104
105 422
            $property->setReflectionProperty($reflectionProperty);
106
        }
107
108 424
        $this->declaredProperties[$propertyName] = $property;
109 424
    }
110
111 424
    public function hasDeclaredProperty(string $propertyName) : bool
112
    {
113 424
        return isset($this->declaredProperties[$propertyName]);
114
    }
115
116
    /**
117
     * @return iterable|Property[]
118
     */
119
    public function getPropertiesIterator() : iterable
120
    {
121
        if ($this->parent) {
122
            yield from $this->parent->getPropertiesIterator();
0 ignored issues
show
Bug Best Practice introduced by
The expression YieldFromNode returns the type Generator which is incompatible with the documented return type Doctrine\ORM\Mapping\Property[]|iterable.
Loading history...
123
        }
124
125
        yield from $this->getDeclaredPropertiesIterator();
126
    }
127
128 1808
    public function getProperty(string $propertyName) : ?Property
129
    {
130 1808
        if (isset($this->declaredProperties[$propertyName])) {
131 1801
            return $this->declaredProperties[$propertyName];
132
        }
133
134 520
        if ($this->parent) {
135 81
            return $this->parent->getProperty($propertyName);
136
        }
137
138 520
        return null;
139
    }
140
141
    public function hasProperty(string $propertyName) : bool
142
    {
143
        if (isset($this->declaredProperties[$propertyName])) {
144
            return true;
145
        }
146
147
        return $this->parent && $this->parent->hasProperty($propertyName);
148
    }
149
150
    /**
151
     * @return ArrayIterator|ColumnMetadata[]
152
     */
153 4
    public function getColumnsIterator() : ArrayIterator
154
    {
155 4
        $iterator = new ArrayIterator();
156
157
        // @todo guilhermeblanco Must be switched to getPropertiesIterator once class only has its declared properties
158 4
        foreach ($this->getDeclaredPropertiesIterator() as $property) {
159
            switch (true) {
160 4
                case $property instanceof FieldMetadata:
161 4
                    $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

161
                    $iterator->offsetSet($property->getColumnName(), /** @scrutinizer ignore-type */ $property);
Loading history...
162 4
                    break;
163
164 2
                case $property instanceof ToOneAssociationMetadata && $property->isOwningSide():
165 2
                    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

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

167
                        $iterator->offsetSet($joinColumn->getColumnName(), /** @scrutinizer ignore-type */ $joinColumn);
Loading history...
168
                    }
169
170 4
                    break;
171
            }
172
        }
173
174 4
        return $iterator;
175
    }
176
}
177