Completed
Push — master ( 9eda17...c0c747 )
by Adrian Florin
04:54
created

ArrayHelper::hasKeysInDepth()   D

Complexity

Conditions 10
Paths 6

Size

Total Lines 41
Code Lines 28

Duplication

Lines 16
Ratio 39.02 %

Code Coverage

Tests 22
CRAP Score 10.0578

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 41
ccs 22
cts 24
cp 0.9167
rs 4.8196
cc 10
eloc 28
nc 6
nop 2
crap 10.0578

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
 * This file is part of the NeedleProject\Common package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace NeedleProject\Common\Helper;
9
10
use NeedleProject\Common\Exception\NotFoundException;
11
12
/**
13
 * Class ErrorToExceptionConverter
14
 *
15
 * @package NeedleProject\Common\Helper
16
 * @author Adrian Tilita <[email protected]>
17
 * @copyright 2017 Adrian Tilita
18
 * @license https://opensource.org/licenses/MIT MIT Licence
19
 */
20
class ArrayHelper
21
{
22
    /**
23
     * Verify if a key exist in depth
24
     *
25
     * @param array $array  - The array in which to search
26
     * @param array $keys - an array with the linear items treated as depth.
27
     *                      Ex: array('first_level','second_level','third_level')
28
     * @todo    Refactor in a "non-breaking" mather - to many breaks
29
     *          Improve design
30
     * @return bool
31
     */
32 10
    public function hasKeysInDepth($array, $keys)
33
    {
34 10 View Code Duplication
        if (!is_array($array) || empty($array)) {
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...
35 6
            throw new \InvalidArgumentException(
36
                sprintf(
37 6
                    "Cannot search keys in depth, expected a non-empty haystack array, got %s or empty array.",
38
                    gettype($array)
39
                )
40
            );
41
        }
42 4 View Code Duplication
        if (!is_array($keys) || empty($keys)) {
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...
43 1
            throw new \InvalidArgumentException(
44
                sprintf(
45 1
                    "Cannot search keys in depth, expected a non-empty keys array, got %s or empty array.",
46
                    gettype($array)
47
                )
48
            );
49
        }
50
51 3
        $result = false;
52 3
        $haystack = $array;
53 3
        $depth = count($keys);
54 3
        for ($i = 0; $i < $depth; $i++) {
55 3
            $isLast = ($i + 1) === $depth;
56 3
            $needle = $keys[$i];
57 3
            if ($isLast && array_key_exists($needle, $haystack)) {
58 2
                $result = true;
59 2
                break;
60
            }
61 3
            if (!isset($haystack[$needle])) {
62 1
                $result = false;
63 1
                break;
64
            }
65 3
            $haystack = $haystack[$needle];
66 3
            if (!is_array($haystack)) {
67
                $result = false;
68
                break;
69
            }
70
        }
71 3
        return $result;
72
    }
73
74
    /**
75
     * Get a value in depth of an array
76
     * @param $array - The array in which to search
77
     * @param $keys - an array with the linear items treated as depth.
78
     *                 Ex: array('first_level','second_level','third_level')
79
     * @return string
80
     * @todo    Refactor in a "non-breaking" mather - to many breaks
81
     *          Improve design
82
     * @throws \Exception
83
     */
84 10
    public function getValueFromDepth($array, $keys)
85
    {
86 10 View Code Duplication
        if (!is_array($array) || empty($array)) {
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...
87 6
            throw new \InvalidArgumentException(
88
                sprintf(
89 6
                    "Cannot get value in depth, expected a non-empty haystack array, got %s or empty array.",
90
                    gettype($array)
91
                )
92
            );
93
        }
94 4 View Code Duplication
        if (!is_array($keys) || empty($keys)) {
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...
95 1
            throw new \InvalidArgumentException(
96
                sprintf(
97 1
                    "Cannot get value in depth, expected a non-empty keys array, got %s or empty array.",
98
                    gettype($array)
99
                )
100
            );
101
        }
102 3
        $result = '';
103 3
        $haystack = $array;
104 3
        $depth = count($keys);
105 3
        for ($i = 0; $i < $depth; $i++) {
106 3
            $isLast = ($i + 1) === $depth;
107 3
            $needle = $keys[$i];
108 3
            if ($isLast && array_key_exists($needle, $haystack)) {
109 2
                $result = $haystack[$needle];
110 2
                break;
111
            }
112 3
            if (!isset($haystack[$needle]) || !is_array($haystack[$needle])) {
113 1
                throw new NotFoundException(
114
                    sprintf(
115 1
                        "Could not find requested value in %s for given path %s",
116
                        json_encode($array),
117 1
                        implode(' -> ', $keys)
118
                    )
119
                );
120
            }
121 2
            $haystack = $haystack[$needle];
122
        }
123 2
        return $result;
124
    }
125
}
126