RuntimePublicReflectionProperty::setValue()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 12
rs 10
ccs 8
cts 8
cp 1
crap 3
1
<?php
2
3
namespace Doctrine\Common\Reflection;
4
5
use Doctrine\Common\Proxy\Proxy;
6
use ReflectionProperty;
7
8
/**
9
 * PHP Runtime Reflection Public Property - special overrides for public properties.
10
 */
11
class RuntimePublicReflectionProperty extends ReflectionProperty
12
{
13
    /**
14
     * {@inheritDoc}
15
     *
16
     * Checks is the value actually exist before fetching it.
17
     * This is to avoid calling `__get` on the provided $object if it
18
     * is a {@see \Doctrine\Common\Proxy\Proxy}.
19
     */
20 3
    public function getValue($object = null)
21
    {
22 3
        $name = $this->getName();
23
24 3
        if ($object instanceof Proxy && ! $object->__isInitialized()) {
25 1
            $originalInitializer = $object->__getInitializer();
26 1
            $object->__setInitializer(null);
27 1
            $val = $object->$name ?? null;
28 1
            $object->__setInitializer($originalInitializer);
29
30 1
            return $val;
31
        }
32
33 2
        return isset($object->$name) ? parent::getValue($object) : null;
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     *
39
     * Avoids triggering lazy loading via `__set` if the provided object
40
     * is a {@see \Doctrine\Common\Proxy\Proxy}.
41
     *
42
     * @link https://bugs.php.net/bug.php?id=63463
43
     */
44 2
    public function setValue($object, $value = null)
45
    {
46 2
        if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
47 2
            parent::setValue($object, $value);
48
49 2
            return;
50
        }
51
52 1
        $originalInitializer = $object->__getInitializer();
53 1
        $object->__setInitializer(null);
54 1
        parent::setValue($object, $value);
55 1
        $object->__setInitializer($originalInitializer);
56 1
    }
57
}
58