Failed Conditions
Pull Request — master (#7898)
by Guilherme
63:09
created

ComponentMetadata::getColumnsIterator()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 5
nop 0
dl 0
loc 21
ccs 0
cts 11
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Mapping;
6
7
use Doctrine\ORM\Reflection\ReflectionService;
8
use ReflectionClass;
9
use ReflectionException;
10
11
/**
12
 * A <tt>ComponentMetadata</tt> instance holds object-relational property mapping.
13
 */
14
abstract class ComponentMetadata
15
{
16
    /** @var string */
17
    protected $className;
18
19
    /** @var ComponentMetadata|null */
20
    protected $parent;
21
22
    /**
23
     * The ReflectionClass instance of the component class.
24
     *
25
     * @var ReflectionClass|null
26
     */
27
    protected $reflectionClass;
28
29
    /** @var CacheMetadata|null */
30
    protected $cache;
31
32
    /** @var Property[] */
33
    protected $properties = [];
34
35
    public function __construct(string $className)
36 449
    {
37
        $this->className = $className;
38 449
    }
39 449
40
    public function getClassName() : string
41 2016
    {
42
        return $this->className;
43 2016
    }
44
45
    public function setParent(ComponentMetadata $parent) : void
46 98
    {
47
        $this->parent = $parent;
48 98
    }
49 98
50
    public function getParent() : ?ComponentMetadata
51 1217
    {
52
        return $this->parent;
53 1217
    }
54
55
    public function wakeupReflection(ReflectionService $reflectionService) : void
56 1954
    {
57
        // Restore ReflectionClass and properties
58
        $this->reflectionClass = $reflectionService->getClass($this->className);
59 1954
60
        if (! $this->reflectionClass) {
61 1954
            return;
62
        }
63
64
        $this->className = $this->reflectionClass->getName();
65 1954
66
        foreach ($this->properties as $property) {
67 1954
            /** @var Property $property */
68
            $property->wakeupReflection($reflectionService);
69 1951
        }
70
    }
71 1954
72
    public function getReflectionClass() : ?ReflectionClass
73 540
    {
74
        return $this->reflectionClass;
75 540
    }
76
77
    public function setCache(?CacheMetadata $cache = null) : void
78 25
    {
79
        $this->cache = $cache;
80 25
    }
81 25
82
    public function getCache() : ?CacheMetadata
83 181
    {
84
        return $this->cache;
85 181
    }
86
87
    /**
88
     * @return iterable|Property[]
89
     */
90
    public function getPropertiesIterator() : iterable
91 1485
    {
92
        foreach ($this->properties as $name => $property) {
93 1485
            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...
94 1484
        }
95
    }
96 1484
97
    /**
98
     * @throws ReflectionException
99
     * @throws MappingException
100
     */
101
    public function addProperty(Property $property) : void
102 416
    {
103
        $className    = $this->getClassName();
104 416
        $propertyName = $property->getName();
105 416
106
        if ($this->hasProperty($propertyName)) {
107 416
            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

107
            throw MappingException::duplicateProperty($className, /** @scrutinizer ignore-type */ $this->getProperty($propertyName));
Loading history...
108 2
        }
109
110
        $property->setDeclaringClass($this);
111 416
112
        $this->properties[$propertyName] = $property;
113 416
    }
114 416
115
    public function hasProperty(string $propertyName) : bool
116 416
    {
117
        return isset($this->properties[$propertyName]);
118 416
    }
119
120
    public function getProperty(string $propertyName) : ?Property
121 1808
    {
122
        return $this->properties[$propertyName] ?? null;
123 1808
    }
124
}
125