Passed
Push — master ( e14a05...ed0db1 )
by Gabor
05:13
created

GeneralLib::mergeArrayOverwrite()   D

Complexity

Conditions 9
Paths 5

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 20
nc 5
nop 0
dl 0
loc 32
ccs 20
cts 20
cp 1
crap 9
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2018 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi;
15
16
use InvalidArgumentException;
17
18
/**
19
 * Class GeneralLib
20
 */
21
class GeneralLib
22
{
23
    /**
24
     * Collects and returns some information about the render time. First call will start start, the others will return.
25
     *
26
     * @return array
27
     *
28
     * @codeCoverageIgnore - don't test core functions.
29
     */
30
    public static function renderStat() : array
31
    {
32
        static $stat;
33
34
        // Set timer
35
        if (!isset($stat)) {
36
            $stat = [
37
                'start_time' => microtime(true),
38
                'end_time' => null,
39
                'duration' => 0,
40
                'memory' => 0,
41
                'memory_bytes' => 0,
42
            ];
43
44
            return $stat;
45
        }
46
47
        // Get time
48
        $stat['end_time'] = microtime(true);
49
        $stat['duration'] = bcsub((string) $stat['end_time'], (string) $stat['start_time'], 4);
50
51
        // Memory peak
52
        $units = ['bytes', 'KB', 'MB', 'GB', 'TB'];
53
        $bytes = max(memory_get_peak_usage(true), 0);
54
        $stat['memory_bytes'] = number_format($bytes).' bytes';
55
56
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
57
        $pow = min($pow, count($units) - 1);
58
        $bytes /= (1 << (10 * $pow));
59
        $stat['memory'] = round($bytes, 2).' '.$units[$pow];
60
61
        return $stat;
62
    }
63
64
    /**
65
     * Merge config arrays in the correct way.
66
     * This rewrites the given key->value pairs and does not make key->array(value1, value2) like the
67
     * `array_merge_recursive` does.
68
     *
69
     * @throws InvalidArgumentException
70
     * @return array
71
     */
72 33
    public static function mergeArrayOverwrite()
73
    {
74 33
        if (func_num_args() < 2) {
75 1
            throw new InvalidArgumentException(
76 1
                __CLASS__ . '::' . __METHOD__ . ' needs two or more array arguments',
77 1
                1000
78
            );
79
        }
80 33
        $arrays = func_get_args();
81 33
        $merged = [];
82
83 33
        while ($arrays) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arrays 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...
84 33
            $array = array_shift($arrays);
85 33
            if (!is_array($array)) {
86 1
                throw new InvalidArgumentException(
87 1
                    __CLASS__ . '::' . __METHOD__ . ' encountered a non array argument',
88 1
                    1001
89
                );
90
            }
91 33
            if (!$array) {
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...
92 33
                continue;
93
            }
94 33
            foreach ($array as $key => $value) {
95 33
                if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
96 1
                    $merged[$key] = self::mergeArrayOverwrite($merged[$key], $value);
97
                } else {
98 33
                    $merged[$key] = $value;
99
                }
100
            }
101
        }
102
103 33
        return $merged;
104
    }
105
}
106