ResponseArrayTrait   B
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 300
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 38
lcom 1
cbo 0
dl 0
loc 300
ccs 100
cts 100
cp 1
rs 8.3999
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 14 3
A exists() 0 8 1
A has() 0 8 1
A first() 0 10 2
A last() 0 10 2
A contains() 0 4 1
A key() 0 7 1
A keys() 0 4 1
A values() 0 4 1
B random() 0 23 5
A count() 0 18 4
A flatten() 0 13 2
A sum() 0 4 1
A __invoke() 0 4 1
B constructKey() 0 12 5
C parseKey() 0 32 7
1
<?php
2
3
namespace Bouhnosaure\Dogecoin;
4
5
trait ResponseArrayTrait
6
{
7
    /**
8
     * Gets data by using key with dotted notation.
9
     *
10
     * @param string|null $key
11
     *
12
     * @return mixed
13
     */
14 36
    public function get($key = null)
15
    {
16 36
        $key = $this->constructKey($key);
17
18 36
        if (is_null($key)) {
19 21
            return $this->result();
0 ignored issues
show
Bug introduced by
It seems like result() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
20
        }
21
22 16
        return $this->parseKey($key, function ($part, $result) {
23 24
            if (isset($result[$part])) {
24 24
                return $result[$part];
25
            }
26 24
        });
27
    }
28
29
    /**
30
     * Checks if key exists.
31
     *
32
     * @param string|null $key
33
     *
34
     * @return bool
35
     */
36 6
    public function exists($key = null)
37
    {
38 6
        $key = $this->constructKey($key);
39
40 4
        return $this->parseKey($key, function ($part, $result) {
41 6
            return array_key_exists($part, $result);
42 6
        });
43
    }
44
45
    /**
46
     * Checks if key exists and not null.
47
     *
48
     * @param string|null $key
49
     *
50
     * @return bool
51
     */
52 3
    public function has($key = null)
53
    {
54 3
        $key = $this->constructKey($key);
55
56 3
        return $this->parseKey($key, function ($part, $result) {
57 3
            return isset($result[$part]);
58 3
        });
59
    }
60
61
    /**
62
     * Gets first element.
63
     *
64
     * @return mixed
65
     */
66 3
    public function first()
67
    {
68 3
        $value = $this->get();
69
70 3
        if (is_array($value)) {
71 3
            return reset($value);
72
        }
73
74 3
        return $value;
75
    }
76
77
    /**
78
     * Gets last element.
79
     *
80
     * @return mixed
81
     */
82 3
    public function last()
83
    {
84 3
        $value = $this->get();
85
86 3
        if (is_array($value)) {
87 3
            return end($value);
88
        }
89
90 3
        return $value;
91
    }
92
93
    /**
94
     * Checks if response contains value.
95
     *
96
     * @param mixed $value
97
     *
98
     * @return bool
99
     */
100 3
    public function contains($value)
101
    {
102 3
        return in_array($value, $this->result());
0 ignored issues
show
Bug introduced by
It seems like result() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
103
    }
104
105
    /**
106
     * Set current key.
107
     *
108
     * @param string|null $key
109
     *
110
     * @return static
111
     */
112 24
    public function key($key = null)
113
    {
114 24
        $new = clone $this;
115 24
        $new->current = $key;
0 ignored issues
show
Bug introduced by
The property current does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
116
117 24
        return $new;
118
    }
119
120
    /**
121
     * Gets response keys.
122
     *
123
     * @return array
124
     */
125 3
    public function keys()
126
    {
127 3
        return array_keys($this->result());
0 ignored issues
show
Bug introduced by
It seems like result() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
128
    }
129
130
    /**
131
     * Gets response values.
132
     *
133
     * @return array
134
     */
135 3
    public function values()
136
    {
137 3
        return array_values($this->result());
0 ignored issues
show
Bug introduced by
It seems like result() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
138
    }
139
140
    /**
141
     * Gets random value.
142
     *
143
     * @param int         $number
144
     * @param string|null $key
145
     *
146
     * @return mixed
147
     */
148 3
    public function random($number = 1, $key = null)
149
    {
150 3
        $value = $this->get($key);
151
152 3
        if (is_array($value)) {
153 3
            $keys = array_keys($value);
154 3
            $keysLength = count($keys);
155
156 3
            shuffle($keys);
157
158 3
            if ($number > $keysLength) {
159 3
                $number = $keysLength;
160 1
            }
161
162 3
            for ($result = [], $count = 0; $count < $number; $count++) {
163 3
                $result[$keys[$count]] = $value[$keys[$count]];
164 1
            }
165
166 3
            return count($result) > 1 ? $result : current($result);
167
        }
168
169 3
        return $value;
170
    }
171
172
    /**
173
     * Counts response items.
174
     *
175
     * @param string|null $key
176
     *
177
     * @return int
178
     */
179 3
    public function count($key = null)
180
    {
181 3
        if (is_null($this->constructKey($key))) {
182 3
            return count($this->result());
0 ignored issues
show
Bug introduced by
It seems like result() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
183
        }
184
185 3
        if (!$this->exists($key)) {
186 3
            return 0;
187
        }
188
189 3
        $value = $this->get($key);
190
191 3
        if (is_array($value)) {
192 3
            return count($value);
193
        }
194
195 3
        return 1;
196
    }
197
198
    /**
199
     * Flattens multi-dimensional array.
200
     *
201
     * @param string|null $key
202
     *
203
     * @return array
204
     */
205 6
    public function flatten($key = null)
206
    {
207 6
        $array = new \RecursiveIteratorIterator(
208 6
            new \RecursiveArrayIterator((array) $this->get($key))
209 2
        );
210
211 6
        $tmp = [];
212 6
        foreach ($array as $value) {
213 6
            $tmp[] = $value;
214 2
        }
215
216 6
        return $tmp;
217
    }
218
219
    /**
220
     * Gets sum of values.
221
     *
222
     * @param string|null $key
223
     *
224
     * @return float
225
     */
226 3
    public function sum($key = null)
227
    {
228 3
        return array_sum($this->flatten($key));
229
    }
230
231
    /**
232
     * Get response item by key.
233
     *
234
     * @param string|null $key
235
     *
236
     * @return mixed
237
     */
238 15
    public function __invoke($key = null)
239
    {
240 15
        return $this->key($key);
241
    }
242
243
    /**
244
     * Constructs full key.
245
     *
246
     * @param string|null $key
247
     *
248
     * @return string|null
249
     */
250 42
    protected function constructKey($key = null)
251
    {
252 42
        if (!is_null($key) && !is_null($this->current)) {
253 9
            return $this->current.'.'.$key;
254
        }
255
256 42
        if (is_null($key) && !is_null($this->current)) {
257 21
            return $this->current;
258
        }
259
260 39
        return $key;
261
    }
262
263
    /**
264
     * Parses dotted notation.
265
     *
266
     * @param string     $key
267
     * @param callable   $callback
268
     * @param array|null $result
269
     *
270
     * @return mixed
271
     */
272 30
    protected function parseKey($key, callable $callback, $result = null)
273
    {
274 30
        $parts = explode('.', trim($key, '.'));
275 30
        $result = $result ?: $this->result();
0 ignored issues
show
Bug introduced by
It seems like result() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
276
277 30
        foreach ($parts as $index => $part) {
278 30
            if ($part == '*') {
279 6
                $sub = [];
280
281 6
                foreach (array_keys($result) as $subKey) {
282 6
                    $path = $subKey;
283
284 6
                    if (isset($parts[$index + 1])) {
285 6
                        $pathParts = array_slice($parts, $index + 1);
286 6
                        $path .= '.'.implode('.', $pathParts);
287 2
                    }
288
289 6
                    $sub[$subKey] = $this->parseKey($path, $callback, $result);
290 2
                }
291
292 6
                return $sub;
293
            }
294
295 30
            if (!$return = $callback($part, $result)) {
296 9
                return $return;
297
            }
298
299 30
            $result = $result[$part];
300 10
        }
301
302 30
        return $return;
0 ignored issues
show
Bug introduced by
The variable $return does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
303
    }
304
}
305