Failed Conditions
Pull Request — master (#2)
by Jonathan
03:29
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
declare(strict_types=1);
4
5
namespace Doctrine\Common\Reflection;
6
7
use Doctrine\Common\Proxy\Proxy;
8
use ReflectionProperty;
9
10
/**
11
 * PHP Runtime Reflection Public Property - special overrides for public properties.
12
 */
13
class RuntimePublicReflectionProperty extends ReflectionProperty
14
{
15
    /**
16
     * {@inheritDoc}
17
     *
18
     * Checks is the value actually exist before fetching it.
19
     * This is to avoid calling `__get` on the provided $object if it
20
     * is a {@see \Doctrine\Common\Proxy\Proxy}.
21
     */
22
    public function getValue($object = null)
23
    {
24
        $name = $this->getName();
25
26
        if ($object instanceof Proxy && ! $object->__isInitialized()) {
27
            $originalInitializer = $object->__getInitializer();
28
            $object->__setInitializer(null);
29
            $val = $object->$name ?? null;
30
            $object->__setInitializer($originalInitializer);
31
32
            return $val;
33
        }
34
35
        return isset($object->$name) ? parent::getValue($object) : null;
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     *
41
     * Avoids triggering lazy loading via `__set` if the provided object
42
     * is a {@see \Doctrine\Common\Proxy\Proxy}.
43
     * @link https://bugs.php.net/bug.php?id=63463
44
     */
45
    public function setValue($object, $value = null)
46
    {
47
        if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
48
            parent::setValue($object, $value);
49
50
            return;
51
        }
52
53
        $originalInitializer = $object->__getInitializer();
54
        $object->__setInitializer(null);
55
        parent::setValue($object, $value);
56
        $object->__setInitializer($originalInitializer);
57
    }
58
}
59