Completed
Pull Request — master (#1)
by Karsten
03:23
created

ReflectionPropertyAccess::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by gerk on 13.11.17 05:50
4
 */
5
6
namespace PeekAndPoke\Component\PropertyAccess;
7
8
/**
9
 * This implementation accesses properties through reflection.
10
 *
11
 * This is a medium speed accessor which less limitation. It will also be the most used one since most
12
 * class properties are protected or private.
13
 *
14
 *
15
 * What can it do?
16
 *
17
 * - access public properties (you should use PublicPropertyAccess instead for performance)
18
 *
19
 * - access protected properties that are visible on the subject class
20
 *
21
 * - access private properties declared on the subject class
22
 *
23
 * What can it NOT do?
24
 *
25
 * - access private properties that are declared on a base class of the subject class
26
 *  -> use ScopedPropertyAccess then which is the slowest accessor
27
 *
28
 *
29
 * @author Karsten J. Gerber <[email protected]>
30
 */
31
class ReflectionPropertyAccess implements PropertyAccess
32
{
33
    /** @var string */
34
    private $className;
35
    /** @var string */
36
    private $propertyName;
37
38
    /** @var \ReflectionProperty */
39
    private $_prop;
40
41
    /**
42
     * @param \ReflectionClass $class
43
     * @param string           $propertyName The name of the property
44
     *
45
     * @return ReflectionPropertyAccess
46
     */
47 9
    public static function create(\ReflectionClass $class, $propertyName)
48
    {
49 9
        $ret               = new self;
50 9
        $ret->className    = $class->getName();
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
51 9
        $ret->propertyName = $propertyName;
52
53 9
        $ret->_prop = $class->getProperty($propertyName);
54 9
        $ret->_prop->setAccessible(true);
55
56 9
        return $ret;
57
    }
58
59
    /**
60
     * Make use of the static create method
61
     */
62
    private function __construct() { }
63
64
    /**
65
     * @return array
66
     */
67
    public function __sleep()
68
    {
69
        return ['className', 'propertyName'];
70
    }
71
72
    /**
73
     *
74
     */
75
    public function __wakeup()
76
    {
77
        $class = new \ReflectionClass($this->className);
78
79
        $this->_prop = $class->getProperty($this->propertyName);
80
        $this->_prop->setAccessible(true);
81
    }
82
83
    /**
84
     * Get the value of the property
85
     *
86
     * @param mixed $subject The object to get the value from
87
     *
88
     * @return mixed
89
     */
90 25
    public function get($subject)
91
    {
92 25
        return $this->_prop->getValue($subject);
93
    }
94
95
    /**
96
     * Set the value of the property
97
     *
98
     * @param mixed $subject The object to set the value to
99
     * @param mixed $value
100
     */
101 20
    public function set($subject, $value)
102
    {
103 20
        $this->_prop->setValue($subject, $value);
104 20
    }
105
}
106