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

ComponentMetadata::getDeclaredPropertiesIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
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 $properties = [];
35
36 469
    public function __construct(string $className, ClassMetadataBuildingContext $metadataBuildingContext)
37
    {
38 469
        $reflectionService = $metadataBuildingContext->getReflectionService();
39
40 469
        $this->reflectionClass = $reflectionService->getClass($className);
41 469
        $this->className       = $this->reflectionClass ? $this->reflectionClass->getName() : $className;
42 469
    }
43
44 2029
    public function getClassName() : string
45
    {
46 2029
        return $this->className;
47
    }
48
49 98
    public function setParent(ComponentMetadata $parent) : void
50
    {
51 98
        $this->parent = $parent;
52 98
    }
53
54 1217
    public function getParent() : ?ComponentMetadata
55
    {
56 1217
        return $this->parent;
57
    }
58
59 543
    public function getReflectionClass() : ?ReflectionClass
60
    {
61 543
        return $this->reflectionClass;
62
    }
63
64 25
    public function setCache(?CacheMetadata $cache = null) : void
65
    {
66 25
        $this->cache = $cache;
67 25
    }
68
69 140
    public function getCache() : ?CacheMetadata
70
    {
71 140
        return $this->cache;
72
    }
73
74
    /**
75
     * @return iterable|Property[]
76
     */
77 1487
    public function getPropertiesIterator() : iterable
78
    {
79 1487
        foreach ($this->properties as $name => $property) {
80 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...
81
        }
82 1486
    }
83
84
    /**
85
     * @throws ReflectionException
86
     * @throws MappingException
87
     */
88 428
    public function addProperty(Property $property) : void
89
    {
90 428
        $className    = $this->getClassName();
91 428
        $propertyName = $property->getName();
92
93 428
        if ($this->hasProperty($propertyName)) {
94 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

94
            throw MappingException::duplicateProperty($className, /** @scrutinizer ignore-type */ $this->getProperty($propertyName));
Loading history...
95
        }
96
97 428
        $property->setDeclaringClass($this);
98
99 428
        if ($this->reflectionClass) {
100 426
            $reflectionProperty = new ReflectionProperty($className, $propertyName);
101
102 426
            $reflectionProperty->setAccessible(true);
103
104 426
            $property->setReflectionProperty($reflectionProperty);
105
        }
106
107 428
        $this->properties[$propertyName] = $property;
108 428
    }
109
110 428
    public function hasProperty(string $propertyName) : bool
111
    {
112 428
        return isset($this->properties[$propertyName]);
113
    }
114
115 1818
    public function getProperty(string $propertyName) : ?Property
116
    {
117 1818
        if (isset($this->properties[$propertyName])) {
118 1811
            return $this->properties[$propertyName];
119
        }
120
121 522
        return null;
122
    }
123
124
    /**
125
     * @return ArrayIterator|ColumnMetadata[]
126
     */
127
    public function getColumnsIterator() : ArrayIterator
128
    {
129
        $iterator = new ArrayIterator();
130
131
        foreach ($this->getPropertiesIterator() as $property) {
132
            switch (true) {
133
                case $property instanceof FieldMetadata:
134
                    $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

134
                    $iterator->offsetSet($property->getColumnName(), /** @scrutinizer ignore-type */ $property);
Loading history...
135
                    break;
136
137
                case $property instanceof ToOneAssociationMetadata && $property->isOwningSide():
138
                    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

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

140
                        $iterator->offsetSet($joinColumn->getColumnName(), /** @scrutinizer ignore-type */ $joinColumn);
Loading history...
141
                    }
142
143
                    break;
144
            }
145
        }
146
147
        return $iterator;
148
    }
149
}
150