PropertyReference::setValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Cascade\Mapper\Field\Reference;
4
5
class PropertyReference implements ReferenceInterface
6
{
7
    /**
8
     * @var string
9
     */
10
    private $property;
11
12
    /**
13
     * @param string $property
14
     */
15 6
    public function __construct($property)
16
    {
17 6
        $this->property = $property;
18 6
    }
19
20 2
    public function getValue($instance)
21
    {
22 2
        $property = $this->property;
23 2
        $className = get_class($instance);
24
25 2
        if ($className === 'stdClass') {
26 1
            return $instance->$property;
27
        }
28
29 1
        return (new \ReflectionProperty($className, $property))->getValue($instance);
30
    }
31
32 2
    public function setValue($instance, $value)
33
    {
34 2
        $property = $this->property;
35 2
        $className = get_class($instance);
36
37 2
        if ($className === 'stdClass') {
38 1
            $instance->$property = $value;
39 1
        } else {
40 1
            (new \ReflectionProperty($className, $property))->setValue($instance, $value);
41
        }
42
43 2
        return $instance;
44
    }
45
}
46