Test Setup Failed
Push — test ( 089b88...0e81d9 )
by Jonathan
04:36
created

Utils::getHumanReadableBytes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace Kint;
4
5
/**
6
 * A collection of utility methods. Should all be static methods with no dependencies.
7
 */
8
class Utils
9
{
10
    /**
11
     * Turns a byte value into a human-readable representation.
12
     *
13
     * @param int $value Amount of bytes
14
     *
15
     * @return array Human readable value and unit
16
     */
17
    public static function getHumanReadableBytes($value)
18
    {
19
        static $unit = array('B', 'KB', 'MB', 'GB', 'TB');
20
21
        $i = floor(log($value, 1024));
22
        $i = min($i, 4); // Only go up to TB
23
24
        return array(
25
            'value' => (float) ($value / pow(1024, $i)),
26
            'unit' => $unit[$i],
27
        );
28
    }
29
30
    public static function isSequential(array $array)
31
    {
32
        return array_keys($array) === range(0, count($array) - 1);
33
    }
34
35
    public static function composerGetExtras($key = 'kint')
36
    {
37
        $extras = array();
38
39
        if (strpos(KINT_DIR, 'phar://') === 0) {
40
            // Only run inside phar file, so skip for code coverage
41
            return $extras; // @codeCoverageIgnore
42
        }
43
44
        $folder = KINT_DIR.'/vendor';
45
46
        for ($i = 0; $i < 4; ++$i) {
47
            $installed = $folder.'/composer/installed.json';
48
49
            if (file_exists($installed) && is_readable($installed)) {
50
                $packages = json_decode(file_get_contents($installed), true);
51
52
                foreach ($packages as $package) {
53 View Code Duplication
                    if (isset($package['extra'][$key]) && is_array($package['extra'][$key])) {
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...
54
                        $extras = array_replace($extras, $package['extra'][$key]);
55
                    }
56
                }
57
58
                $folder = dirname($folder);
59
60
                if (file_exists($folder.'/composer.json') && is_readable($folder.'/composer.json')) {
61
                    $composer = json_decode(file_get_contents($folder.'/composer.json'), true);
62
63 View Code Duplication
                    if (isset($composer['extra'][$key]) && is_array($composer['extra'][$key])) {
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...
64
                        $extras = array_replace($extras, $composer['extra'][$key]);
65
                    }
66
                }
67
68
                break;
69
            } else {
70
                $folder = dirname($folder);
71
            }
72
        }
73
74
        return $extras;
75
    }
76
77
    /**
78
     * @codeCoverageIgnore
79
     */
80
    public static function composerSkipFlags()
81
    {
82
        $extras = self::composerGetExtras();
83
84
        if (!empty($extras['disable-facade']) && !defined('KINT_SKIP_FACADE')) {
85
            define('KINT_SKIP_FACADE', true);
86
        }
87
88
        if (!empty($extras['disable-helpers']) && !defined('KINT_SKIP_HELPERS')) {
89
            define('KINT_SKIP_HELPERS', true);
90
        }
91
    }
92
}
93