Failed Conditions
Push — master ( ec2bd1...5afe97 )
by Jonathan
41s
created

RuntimePublicReflectionProperty::getValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
ccs 0
cts 9
cp 0
cc 4
eloc 8
nc 3
nop 1
crap 20
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
    public function getValue($object = null)
21
    {
22
        $name = $this->getName();
23
24
        if ($object instanceof Proxy && ! $object->__isInitialized()) {
25
            $originalInitializer = $object->__getInitializer();
26
            $object->__setInitializer(null);
27
            $val = $object->$name ?? null;
28
            $object->__setInitializer($originalInitializer);
29
30
            return $val;
31
        }
32
33
        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
     * @link https://bugs.php.net/bug.php?id=63463
42
     */
43
    public function setValue($object, $value = null)
44
    {
45
        if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
46
            parent::setValue($object, $value);
47
48
            return;
49
        }
50
51
        $originalInitializer = $object->__getInitializer();
52
        $object->__setInitializer(null);
53
        parent::setValue($object, $value);
54
        $object->__setInitializer($originalInitializer);
55
    }
56
}
57