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

Utils   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 85
Duplicated Lines 7.06 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 6
loc 85
rs 10
c 1
b 0
f 0
wmc 19
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getHumanReadableBytes() 0 12 1
A isSequential() 0 4 1
C composerGetExtras() 6 41 12
B composerSkipFlags() 0 12 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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