Passed
Pull Request — master (#236)
by Théo
02:54
created

formatted_filesize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box;
16
17
use Assert\Assertion;
18
use Phar;
19
use function array_key_exists;
20
use function constant;
21
use function define;
22
use function defined;
23
24
/**
25
 * TODO: this function should be pushed down to the PHAR extension.
26
 *
27
 * @private
28
 */
29
function get_phar_compression_algorithms(): array
30
{
31
    static $algorithms = [
32
        'GZ' => Phar::GZ,
33
        'BZ2' => Phar::BZ2,
34
        'NONE' => Phar::NONE,
35
    ];
36
37
    return $algorithms;
38
}
39
40
/**
41
 * @private
42
 */
43
function get_phar_compression_algorithm_extension(int $algorithm): ?string
44
{
45
    static $extensions = [
46
        Phar::GZ => 'zlib',
47
        Phar::BZ2 => 'bz2',
48
        Phar::NONE => null,
49
    ];
50
51
    Assertion::true(array_key_exists($algorithm, $extensions));
52
53
    return $extensions[$algorithm];
54
}
55
56
/**
57
 * @private
58
 */
59
// TODO: add more tests for this
60
function format_size(int $size): string
61
{
62
    $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
63
64
    $power = $size > 0 ? (int) floor(log($size, 1024)) : 0;
65
66
    return sprintf(
67
        '%s%s',
68
        number_format(
69
            $size / (1024 ** $power),
70
            2,
71
            '.',
72
            ','
73
        ),
74
        $units[$power]
75
    );
76
}
77
78
/**
79
 * @private Converts a memory string, e.g. '2000M' to bytes
80
 */
81
function memory_to_bytes(string $value): int
82
{
83
    $unit = strtolower($value[strlen($value) - 1]);
84
85
    $value = (int) $value;
86
    switch ($unit) {
87
        case 'g':
88
            $value *= 1024;
89
        // no break (cumulative multiplier)
90
        case 'm':
91
            $value *= 1024;
92
        // no break (cumulative multiplier)
93
        case 'k':
94
            $value *= 1024;
95
    }
96
97
    return $value;
98
}
99
100
/**
101
 * @private
102
 */
103
function register_aliases(): void
104
{
105
    // Exposes the finder used by PHP-Scoper PHAR to allow its usage in the configuration file.
106
    if (false === class_exists(\Isolated\Symfony\Component\Finder\Finder::class)) {
107
        class_alias(\Symfony\Component\Finder\Finder::class, \Isolated\Symfony\Component\Finder\Finder::class);
108
    }
109
110
    // Register compactors aliases
111
    if (false === class_exists(\Herrera\Box\Compactor\Json::class, false)) {
112
        class_alias(\KevinGH\Box\Compactor\Json::class, \Herrera\Box\Compactor\Json::class);
113
    }
114
115
    if (false === class_exists(\Herrera\Box\Compactor\Php::class, false)) {
116
        class_alias(\KevinGH\Box\Compactor\Php::class, \Herrera\Box\Compactor\Php::class);
117
    }
118
}
119
120
/**
121
 * @private
122
 */
123
function disable_parallel_processing(): void
124
{
125
    define(_NO_PARALLEL_PROCESSING, true);
126
}
127
128
/**
129
 * @private
130
 */
131
function is_parallel_processing_enabled(): bool
132
{
133
    return false === defined(_NO_PARALLEL_PROCESSING) || false === constant(_NO_PARALLEL_PROCESSING);
134
}
135