Passed
Push — master ( 50961b...a86822 )
by Théo
02:40
created

get_box_version()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 PackageVersions\Versions;
19
use Phar;
20
use function array_key_exists;
21
use function bin2hex;
22
use function class_alias;
23
use function class_exists;
24
use function constant;
25
use function define;
26
use function defined;
27
use function floor;
28
use function log;
29
use function number_format;
30
use function random_bytes;
31
use function sprintf;
32
use function strlen;
33
use function strtolower;
34
35
function get_box_version(): string
36
{
37
    $rawVersion = Versions::getVersion('humbug/box');
38
39
    [$prettyVersion, $commitHash] = explode('@', $rawVersion);
40
41
    return $prettyVersion.'@'.substr($commitHash, 0, 7);
42
}
43
44
/**
45
 * @private
46
 *
47
 * @return <string, int>
0 ignored issues
show
Documentation Bug introduced by
The doc comment <string, int> at position 0 could not be parsed: Unknown type name '<' at position 0 in <string, int>.
Loading history...
48
 */
49
function get_phar_compression_algorithms(): array
50
{
51
    static $algorithms = [
52
        'GZ' => Phar::GZ,
53
        'BZ2' => Phar::BZ2,
54
        'NONE' => Phar::NONE,
55
    ];
56
57
    return $algorithms;
58
}
59
60
function get_phar_compression_algorithm_extension(int $algorithm): ?string
61
{
62
    static $extensions = [
63
        Phar::GZ => 'zlib',
64
        Phar::BZ2 => 'bz2',
65
        Phar::NONE => null,
66
    ];
67
68
    Assertion::true(
69
        array_key_exists($algorithm, $extensions),
70
        sprintf('Unknown compression algorithm code "%d"', $algorithm)
71
    );
72
73
    return $extensions[$algorithm];
74
}
75
76
/**
77
 * @private
78
 *
79
 * @return <string, int>
0 ignored issues
show
Documentation Bug introduced by
The doc comment <string, int> at position 0 could not be parsed: Unknown type name '<' at position 0 in <string, int>.
Loading history...
80
 */
81
function get_phar_signing_algorithms(): array
82
{
83
    static $algorithms = [
84
        'MD5' => Phar::MD5,
85
        'SHA1' => Phar::SHA1,
86
        'SHA256' => Phar::SHA256,
87
        'SHA512' => Phar::SHA512,
88
        'OPENSSL' => Phar::OPENSSL,
89
    ];
90
91
    return $algorithms;
92
}
93
94
function format_size(int $size): string
95
{
96
    $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
97
98
    $power = $size > 0 ? (int) floor(log($size, 1024)) : 0;
99
100
    return sprintf(
101
        '%s%s',
102
        number_format(
103
            $size / (1024 ** $power),
104
            2,
105
            '.',
106
            ','
107
        ),
108
        $units[$power]
109
    );
110
}
111
112
function memory_to_bytes(string $value): int
113
{
114
    $unit = strtolower($value[strlen($value) - 1]);
115
116
    $bytes = (int) $value;
117
    switch ($unit) {
118
        case 'g':
119
            $bytes *= 1024;
120
        // no break (cumulative multiplier)
121
        case 'm':
122
            $bytes *= 1024;
123
        // no break (cumulative multiplier)
124
        case 'k':
125
            $bytes *= 1024;
126
    }
127
128
    return $bytes;
129
}
130
131
function register_aliases(): void
132
{
133
    // Exposes the finder used by PHP-Scoper PHAR to allow its usage in the configuration file.
134
    if (false === class_exists(\Isolated\Symfony\Component\Finder\Finder::class)) {
135
        class_alias(\Symfony\Component\Finder\Finder::class, \Isolated\Symfony\Component\Finder\Finder::class);
136
    }
137
138
    // Register compactors aliases
139
    if (false === class_exists(\Herrera\Box\Compactor\Json::class, false)) {
140
        class_alias(\KevinGH\Box\Compactor\Json::class, \Herrera\Box\Compactor\Json::class);
141
    }
142
143
    if (false === class_exists(\Herrera\Box\Compactor\Php::class, false)) {
144
        class_alias(\KevinGH\Box\Compactor\Php::class, \Herrera\Box\Compactor\Php::class);
145
    }
146
}
147
148
function disable_parallel_processing(): void
149
{
150
    if (false === defined(_NO_PARALLEL_PROCESSING)) {
151
        define(_NO_PARALLEL_PROCESSING, true);
152
    }
153
}
154
155
function is_parallel_processing_enabled(): bool
156
{
157
    return false === defined(_NO_PARALLEL_PROCESSING) || false === constant(_NO_PARALLEL_PROCESSING);
158
}
159
160
/**
161
 * @private
162
 *
163
 * @return string Random 12 charactres long (plus the prefix) string composed of a-z characters and digits
164
 */
165
function unique_id(string $prefix): string
166
{
167
    return $prefix.bin2hex(random_bytes(6));
168
}
169