Completed
Push — master ( 938a56...8c0811 )
by Jonathan
10s
created

RuntimePublicReflectionProperty::getValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 1
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
ccs 8
cts 9
cp 0.8889
crap 4.0218
1
<?php
2
namespace Doctrine\Common\Reflection;
3
4
use Doctrine\Common\Proxy\Proxy;
5
use ReflectionProperty;
6
7
/**
8
 * PHP Runtime Reflection Public Property - special overrides for public properties.
9
 *
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 1
    public function getValue($object = null)
21
    {
22 1
        $name = $this->getName();
23
24 1
        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
        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 1
    public function setValue($object, $value = null)
44
    {
45 1
        if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
46 1
            parent::setValue($object, $value);
47
48 1
            return;
49
        }
50
51 1
        $originalInitializer = $object->__getInitializer();
52 1
        $object->__setInitializer(null);
53 1
        parent::setValue($object, $value);
54 1
        $object->__setInitializer($originalInitializer);
55 1
    }
56
}
57