Test Setup Failed
Push — next ( 738e04...b06172 )
by Jonathan
25:05
created

Utils::isTrace()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 8
nop 1
dl 0
loc 36
rs 4.909
c 0
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
    public static function isTrace(array $trace)
94
    {
95
        if (!self::isSequential($trace)) {
96
            return false;
97
        }
98
99
        static $bt_structure = array(
100
            'function' => 'string',
101
            'line' => 'integer',
102
            'file' => 'string',
103
            'class' => 'string',
104
            'object' => 'object',
105
            'type' => 'string',
106
            'args' => 'array',
107
        );
108
109
        $file_found = false;
0 ignored issues
show
Coding Style introduced by
$file_found does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
110
111
        foreach ($trace as $frame) {
112
            if (!is_array($frame) || !isset($frame['function'])) {
113
                return false;
114
            }
115
116
            foreach ($frame as $key => $val) {
117
                if (!isset($bt_structure[$key])) {
0 ignored issues
show
Coding Style introduced by
$bt_structure does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
118
                    return false;
119
                } elseif (gettype($val) !== $bt_structure[$key]) {
0 ignored issues
show
Coding Style introduced by
$bt_structure does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
120
                    return false;
121
                } elseif ($key === 'file') {
122
                    $file_found = true;
0 ignored issues
show
Coding Style introduced by
$file_found does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
123
                }
124
            }
125
        }
126
127
        return $file_found;
0 ignored issues
show
Coding Style introduced by
$file_found does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
128
    }
129
130
    public static function traceFrameIsListed(array $frame, array $matches)
0 ignored issues
show
Coding Style introduced by
function traceFrameIsListed() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
131
    {
132
        if (isset($frame['class'])) {
133
            $called = array(strtolower($frame['class']), strtolower($frame['function']));
134
        } else {
135
            $called = strtolower($frame['function']);
136
        }
137
138
        return in_array($called, $matches, true);
139
    }
140
141
    public static function normalizeAliases(array &$aliases)
142
    {
143
        foreach ($aliases as $index => &$alias) {
144
            if (is_array($alias) && count($alias) === 2) {
145
                $alias = array_values(array_filter($alias, 'is_string'));
146
147
                if (count($alias) === 2 &&
148
                    preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $alias[1]) &&
149
                    preg_match('/^[a-zA-Z_\x7f-\xff\\\\][a-zA-Z0-9_\x7f-\xff\\\\]*$/', $alias[0])
150
                ) {
151
                    $alias = array(
152
                        strtolower(ltrim($alias[0], '\\')),
153
                        strtolower($alias[1]),
154
                    );
155
                } else {
156
                    unset($aliases[$index]);
157
                    continue;
158
                }
159
            } elseif (is_string($alias)) {
160
                if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $alias)) {
161
                    $alias = strtolower($alias);
162
                } else {
163
                    unset($aliases[$index]);
164
                    continue;
165
                }
166
            } else {
167
                unset($aliases[$index]);
168
            }
169
        }
170
171
        $aliases = array_values($aliases);
172
    }
173
}
174