Completed
Push — master ( 988869...84d590 )
by Lorenzo
08:17
created

array.php ➔ array_remove_columns()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 10
nop 2
dl 0
loc 30
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3 View Code Duplication
if (!function_exists('get')) {
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...
4
    /**
5
     * Get an item from an array using "dot" notation.
6
     *
7
     * @param  array $array
8
     * @param  string $key
9
     * @param  mixed $default
10
     * @return mixed
11
     */
12
    function get($array, $key, $default = null)
13
    {
14
        if (is_null($key)) {
15
            return $array;
16
        }
17
        if (isset($array[$key])) {
18
            return $array[$key];
19
        }
20
        foreach (explode('.', $key) as $segment) {
21
            if (!array_key_exists_safe($segment, $array)) {
22
                return value($default);
23
            }
24
            $array = $array[$segment];
25
        }
26
        return $array;
27
    }
28
}
29
30
if (!function_exists('head')) {
31
    /**
32
     * Get the first element of an array. Useful for method chaining.
33
     *
34
     * @param  array $array
35
     * @return mixed
36
     */
37
    function head($array)
38
    {
39
        return reset($array);
40
    }
41
}
42
43
if (!function_exists('last')) {
44
    /**
45
     * Get the last element from an array.
46
     *
47
     * @param  array $array
48
     * @return mixed
49
     */
50
    function last($array)
51
    {
52
        return end($array);
53
    }
54
}
55
56
if (!function_exists('insert_at_top')) {
57
58
    /**
59
     * Insert element in top of array and return $count element.
60
     * @param $my_arr
61
     * @param $elem
62
     * @param int $count
63
     * @return bool
64
     * @see https://github.com/ifsnop/lpsf/blob/master/src/Ifsnop/functions.inc.php
65
     */
66
    function insert_at_top(&$my_arr, $elem, int $count = 10) : bool
67
    {
68
        if (!is_array($my_arr) || is_null($elem)) {
69
            return false;
70
        }
71
        array_splice($my_arr, $count - 1);
72
        array_unshift($my_arr, $elem);    // Shift every element down one, and insert new at top
73
        return true;
74
    }
75
}
76
77
if (!function_exists('array_has')) {
78
    /**
79
     * Check if an item exists in an array using "dot" notation.
80
     *
81
     * @param  array $array
82
     * @param  string $key
83
     * @return bool
84
     */
85
    function array_has($array, $key)
86
    {
87
        if (empty($array) || is_null($key)) {
88
            return false;
89
        }
90
        if (array_key_exists($key, $array)) {
91
            return true;
92
        }
93
        foreach (explode('.', $key) as $segment) {
94
            if (!array_key_exists_safe($segment, $array)) {
95
                return false;
96
            }
97
            $array = $array[$segment];
98
        }
99
        return true;
100
    }
101
}
102
103 View Code Duplication
if (!function_exists('array_get')) {
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...
104
    /**
105
     * Get an item from an array using "dot" notation.
106
     *
107
     * @param  array $array
108
     * @param  string $key
109
     * @param  mixed $default
110
     * @return mixed
111
     */
112
    function array_get($array, $key, $default = null)
113
    {
114
        if (is_null($key)) {
115
            return $array;
116
        }
117
        if (isset($array[$key])) {
118
            return $array[$key];
119
        }
120
        foreach (explode('.', $key) as $segment) {
121
            if (!is_array($segment) || !array_key_exists_safe($segment, $array)) {
122
                return value($default);
123
            }
124
            $array = $array[$segment];
125
        }
126
        return $array;
127
    }
128
}
129
130
if (!function_exists('array_set')) {
131
    /**
132
     * Set an array item to a given value using "dot" notation.
133
     *
134
     * If no key is given to the method, the entire array will be replaced.
135
     *
136
     * @param  array $array
137
     * @param  string $key
138
     * @param  mixed $value
139
     * @return array
140
     */
141
    function array_set(&$array, $key, $value)
142
    {
143
        if (is_null($key)) {
144
            return $array = $value;
145
        }
146
        $keys = explode('.', $key);
147
        while (count($keys) > 1) {
148
            $key = array_shift($keys);
149
            // If the key doesn't exist at this depth, we will just create an empty array
150
            // to hold the next value, allowing us to create the arrays to hold final
151
            // values at the correct depth. Then we'll keep digging into the array.
152
            if (!isset($array[$key]) || !is_array($array[$key])) {
153
                $array[$key] = array();
154
            }
155
            $array =& $array[$key];
156
        }
157
        $array[array_shift($keys)] = $value;
158
        return $array;
159
    }
160
}
161
162
/**
163
 * Return an array with only integers value contained in the array passed
164
 * @param array $array
165
 * @return array
166
 **/
