Test Failed
Push — master ( 72a6c5...9329b7 )
by Arun
03:52
created

ReplaceFilter::apply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace DeepCopy\Filter;
4
5
/**
6
 * Replace the value of a property
7
 */
8
class ReplaceFilter implements Filter
9
{
10
    /**
11
     * @var callable
12
     */
13
    protected $callback;
14
15
    /**
16
     * @param callable $callable Will be called to get the new value for each property to replace
17
     */
18
    public function __construct(callable $callable)
19
    {
20
        $this->callback = $callable;
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function apply($object, $property, $objectCopier)
27
    {
28
        $reflectionProperty = new \ReflectionProperty($object, $property);
29
        $reflectionProperty->setAccessible(true);
30
31
        $value = call_user_func($this->callback, $reflectionProperty->getValue($object));
32
33
        $reflectionProperty->setValue($object, $value);
34
    }
35
}
36