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

ReplaceFilter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 2
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A apply() 0 9 1
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