GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ArrayHelper::extract()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 12
rs 8.8571
cc 5
eloc 7
nc 2
nop 2
1
<?php
2
/**
3
 * Array helper
4
 *
5
 * @author Kachit
6
 * @package Kachit\Helper
7
 */
8
namespace Kachit\Helper;
9
10
class ArrayHelper
11
{
12
    const INSERT_TYPE_AFTER = 'after';
13
    const INSERT_TYPE_BEFORE = 'before';
14
15
    /**
16
     * @var array
17
     */
18
    protected $insertTypes = [
19
        self::INSERT_TYPE_AFTER,
20
        self::INSERT_TYPE_BEFORE,
21
    ];
22
23
    /**
24
     * Fetch single dimensional array from multi dimensional array (array_column analog)
25
     *
26
     * @param array $array
27
     * @param string $valueParam
28
     * @param string $keyParam
29
     * @return array
30
     */
31
    public function flatten(array $array, $valueParam, $keyParam = null)
32
    {
33
        if (PHP_VERSION_ID >= 50500) {
34
            return array_column($array, $valueParam, $keyParam);
35
        }
36
        $data = array();
37
        if(!empty($array)) {
38
            foreach($array as $key => $value) {
39
                if(empty($keyParam)) {
40
                    $data[$key] = $value[$valueParam];
41
                } else {
42
                    $data[$value[$keyParam]] = $value[$valueParam];
43
                }
44
            }
45
        }
46
        return $data;
47
    }
48
49
    /**
50
     * Check is assoc array
51
     *
52
     * @param array $array
53
     * @return bool
54
     */
55
    public function isAssoc(array &$array)
56
    {
57
        $keys = array_keys($array);
58
        return array_keys($keys) !== $keys;
59
    }
60
61
    /**
62
     * Check is array
63
     *
64
     * @param mixed $value value to check
65
     * @return boolean
66
     */
67
    public function isArray($value)
68
    {
69
        if (is_array($value)) {
70
            return true;
71
        }
72
        return (is_object($value) && $value instanceof \ArrayAccess);
73
    }
74
75
    /**
76
     * Check for key exists
77
     *
78
     * @param array $array
79
     * @param mixed $key
80
     * @return bool
81
     */
82
    public function has(array $array, $key)
83
    {
84
        return array_key_exists($key, $array);
85
    }
86
87
    /**
88
     * Delete element from array
89
     *
90
     * @param array $array
91
     * @param $key
92
     * @return bool
93
     */
94
    public function delete(array &$array, $key)
95
    {
96
        $result = false;
97
        if ($this->has($array, $key)) {
98
            unset($array[$key]);
99
            $result = true;
100
        }
101
        return $result;
102
    }
103
104
    /**
105
     * Extract values list from array
106
     *
107
     * @param array $array
108
     * @param array $keys
109
     * @return array
110
     */
111
    public function extract(array $array, array $keys)
112
    {
113
        $found = [];
114
        if($array && $keys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $keys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
115
            foreach ($keys as $key) {
116
                if ($this->has($array, $key)) {
117
                    $found[$key] = $array[$key];
118
                }
119
            }
120
        }
121
        return $found;
122
    }
123
124
    /**
125
     * Extract from array with exclude
126
     *
127
     * @param array $array
128
     * @param array $keys
129
     * @return array
130
     */
131
    public function exclude(array $array, array $keys)
132
    {
133
        if($array && $keys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $keys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
134
            foreach ($keys as $key) {
135
                $this->delete($array, $key);
136
            }
137
        }
138
        return $array;
139
    }
140
141
    /**
142
     * Check is multi dimensional array
143
     *
144
     * @param array $array
145
     * @return bool
146
     */
147
    public function isMultiDimensional(array &$array)
148
    {
149
        return (count($array) !== count($array, true));
150
    }
151
152
    /**
153
     * Get element from array
154
     *
155
     * @param array $array
156
     * @param string $key
157
     * @param string $default
158
     * @return mixed
159
     */
160
    public function getElement(array &$array, $key, $default = null)
161
    {
162
        return (array_key_exists($key, $array)) ? $array[$key] : $default;
163
    }
164
165
    /**
166
     * Clear array by filter
167
     *
168
     * @param array $array
169
     * @param array $filter
170
     * @return array
171
     */
172
    public function clear(array $array, array $filter = ['', null])
173
    {
174
        return array_diff($array, $filter);
175
    }
176
177
    /**
178
     * Insert after
179
     *
180
     * @param array $array
181
     * @param array $insert
182
     * @param mixed $key
183
     * @param string $position
184
     * @return array
185
     */
186
    public function insert(array $array, $key, array $insert, $position = self::INSERT_TYPE_AFTER)
187
    {
188
        if (!in_array($position, $this->insertTypes)) {
189
            return false;
190
        }
191
        $keyPosition = array_search($key, array_keys($array));
192
        if (self::INSERT_TYPE_AFTER == $position) {
193
            $keyPosition++;
194
        }
195
196
        if (false !== $keyPosition ) {
197
            $result = array_slice($array, 0, $keyPosition);
198
            $result = array_merge($result, $insert);
199
            $result = array_merge($result, array_slice($array, $keyPosition));
200
        } else {
201
            $result = array_merge($array, $insert);
202
        }
203
        return $result;
204
    }
205
206
    /**
207
     * Insert after
208
     *
209
     * @param array $array
210
     * @param mixed $key
211
     * @param array $insert
212
     * @return array
213
     */
214
    public function insertAfter(array $array, $key, array $insert)
215
    {
216
        return $this->insert($array, $key, $insert);
217
    }
218
219
    /**
220
     * Insert before
221
     *
222
     * @param array $array
223
     * @param mixed $key
224
     * @param array $insert
225
     * @return array
226
     */
227
    public function insertBefore(array $array, $key, array $insert)
228
    {
229
        return $this->insert($array, $key, $insert, self::INSERT_TYPE_BEFORE);
230
    }
231
}