Passed
Push — master ( 455d8b...50583e )
by Gabor
09:52
created

GeneralLib::renderStat()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 18
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 2
nop 0
crap 12
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
    public static function renderStat() : array
29
    {
30
        static $stat;
31
32
        // Set timer
33
        if (!isset($stat)) {
34
            $stat = [
35
                'start_time' => microtime(true),
36
                'end_time' => null,
37
                'duration' => 0,
38
                'memory' => 0,
39
                'memory_bytes' => 0,
40
            ];
41
42
            return $stat;
43
        }
44
45
        // Get time
46
        $stat['end_time'] = microtime(true);
47
        $stat['duration'] = bcsub((string) $stat['end_time'], (string) $stat['start_time'], 4);
48
49
        // Memory peak
50
        $units = ['bytes', 'KB', 'MB', 'GB', 'TB'];
51
        $bytes = max(memory_get_peak_usage(true), 0);
52
        $stat['memory_bytes'] = number_format($bytes).' bytes';
53
54
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
55
        $pow = min($pow, count($units) - 1);
56
        $bytes /= (1 << (10 * $pow));
57
        $stat['memory'] = round($bytes, 2).' '.$units[$pow];
58
59
        return $stat;
60
    }
61
62
    /**
63
     * Merge config arrays in the correct way.
64
     * This rewrites the given key->value pairs and does not make key->array(value1, value2) like the
65
     * `array_merge_recursive` does.
66
     *
67
     * @throws InvalidArgumentException
68
     * @return array
69
     */
70 27
    public static function mergeArrayOverwrite()
71
    {
72 27
        if (func_num_args() < 2) {
73
            throw new InvalidArgumentException(__CLASS__ . '::' . __METHOD__ . ' needs two or more array arguments');
74
        }
75 27
        $arrays = func_get_args();
76 27
        $merged = [];
77
78 27
        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...
79 27
            $array = array_shift($arrays);
80 27
            if (!is_array($array)) {
81
                throw new InvalidArgumentException(__CLASS__ . '::' . __METHOD__ . ' encountered a non array argument');
82
            }
83 27
            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...
84 27
                continue;
85
            }
86 27
            foreach ($array as $key => $value) {
87 27
                if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
88
                    $merged[$key] = self::mergeArrayOverwrite($merged[$key], $value);
89
                } else {
90 27
                    $merged[$key] = $value;
91
                }
92
            }
93
        }
94
95 27
        return $merged;
96
    }
97
}
98