Passed
Pull Request — master (#5)
by Jonas
01:18
created

Arr::has()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 6
Ratio 31.58 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
dl 6
loc 19
ccs 10
cts 11
cp 0.9091
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 5
nop 2
crap 5.0187
1
<?php
2
3
namespace ArrayHelpers;
4
5
class Arr
6
{
7
    /**
8
     * Get an item from an array.
9
     * Supports dot notation:
10
     * e.g `Arr::get($array, 'section.subsection.item')`
11
     *
12
     * @param array $array
13
     * @param string $key
14
     * @param mixed|null $default
15
     * @return mixed|null
16
     */
17 4
    public static function get(array $array, $key, $default = null)
18
    {
19 4
        if (array_key_exists($key, $array)) {
20 2
            return $array[$key];
21
        }
22
23 3
        if (strpos($key, '.') === false) {
24 2
            return $default;
25
        }
26
27 1
        $keys = explode('.', $key);
28 1 View Code Duplication
        while (count($keys) > 0) {
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...
29 1
            $subSet = $array[array_shift($keys)];
30 1
            if (is_array($subSet)) {
31 1
                return static::get($subSet, join('.', $keys), $default);
32
            }
33
        }
34
35
        return $default;
36
    }
37
38
    /**
39
     * Checks if an item is available in an array.
40
     * Supports dot notation:
41
     * e.g `Arr::has($array, 'section.subsection.item')`
42
     *
43
     * @param array $array
44
     * @param string $key
45
     * @return bool
46
     */
47 3
    public static function has(array $array, $key)
48
    {
49 3
        if (array_key_exists($key, $array)) {
50 2
            return true;
51
        }
52
53 2
        if (strpos($key, '.') === false) {
54 1
            return false;
55
        }
56
57 1
        $keys = explode('.', $key);
58 1 View Code Duplication
        while (count($keys) > 0) {
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...
59 1
            $subSet = $array[array_shift($keys)];
60 1
            if (is_array($subSet)) {
61 1
                return static::has($subSet, join('.', $keys));
62
            }
63
        }
64
65
        return false;
66
    }
67
}
68