Accessor   B
last analyzed

Complexity

Total Complexity 51

Size/Duplication

Total Lines 167
Duplicated Lines 3.59 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 1 Features 2
Metric Value
wmc 51
c 9
b 1
f 2
lcom 1
cbo 0
dl 6
loc 167
ccs 83
cts 83
cp 1
rs 8.3206

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getData() 0 4 1
A getCurrent() 0 4 1
C to() 3 30 12
B set() 0 20 6
D has() 0 16 18
B remove() 3 21 8
A isObjectWithMethod() 0 4 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Accessor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Accessor, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Cocur\Vale;
4
5
/**
6
 * Accessor.
7
 *
8
 * @author    Florian Eckerstorfer <[email protected]>
9
 * @copyright 2015 Florian Eckerstorfer
10
 */
11
class Accessor
12
{
13
    /**
14
     * @var mixed
15
     */
16
    protected $data;
17
18
    /**
19
     * @var mixed
20
     */
21
    protected $current;
22
23
    /**
24
     * @param mixed $data
25
     */
26 3
    public function __construct($data)
27
    {
28 3
        if (is_array($data)) {
29 2
            $this->data    = &$data;
30 2
            $this->current = &$data;
31 2
        } else {
32 1
            $this->data    = $data;
33 1
            $this->current = $data;
34
        }
35 3
    }
36
37
    /**
38
     * @return mixed
39
     */
40 2
    public function getData()
41
    {
42 2
        return $this->data;
43
    }
44
45
    /**
46
     * @return mixed
47
     */
48 1
    public function getCurrent()
49
    {
50 1
        return $this->current;
51
    }
52
53
    /**
54
     * Navigates to `$key`.
55
     *
56
     * @param string|int $key
57
     *
58
     * @return bool `true` if it is possible to navigate to `$key`, `false` if not
59
     */
60 21
    public function to($key)
0 ignored issues
show
Coding Style introduced by
function to() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
61
    {
62 21
        $getter = 'get'.ucfirst($key);
63 21
        $hasser = 'has'.ucfirst($key);
64 21
        $isser  = 'is'.ucfirst($key);
65
66 21
        if (is_array($this->current) && array_key_exists($key, $this->current)) {
67 2
            $this->current = &$this->current[$key];
68 21
        } elseif ($this->isObjectWithMethod($this->current, $key)) {
69 2
            $this->current = $this->current->$key();
70 19
        } elseif ($this->isObjectWithMethod($this->current, $getter)) {
71 2
            $this->current = $this->current->$getter();
72 17
        } elseif ($this->isObjectWithMethod($this->current, 'get')) {
73 1
            $this->current = $this->current->get($key);
74 15
        } elseif ($this->isObjectWithMethod($this->current, $hasser)) {
75 2
            $this->current = $this->current->$hasser();
76 14
        } elseif ($this->isObjectWithMethod($this->current, 'has')) {
77 1
            $this->current = $this->current->has($key);
78 12
        } elseif ($this->isObjectWithMethod($this->current, $isser)) {
79 2
            $this->current = $this->current->$isser();
80 11
        } elseif ($this->isObjectWithMethod($this->current, 'is')) {
81 1
            $this->current = $this->current->is($key);
82 9 View Code Duplication
        } elseif (is_object($this->current) && isset($this->current->$key)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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 2
            $this->current = $this->current->$key;
84 2
        } else {
85 6
            return false;
86
        }
87
88 15
        return true;
89
    }
90
91
    /**
92
     * @param string|int $key
93
     * @param mixed      $value
94
     *
95
     * @return bool
96
     */
97 14
    public function set($key, $value)
0 ignored issues
show
Coding Style introduced by
function set() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
98
    {
99 14
        $setter = 'set'.ucfirst($key);
100
101 14
        if (is_array($this->current)) {
102 4
            $this->current[$key] = $value;
103 14
        } elseif ($this->isObjectWithMethod($this->current, $key)) {
104 2
            $this->current->$key($value);
105 10
        } elseif ($this->isObjectWithMethod($this->current, $setter)) {
106 2
            $this->current->$setter($value);
107 8
        } elseif ($this->isObjectWithMethod($this->current, 'set')) {
108 1
            $this->current->set($key, $value);
109 6
        } elseif (is_object($this->current)) {
110 4
            $this->current->$key = $value;
111 4
        } else {
112 1
            return false;
113
        }
114
115 13
        return true;
116
    }
117
118
    /**
119
     * @param string|int $key
120
     *
121
     * @return bool
122
     */
123 23
    public function has($key)
124
    {
125 23
        $getter = 'get'.ucfirst($key);
126 23
        $hasser = 'has'.ucfirst($key);
127 23
        $isser  = 'is'.ucfirst($key);
128
129 23
        return (is_array($this->current) && isset($this->current[$key]))
130 22
            || is_object($this->current) && isset($this->current->$key)
131 22
            || ($this->isObjectWithMethod($this->current, $hasser) && $this->current->$hasser())
132 21
            || ($this->isObjectWithMethod($this->current, 'has') && $this->current->has($key))
133 20
            || ($this->isObjectWithMethod($this->current, $isser) && $this->current->$isser())
134 19
            || ($this->isObjectWithMethod($this->current, 'is') && $this->current->is($key))
135 18
            || ($this->isObjectWithMethod($this->current, $key) && $this->current->$key())
136 17
            || ($this->isObjectWithMethod($this->current, $getter) && $this->current->$getter())
137 23
            || ($this->isObjectWithMethod($this->current, 'get') && $this->current->get($key));
138
    }
139
140
    /**
141
     * @param string|int $key
142
     *
143
     * @return bool
144
     */
145 7
    public function remove($key)
0 ignored issues
show
Coding Style introduced by
function remove() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
146
    {
147 7
        $unsetter = 'unset'.ucfirst($key);
148 7
        $remover  = 'remove'.ucfirst($key);
149
150 7
        if (is_array($this->current) && array_key_exists($key, $this->current)) {
151 1
            unset($this->current[$key]);
152 7 View Code Duplication
        } elseif (is_object($this->current) && isset($this->current->$key)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
153 1
            unset($this->current->$key);
154 6
        } elseif ($this->isObjectWithMethod($this->current, $unsetter)) {
155 1
            $this->current->$unsetter();
156 5
        } elseif ($this->isObjectWithMethod($this->current, $remover)) {
157 1
            $this->current->$remover();
158 4
        } elseif ($this->isObjectWithMethod($this->current, 'remove')) {
159 1
            $this->current->remove($key);
160 1
        } else {
161 2
            return false;
162
        }
163
164 5
        return true;
165
    }
166
167
    /**
168
     * @param mixed      $data
169
     * @param string|int $key
170
     *
171
     * @return bool
172
     */
173 18
    protected function isObjectWithMethod($data, $key)
174
    {
175 18
        return is_object($data) && method_exists($data, $key) && is_callable([$data, $key]);
176
    }
177
}
178