Failed Conditions
Push — master ( 148895...b07393 )
by Guilherme
09:59
created

ComponentMetadata::wakeupReflection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
crap 3.0261
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 Doctrine\ORM\Reflection\ReflectionService;
9
use ReflectionClass;
10
use ReflectionException;
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 $properties = [];
35
36 451
    public function __construct(string $className)
37
    {
38 451
        $this->className = $className;
39 451
    }
40
41 2016
    public function getClassName() : string
42
    {
43 2016
        return $this->className;
44
    }
45
46 98
    public function setParent(ComponentMetadata $parent) : void
47
    {
48 98
        $this->parent = $parent;
49 98
    }
50
51 1217
    public function getParent() : ?ComponentMetadata
52
    {
53 1217
        return $this->parent;
54
    }
55
56 1954
    public function wakeupReflection(ReflectionService $reflectionService) : void
57
    {
58
        // Restore ReflectionClass and properties
59 1954
        $this->reflectionClass = $reflectionService->getClass($this->className);
60
61 1954
        if (! $this->reflectionClass) {
62
            return;
63
        }
64
65 1954
        $this->className = $this->reflectionClass->getName();
66
67 1954
        foreach ($this->properties as $property) {
68
            /** @var Property $property */
69 1951
            $property->wakeupReflection($reflectionService);
70
        }
71 1954
    }
72
73 540
    public function getReflectionClass() : ?ReflectionClass
74
    {
75 540
        return $this->reflectionClass;
76
    }
77
78 25
    public function setCache(?CacheMetadata $cache = null) : void
79
    {
80 25
        $this->cache = $cache;
81 25
    }
82
83 181
    public function getCache() : ?CacheMetadata
84
    {
85 181
        return $this->cache;
86
    }
87
88
    /**
89
     * @return iterable|Property[]
90
     */
91 1485
    public function getPropertiesIterator() : iterable
92
    {
93 1485
        foreach ($this->properties as $name => $property) {
94 1484
            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...
95
        }
96 1484
    }
97
98
    /**
99
     * @throws ReflectionException
100
     * @throws MappingException
101
     */
102 416
    public function addProperty(Property $property) : void
103
    {
104 416
        $className    = $this->getClassName();
105 416
        $propertyName = $property->getName();
106
107 416
        if ($this->hasProperty($propertyName)) {
108 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

108
            throw MappingException::duplicateProperty($className, /** @scrutinizer ignore-type */ $this->getProperty($propertyName));
Loading history...
109
        }
110
111 416
        $property->setDeclaringClass($this);
112
113 416
        $this->properties[$propertyName] = $property;
114 416
    }
115
116 416
    public function hasProperty(string $propertyName) : bool
117
    {
118 416
        return isset($this->properties[$propertyName]);
119
    }
120
121 1808
    public function getProperty(string $propertyName) : ?Property
122
    {
123 1808
        if (isset($this->properties[$propertyName])) {
124 1801
            return $this->properties[$propertyName];
125
        }
126
127 522
        return null;
128
    }
129
130
    /**
131
     * @return ArrayIterator|ColumnMetadata[]
132
     */
133
    public function getColumnsIterator() : ArrayIterator
134
    {
135
        $iterator = new ArrayIterator();
136
137
        foreach ($this->getPropertiesIterator() as $property) {
138
            switch (true) {
139
                case $property instanceof FieldMetadata:
140
                    $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

140
                    $iterator->offsetSet($property->getColumnName(), /** @scrutinizer ignore-type */ $property);
Loading history...
141
                    break;
142
143
                case $property instanceof ToOneAssociationMetadata && $property->isOwningSide():
144
                    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

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

146
                        $iterator->offsetSet($joinColumn->getColumnName(), /** @scrutinizer ignore-type */ $joinColumn);
Loading history...
147
                    }
148
149
                    break;
150
            }
151
        }
152
153
        return $iterator;
154
    }
155
}
156