Completed
Push — master ( 7e12ab...eee1f0 )
by Eric
03:22
created

ReflectionMutator::setValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Mutator;
13
14
/**
15
 * @author GeLo <[email protected]>
16
 */
17
class ReflectionMutator implements MutatorInterface
18
{
19
    /**
20
     * @var \ReflectionProperty[]
21
     */
22
    private $properties = [];
23
24
    /**
25
     * @var \ReflectionMethod[]
26
     */
27
    private $methods = [];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 126
    public function setValue($object, $property, $value)
33
    {
34 126
        if (property_exists($object, $property)) {
35 117
            $this->getReflectionProperty($object, $property)->setValue($object, $value);
36 78
        } else {
37 9
            $this->setMethodValue($object, $property, $value);
38
        }
39 126
    }
40
41
    /**
42
     * @param object $object
43
     * @param string $property
44
     * @param mixed  $value
45
     */
46 9 View Code Duplication
    private function setMethodValue($object, $property, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48 9
        $methods = [$property];
49
50 9
        if (method_exists($object, $property)) {
51 9
            $this->getReflectionMethod($object, $property)->invoke($object, $value);
52
53 9
            return;
54
        }
55
56
        $methodSuffix = ucfirst($property);
57
58
        foreach (['get', 'has', 'is'] as $methodPrefix) {
59
            $methods[] = $method = $methodPrefix.$methodSuffix;
60
61
            if (method_exists($object, $method)) {
62
                $this->getReflectionMethod($object, $method)->invoke($object, $value);
63
64
                return;
65
            }
66
        }
67
68
        throw new \InvalidArgumentException(sprintf(
69
            'The property "%s" or methods %s don\'t exist on class "%s".',
70
            $property,
71
            '"'.implode('", "', $methods).'"',
72
            get_class($object)
73
        ));
74
    }
75
76
    /**
77
     * @param object $object
78
     * @param string $property
79
     *
80
     * @return \ReflectionProperty
81
     */
82 117 View Code Duplication
    private function getReflectionProperty($object, $property)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84 117
        if (isset($this->properties[$key = $this->getCacheKey($object, $property)])) {
85 27
            return $this->properties[$key];
86
        }
87
88 117
        $reflection = new \ReflectionProperty($object, $property);
89 117
        $reflection->setAccessible(true);
90
91 117
        return $this->properties[$key] = $reflection;
92
    }
93
94
    /**
95
     * @param object $object
96
     * @param string $method
97
     *
98
     * @return \ReflectionMethod
99
     */
100 9 View Code Duplication
    private function getReflectionMethod($object, $method)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102 9
        if (isset($this->methods[$key = $this->getCacheKey($object, $method)])) {
103
            return $this->methods[$key];
104
        }
105
106 9
        $reflection = new \ReflectionMethod($object, $method);
107 9
        $reflection->setAccessible(true);
108
109 9
        return $this->methods[$key] = $reflection;
110
    }
111
112
    /**
113
     * @param object $object
114
     * @param string $key
115
     *
116
     * @return string
117
     */
118 126
    private function getCacheKey($object, $key)
119
    {
120 126
        return get_class($object).'::'.$key;
121
    }
122
}
123