ReflectionPropertyAccess   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 42
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 5 1
A get() 0 6 1
A getReflectionProperty() 0 9 1
1
<?php
2
3
namespace Happyr\SerializerBundle\PropertyManager;
4
5
/**
6
 * An easy way to get properties from an object.
7
 *
8
 * @author Tobias Nyholm <[email protected]>
9
 */
10
class ReflectionPropertyAccess
11
{
12
    /**
13
     * @param $object
14
     * @param $propertyName
15
     * @param $value
16
     */
17 11
    public static function set($object, $propertyName, $value)
18
    {
19 11
        $reflectionProperty = self::getReflectionProperty($object, $propertyName);
20 11
        $reflectionProperty->setValue($object, $value);
21 11
    }
22
23
    /**
24
     * @param $object
25
     * @param $propertyName
26
     *
27
     * @return mixed
28
     */
29 23
    public static function get($object, $propertyName)
30
    {
31 23
        $reflectionProperty = self::getReflectionProperty($object, $propertyName);
32
33 23
        return $reflectionProperty->getValue($object);
34
    }
35
36
    /**
37
     * @param $object
38
     * @param $propertyName
39
     *
40
     * @return \ReflectionProperty
41
     */
42 23
    private static function getReflectionProperty($object, $propertyName)
43
    {
44 23
        $reflectionClass = new \ReflectionClass($object);
45
46 23
        $reflectionProperty = $reflectionClass->getProperty($propertyName);
47 23
        $reflectionProperty->setAccessible(true);
48
49 23
        return $reflectionProperty;
50
    }
51
}
52