Failed Conditions
Pull Request — master (#1)
by Jonathan
13:22 queued 10:46
created

RuntimePublicReflectionProperty::setValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 2
crap 12
1
<?php
2
namespace Doctrine\Common\Reflection;
3
4
use Doctrine\Common\Proxy\Proxy;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Proxy\Proxy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use ReflectionProperty;
6
7
/**
8
 * PHP Runtime Reflection Public Property - special overrides for public properties.
9
 *
10
 * @author Marco Pivetta <[email protected]>
11
 * @since  2.4
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 = isset($object->$name) ? $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