167
function CleanUpArrayOfInt($array)
168
{
169
    $result = array();
170
    if (isNullOrEmptyArray($array)) {
171
        return $result;
172
    }
173
    reset($array);
174
    while (list($key, $value) = each($array)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $key is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
175
        if (isInteger($value)) {
176
            $result[] = $value;
177
        }
178
    }
179
    reset($array);
180
181
    return $result;
182
}
183
184
if (!function_exists('array_split_filter')) {
185
186
    /**
187
     * Returns an array with two elements.
188
     *
189
     * Iterates over each value in the array passing them to the callback function.
190
     * If the callback function returns true, the current value from array is returned in the first
191
     * element of result array. If not, it is return in the second element of result array.
192
     *
193
     * Array keys are preserved.
194
     *
195
     * @param array $array
196
     * @param callable $callback
197
     * @return array
198
     * @see https://github.com/spatie/array-functions/blob/master/src/array_functions.php
199
     */
200
    function array_split_filter(array $array, callable $callback)
201
    {
202
        $passesFilter = array_filter($array, $callback);
203
        $negatedCallback = function ($item) use ($callback) {
204
            return !$callback($item);
205
        };
206
        $doesNotPassFilter = array_filter($array, $negatedCallback);
207
        return [$passesFilter, $doesNotPassFilter];
208
    }
209
}
210
211
if (!function_exists('in_array_column')) {
212
213
    /**
214
     * Checks whether specific value exists in array of object.
215
     * For exampe, following code
216
     *  $exist = in_array_column([['id' => 1], ['id' => 2], ['id' => 3]], 3, 'id');
217
     * will produce 2
218
     * @author wapmorgan
219
     * @since 2015.05.19
220
     * @param array $haystack Source array
221
     * @param mixed $needle Needed value
222
     * @param string $column Column to perform search
223
     * @param bool $strict Should search be strict or not.
224
     * @return bool True if value exists in array, False otherwise.
225
     * @see modified from https://github.com/wapmorgan/php-functions-repository/blob/master/i/in_array_column.php
226
     */
227
    function in_array_column($haystack, $needle, $column, $strict = false)
228
    {
229
        foreach ($haystack as $k => $elem) {
230
            if ((!$strict && $elem[$column] == $needle) || ($strict && $elem[$column] === $needle)) {
231
                return true;
232
            }
233
        }
234
        return false;
235
    }
236
}
237
238
if (!function_exists('objectToArray')) {
239
240
    /**
241
     * Convert objecte to the array.
242
     *
243
     * @param $object
244
     *
245
     * @return array
246
     * @see https://github.com/ngfw/Recipe/blob/master/src/ngfw/Recipe.php
247
     */
248
    function objectToArray($object) : array
249
    {
250
        if (!is_object($object) && !is_array($object)) {
251
            return [];
252
        }
253
        if (is_object($object)) {
254
            $object = get_object_vars($object);
255
        }
256
        return array_map('objectToArray', $object);
257
    }
258
}
259
260
if (!function_exists('arrayToObject')) {
261
262
    /**
263
     * Convert array to the object.
264
     *
265
     * @param array $array PHP array
266
     *
267
     * @return stdClass (object)
268
     * @see https://github.com/ngfw/Recipe/blob/master/src/ngfw/Recipe.php
269
     */
270
    function arrayToObject(array $array = []) : \stdClass
271
    {
272
        $object = new \stdClass();
273
274
        if (!is_array($array) || count($array) < 1) {
275
            return $object;
276
        }
277
278
        foreach ($array as $name => $value) {
279
            if (is_array($value)) {
280
                $object->$name = arrayToObject($value);
281
                continue;
282
            }
283
            $object->{$name} = $value;
284
        }
285
        return $object;
286
    }
287
}
288
289
if (!function_exists('arrayToString')) {
290
291
    /**
292
     * Convert Array to string
293
     * expected output: <key1>="value1" <key2>="value2".
294
     *
295
     * @param array $array array to convert to string
296
     *
297
     * @return string
298
     * @see https://github.com/ngfw/Recipe/blob/master/src/ngfw/Recipe.php
299
     */
300
    function arrayToString(array $array = []) : string
301
    {
302
        if (isNullOrEmptyArray($array)) {
303
            return '';
304
        }
305
306
        $string = '';
307
        foreach ($array as $key => $value) {
308
            $string .= $key . '="' . $value . '" ';
309
        }
310
        return rtrim($string, ' ');
311
    }
312
}
313
314
if (!function_exists('array_key_exists_safe')) {
315
316
    /**
317
     * Check if a key exists in array
318
     * @param array $array
319
     * @param string $key
320
     * @return bool
321
     */
322
    function array_key_exists_safe(array $array, string $key) : bool
323
    {
324
        if (isNullOrEmptyArray($array) || isNullOrEmpty($key)) {
325
            return false;
326
        }
327
328
        return array_key_exists($key, $array);
329
    }
330
}
331
332
if (!function_exists('array_get_key_value_safe')) {
333
334
    /**
335
     * Retrieve a single key from an array.
336
     * If the key does not exist in the array, or array is null or empty
337
     * the default value will be returned instead.
338
     * @param array $array
339
     * @param string $key
340
     * @param string $default
341
     * @return mixed
342
     */
343
    function array_get_key_value_safe(array $array, string $key, $default = null)
344
    {
345
        if (isNullOrEmptyArray($array) || isNullOrEmpty($key) || !array_key_exists($key, $array)) {
346
            return $default;
347
        }
348
349
        return $array[$key];
350
    }
351
}
352
353
if (!function_exists('isNullOrEmptyArray')) {
354
355
    /**
356
     * Check if array is null or empty.
357
     * @param $array
358
     * @return bool
359
     */
360
    function isNullOrEmptyArray($array):bool
361
    {
362
        return $array === null || !is_array($array) || count($array) < 1;
363
    }
364
}
365
366
if (!function_exists('isNullOrEmptyArrayKey')) {
367
368
    /**
369
     * Check if an array key not exits or exists and is null or empty.
370
     * @param $array
371
     * @param string $key
372
     * @param bool $withTrim if set to true (default) check if trim()!='' too.
373
     * @return bool
374
     */
375
    function isNullOrEmptyArrayKey(array $array, string $key, bool $withTrim = true):bool
376
    {
377
        return !array_key_exists_safe($array, $key) || $array[$key] === null || isNullOrEmpty($array[$key], $withTrim);
378
    }
379
}
380
381
if (!function_exists('isNotNullOrEmptyArray')) {
382
383
    /**
384
     * Check if array is not null and not empty.
385
     * @param $array
386
     * @return bool
387
     */
388
    function isNotNullOrEmptyArray($array):bool
389
    {
390
        return !isNullOrEmptyArray($array);
391
    }
392
}
393
394
if (!function_exists('isNotNullOrEmptyArrayKey')) {
395
396
    /**
397
     * Check if an array key exists and is not null and not empty.
398
     * @param $array
399
     * @param string $key
400
     * @param bool $withTrim if set to true (default) check if trim()!='' too.
401
     * @return bool
402
     */
403
    function isNotNullOrEmptyArrayKey(array $array, string $key, bool $withTrim = true):bool
404
    {
405
        return !isNullOrEmptyArrayKey($array, $key, $withTrim);
406
    }
407
}
408
409
if (!function_exists('array_remove_columns')) {
410
411
    /**
412
     * Remove given column from the subarrays of a two dimensional array.
413
     * @param $array
414
     * @param int $columnToRemove
415
     * @return array
416
     */
417
    function array_remove_columns(array $array, int $columnToRemove):array
418
    {
419
        if (count($array) < 1) {
420
            return [];
421
        }
422
423
        $numCol = count($array[0]);
424
425
        if ($columnToRemove == 1 && isInRange($numCol, 0, 1)) {
426
            return [];
427
        }
428
429
        foreach ($array as &$element) {
430
            if ($columnToRemove == 1) {
431
                $element = array_slice($element, 1, $numCol - 1);
432
            } elseif ($columnToRemove == $numCol) {
433
                $element = array_slice($element, 0, $numCol - 1);
434
            } else {
435
                $element1 = array_slice($element, 0, $columnToRemove - 1);
436
                $element2 = array_slice($element, $columnToRemove, $numCol - $columnToRemove);
437
                $element = array_merge($element1, $element2);
438
            }
439
        }
440
441
        if (!is_array($element)) {
0 ignored issues
show
Bug introduced by
The variable $element does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
442
            return [];
443
        }
444
445
        return $array;
446
    }
447
}
448
449
if (!function_exists('array_remove_first_columns')) {
450
451
    /**
452
     * Remove first column from the subarrays of a two dimensional array.
453
     * @param $array
454
     * @return array
455
     */
456
    function array_remove_first_columns(array $array):array
457
    {
458
        return array_remove_columns($array, 1);
459
    }
460
}
461
462
if (!function_exists('array_remove_last_columns')) {
463
464
    /**
465
     * Remove last column from the subarrays of a two dimensional array.
466
     * @param $array
467
     * @return array
468
     */
469
    function array_remove_last_columns(array $array):array
470
    {
471
        if (count($array) < 1) {
472
            return [];
473
        }
474
475
        $numCol = count($array[0]);
476
477
        return array_remove_columns($array, $numCol);
478
    }
479
}
480