Completed
Push — master ( a8631d...f95348 )
by Eric
02:27
created

ReflectionMutator::getReflectionProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 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
    public function setValue($object, $property, $value)
33
    {
34
        try {
35
            return $this->setPropertyValue($object, $property, $value);
36
        } catch (\ReflectionException $e) {
37
            $this->setMethodValue($object, $property, $value);
38
        }
39
    }
40
41
    /**
42
     * @param object $object
43
     * @param string $property
44
     * @param mixed  $value
45
     */
46
    private function setPropertyValue($object, $property, $value)
47
    {
48
        $this->getReflectionProperty($object, $property)->setValue($object, $value);
49
    }
50
51
    /**
52
     * @param object $object
53
     * @param string $property
54
     * @param mixed  $value
55
     */
56 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...
57
    {
58
        $methods = [];
59
        $methodSuffix = ucfirst($property);
60
61
        foreach (['get', 'has', 'is'] as $methodPrefix) {
62
            try {
63
                $methods[] = $method = $methodPrefix.$methodSuffix;
64
                $this->getReflectionMethod($object, $method)->invoke($object, $value);
65
66
                return;
67
            } catch (\ReflectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
68
            }
69
        }
70
71
        throw new \InvalidArgumentException(sprintf(
72
            'The property "%s" or methods %s don\'t exist.',
73
            $property,
74
            '"'.implode('", "', $methods).'"'
75
        ));
76
    }
77
78
    /**
79
     * @param object $object
80
     * @param string $property
81
     *
82
     * @return \ReflectionProperty
83
     */
84 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...
85
    {
86
        if (isset($this->properties[$key = $this->getCacheKey($object, $property)])) {
87
            return $this->properties[$key];
88
        }
89
90
        $reflection = new \ReflectionProperty($object, $property);
91
        $reflection->setAccessible(true);
92
93
        return $this->properties[$key] = $reflection;
94
    }
95
96
    /**
97
     * @param object $object
98
     * @param string $method
99
     *
100
     * @return \ReflectionMethod
101
     */
102 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...
103
    {
104
        if (isset($this->methods[$key = $this->getCacheKey($object, $method)])) {
105
            return $this->methods[$key];
106
        }
107
108
        $reflection = new \ReflectionMethod($object, $method);
109
        $reflection->setAccessible(true);
110
111
        return $this->methods[$key] = $reflection;
112
    }
113
114
    /**
115
     * @param object $object
116
     * @param string $key
117
     *
118
     * @return string
119
     */
120
    private function getCacheKey($object, $key)
121
    {
122
        return get_class($object).'::'.$key;
123
    }
124
}
125