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

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