Accessor::has()   D
last analyzed

Complexity

Conditions 18
Paths 767

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 18

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 16
ccs 13
cts 13
cp 1
rs 4.9231
cc 18
eloc 13
nc 767
nop 1
crap 18

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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