ReflectionAccessor   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 45.63 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 61.76%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 47
loc 103
ccs 21
cts 34
cp 0.6176
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 8 2
B getMethodValue() 25 25 4
A getReflectionProperty() 11 11 2
A getReflectionMethod() 11 11 2
A getCacheKey() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 672
    public function getValue($object, $property)
33
    {
34 672
        if (property_exists($object, $property)) {
35 656
            return $this->getReflectionProperty($object, $property)->getValue($object);
36
        }
37
38 16
        return $this->getMethodValue($object, $property);
39
    }
40
41
    /**
42
     * @param object $object
43
     * @param string $property
44
     *
45
     * @return mixed
46
     */
47 16 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...
48
    {
49 16
        $methods = [$property];
50
51 16
        if (method_exists($object, $property)) {
52 16
            return $this->getReflectionMethod($object, $property)->invoke($object);
53
        }
54
55
        $methodSuffix = ucfirst($property);
56
57
        foreach (['get', 'has', 'is'] as $methodPrefix) {
58
            $methods[] = $method = $methodPrefix.$methodSuffix;
59
60
            if (method_exists($object, $method)) {
61
                return $this->getReflectionMethod($object, $method)->invoke($object);
62
            }
63
        }
64
65
        throw new \InvalidArgumentException(sprintf(
66
            'The property "%s" or methods %s don\'t exist on class "%s".',
67
            $property,
68
            '"'.implode('", "', $methods).'"',
69
            get_class($object)
70
        ));
71
    }
72
73
    /**
74
     * @param object $object
75
     * @param string $property
76
     *
77
     * @return \ReflectionProperty
78
     */
79 656 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...
80
    {
81 656
        if (isset($this->properties[$key = $this->getCacheKey($object, $property)])) {
82 64
            return $this->properties[$key];
83
        }
84
85 656
        $reflection = new \ReflectionProperty($object, $property);
86 656
        $reflection->setAccessible(true);
87
88 656
        return $this->properties[$key] = $reflection;
89
    }
90
91
    /**
92
     * @param object $object
93
     * @param string $method
94
     *
95
     * @return \ReflectionMethod
96
     */
97 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...
98
    {
99 16
        if (isset($this->methods[$key = $this->getCacheKey($object, $method)])) {
100
            return $this->methods[$key];
101
        }
102
103 16
        $reflection = new \ReflectionMethod($object, $method);
104 16
        $reflection->setAccessible(true);
105
106 16
        return $this->methods[$key] = $reflection;
107
    }
108
109
    /**
110
     * @param object $object
111
     * @param string $key
112
     *
113
     * @return string
114
     */
115 672
    private function getCacheKey($object, $key)
116
    {
117 672
        return get_class($object).'::'.$key;
118
    }
119
}
120