Completed
Push — master ( fb964a...b7229e )
by Jonathan
9s
created

RuntimePublicReflectionProperty   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 12 3
A getValue() 0 14 4
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
 */
12
class RuntimePublicReflectionProperty extends ReflectionProperty
13
{
14
    /**
15
     * {@inheritDoc}
16
     *
17
     * Checks is the value actually exist before fetching it.
18
     * This is to avoid calling `__get` on the provided $object if it
19
     * is a {@see \Doctrine\Common\Proxy\Proxy}.
20
     */
21 3
    public function getValue($object = null)
22
    {
23 3
        $name = $this->getName();
24
25 3
        if ($object instanceof Proxy && ! $object->__isInitialized()) {
26 1
            $originalInitializer = $object->__getInitializer();
27 1
            $object->__setInitializer(null);
28 1
            $val = $object->$name ?? null;
29 1
            $object->__setInitializer($originalInitializer);
30
31 1
            return $val;
32
        }
33
34 2
        return isset($object->$name) ? parent::getValue($object) : null;
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     *
40
     * Avoids triggering lazy loading via `__set` if the provided object
41
     * is a {@see \Doctrine\Common\Proxy\Proxy}.
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