Completed
Push — master ( 1c49db...1a6bbd )
by Julien
02:00
created

ArrayHelper::arraySame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Mapado\RestClientSdk\Helper;
4
5
/**
6
 * Some array helpers.
7
 * Greatly inspired by Laravel's helper: https://github.com/rappasoft/laravel-helpers
8
 *
9
 * @author Julien Deniau <[email protected]>
10
 */
11
class ArrayHelper
12
{
13
    /**
14
     * Get an item from an array using "dot" notation.
15
     *
16
     * @param  array   $array
17
     * @param  string  $key
18
     * @param  mixed   $default
19
     * @return mixed
20
     */
21
    public static function arrayGet($array, $key, $default = null)
22
    {
23
        if (is_null($key)) {
24
            return $array;
25
        }
26
27
        if (isset($array[$key])) {
28
            return $array[$key];
29
        }
30
31 View Code Duplication
        foreach (explode('.', $key) as $segment) {
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...
32
            if (!is_array($array) || !array_key_exists($segment, $array)) {
33
                return self::value($default);
34
            }
35
            $array = $array[$segment];
36
        }
37
        return $array;
38
    }
39
40
    /**
41
     * Check if an item exists in an array using "dot" notation.
42
     *
43
     * @param  array   $array
44
     * @param  string  $key
45
     * @return bool
46
     */
47
    public static function arrayHas($array, $key)
48
    {
49
        if (empty($array) || is_null($key)) {
50
            return false;
51
        }
52
53
        if (array_key_exists($key, $array)) {
54
            return true;
55
        }
56
57 View Code Duplication
        foreach (explode('.', $key) as $segment) {
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...
58
            if (!is_array($array) || !array_key_exists($segment, $array)) {
59
                return false;
60
            }
61
62
            $array = $array[$segment];
63
        }
64
65
        return true;
66
    }
67
68
    /**
69
     * Flatten a multi-dimensional associative array with dots.
70
     *
71
     * @param  array   $array
72
     * @param  string  $prepend
73
     * @return array
74
     */
75
    public static function arrayDot($array, $prepend = '')
76
    {
77
        $results = [];
78
        foreach ($array as $key => $value) {
79
            if (is_array($value) && !empty($value)) {
80
                $results = array_merge($results, static::arrayDot($value, $prepend . $key . '.'));
81
            } else {
82
                $results[$prepend . $key] = $value;
83
            }
84
        }
85
86
        return $results;
87
    }
88
89
    public static function arrayDiffAssocRecursive($array1, $array2)
90
    {
91
        return array_diff_assoc(static::arrayDot($array1), static::arrayDot($array2));
92
    }
93
94
    public static function arraySame($array1, $array2)
95
    {
96
        return empty(static::arrayDiffAssocRecursive($array1, $array2));
97
    }
98
99
100
    /**
101
     * Return the default value of the given value.
102
     *
103
     * @param  mixed  $value
104
     * @return mixed
105
     */
106
    public static function value($value)
107
    {
108
        return $value instanceof Closure ? $value() : $value;
0 ignored issues
show
Bug introduced by
The class Mapado\RestClientSdk\Helper\Closure does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
109
    }
110
}
111