Passed
Pull Request — 2.6 (#8015)
by
unknown
08:26
created

ReflectionEmbeddedProperty::setValue()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.0488

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 14
ccs 7
cts 8
cp 0.875
rs 9.6111
c 1
b 0
f 0
cc 5
nc 4
nop 2
crap 5.0488
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Mapping;
21
22
use Doctrine\Instantiator\Instantiator;
23
use ReflectionProperty;
24
25
/**
26
 * Acts as a proxy to a nested Property structure, making it look like
27
 * just a single scalar property.
28
 *
29
 * This way value objects "just work" without UnitOfWork, Persisters or Hydrators
30
 * needing any changes.
31
 *
32
 * TODO: Move this class into Common\Reflection
33
 */
34
class ReflectionEmbeddedProperty extends ReflectionProperty
35
{
36
    /**
37
     * @var ReflectionProperty reflection property of the class where the embedded object has to be put
38
     */
39
    private $parentProperty;
40
41
    /**
42
     * @var ReflectionProperty reflection property of the embedded object
43
     */
44
    private $childProperty;
45
46
    /**
47
     * @var string name of the embedded class to be eventually instantiated
48
     */
49
    private $embeddedClass;
50
51
    /**
52
     * @var Instantiator|null
53
     */
54
    private $instantiator;
55
56
    /**
57
     * @param ReflectionProperty $parentProperty
58
     * @param ReflectionProperty $childProperty
59
     * @param string             $embeddedClass
60
     */
61 22
    public function __construct(ReflectionProperty $parentProperty, ReflectionProperty $childProperty, $embeddedClass)
62
    {
63 22
        $this->parentProperty  = $parentProperty;
64 22
        $this->childProperty   = $childProperty;
65 22
        $this->embeddedClass   = (string) $embeddedClass;
66
67 22
        parent::__construct($childProperty->getDeclaringClass()->getName(), $childProperty->getName());
68 22
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 9
    public function getValue($object = null)
74
    {
75 9
        $embeddedObject = $this->parentProperty->getValue($object);
76
77 9
        if (null === $embeddedObject) {
78 7
            return null;
79
        }
80
81 3
        return $this->childProperty->getValue($embeddedObject);
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87 2
    public function setValue($object, $value = null)
88
    {
89 2
        $embeddedObject = $this->parentProperty->getValue($object);
90
91 2
        if (null === $embeddedObject) {
92 1
            $this->instantiator = $this->instantiator ?: new Instantiator();
93
94 1
            $embeddedObject = $this->instantiator->instantiate($this->embeddedClass);
95
96 1
            $this->parentProperty->setValue($object, $embeddedObject);
97
        }
98
99 2
        if (true === $this->childProperty->getType()->allowsNull() || null !== $value) {
0 ignored issues
show
Bug introduced by
The method getType() does not exist on ReflectionProperty. ( Ignorable by Annotation )

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

99
        if (true === $this->childProperty->/** @scrutinizer ignore-call */ getType()->allowsNull() || null !== $value) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
            $this->childProperty->setValue($embeddedObject, $value);
101
        }
102
    }
103
}
104