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

Utils::composerGetExtras()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 41
Code Lines 21

Duplication

Lines 6
Ratio 14.63 %

Importance

Changes 0
Metric Value
cc 12
eloc 21
nc 9
nop 1
dl 6
loc 41
rs 5.1612
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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