Test Failed
Push — master ( 614e1a...5c1cf2 )
by gt9000
13:11
created

Arr   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 8.11 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 6
loc 74
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C getValue() 6 33 12

How to fix   Duplicated Code   

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:

1
<?php
2
3
namespace zqhong\route\Helpers;
4
5
class Arr
6
{
7
    /**
8
     * Retrieves the value of an array element or object property with the given key or property name.
9
     * If the key does not exist in the array or object, the default value will be returned instead.
10
     *
11
     * The key may be specified in a dot format to retrieve the value of a sub-array or the property
12
     * of an embedded object. In particular, if the key is `x.y.z`, then the returned value would
13
     * be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']`
14
     * or `$array->x` is neither an array nor an object, the default value will be returned.
15
     * Note that if the array already has an element `x.y.z`, then its value will be returned
16
     * instead of going through the sub-arrays. So it is better to be done specifying an array of key names
17
     * like `['x', 'y', 'z']`.
18
     *
19
     * Below are some usage examples,
20
     *
21
     * ```php
22
     * // working with array
23
     * $username = \yii\helpers\ArrayHelper::getValue($_POST, 'username');
24
     * // working with object
25
     * $username = \yii\helpers\ArrayHelper::getValue($user, 'username');
26
     * // working with anonymous function
27
     * $fullName = \yii\helpers\ArrayHelper::getValue($user, function ($user, $defaultValue) {
28
     *     return $user->firstName . ' ' . $user->lastName;
29
     * });
30
     * // using dot format to retrieve the property of embedded object
31
     * $street = \yii\helpers\ArrayHelper::getValue($users, 'address.street');
32
     * // using an array of keys to retrieve the value
33
     * $value = \yii\helpers\ArrayHelper::getValue($versions, ['1.0', 'date']);
34
     * ```
35
     *
36
     * @param array|object $array array or object to extract value from
37
     * @param string|\Closure|array $key key name of the array element, an array of keys or property name of the object,
38
     * or an anonymous function returning the value. The anonymous function signature should be:
39
     * `function($array, $defaultValue)`.
40
     * The possibility to pass an array of keys is available since version 2.0.4.
41
     * @param mixed $default the default value to be returned if the specified array key does not exist. Not used when
42
     * getting value from an object.
43
     * @return mixed the value of the element if found, default value otherwise
44
     */
45
    public static function getValue($array, $key, $default = null)
46
    {
47
        if ($key instanceof \Closure) {
48
            return $key($array, $default);
49
        }
50
51
        if (is_array($key)) {
52
            $lastKey = array_pop($key);
53
            foreach ($key as $keyPart) {
54
                $array = static::getValue($array, $keyPart);
55
            }
56
            $key = $lastKey;
57
        }
58
59 View Code Duplication
        if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $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...
60
            return $array[$key];
61
        }
62
63
        if (($pos = strrpos($key, '.')) !== false) {
64
            $array = static::getValue($array, substr($key, 0, $pos), $default);
65
            $key = substr($key, $pos + 1);
66
        }
67
68
        if (is_object($array)) {
69
            // this is expected to fail if the property does not exist, or __get() is not implemented
70
            // it is not reliably possible to check whether a property is accessible beforehand
71
            return $array->$key;
72 View Code Duplication
        } elseif (is_array($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...
73
            return (isset($array[$key]) || array_key_exists($key, $array)) ? $array[$key] : $default;
74
        } else {
75
            return $default;
76
        }
77
    }
78
}
79