ReflectionMutator::getCacheKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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 328
    public function setValue($object, $property, $value)
33
    {
34 328
        if (property_exists($object, $property)) {
35 312
            $this->getReflectionProperty($object, $property)->setValue($object, $value);
36 156
        } else {
37 16
            $this->setMethodValue($object, $property, $value);
38
        }
39 328
    }
40
41
    /**
42
     * @param object $object
43
     * @param string $property
44
     * @param mixed  $value
45
     */
46 16 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 16
        $methods = [$property];
49
50 16
        if (method_exists($object, $property)) {
51 16
            $this->getReflectionMethod($object, $property)->invoke($object, $value);
52
53 16
            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 312 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 312
        if (isset($this->properties[$key = $this->getCacheKey($object, $property)])) {
85 44
            return $this->properties[$key];
86
        }
87
88 312
        $reflection = new \ReflectionProperty($object, $property);
89 312
        $reflection->setAccessible(true);
90
91 312
        return $this->properties[$key] = $reflection;
92
    }
93
94
    /**
95
     * @param object $object
96
     * @param string $method
97
     *
98
     * @return \ReflectionMethod
99
     */
100 16 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 16
        if (isset($this->methods[$key = $this->getCacheKey($object, $method)])) {
103
            return $this->methods[$key];
104
        }
105
106 16
        $reflection = new \ReflectionMethod($object, $method);
107 16
        $reflection->setAccessible(true);
108
109 16
        return $this->methods[$key] = $reflection;
110
    }
111
112
    /**
113
     * @param object $object
114
     * @param string $key
115
     *
116
     * @return string
117
     */
118 328
    private function getCacheKey($object, $key)
119
    {
120 328
        return get_class($object).'::'.$key;
121
    }
122
}
123