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

RuntimePublicReflectionProperty   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
ccs 16
cts 17
cp 0.9412
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 12 3
A getValue() 0 14 4
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