Passed
Push — master ( 4ede80...80c5aa )
by Théo
02:05
created

unique_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 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 function bin2hex;
19
use Phar;
20
use function array_key_exists;
21
use function constant;
22
use function define;
23
use function defined;
24
use function random_bytes;
25
use function sprintf;
26
use function strlen;
27
28
/**
29
 * @private
30
 *
31
 * @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...
32
 */
33
function get_phar_compression_algorithms(): array
34
{
35
    static $algorithms = [
36
        'GZ' => Phar::GZ,
37
        'BZ2' => Phar::BZ2,
38
        'NONE' => Phar::NONE,
39
    ];
40
41
    return $algorithms;
42
}
43
44
/**
45
 * @private
46
 */
47
function get_phar_compression_algorithm_extension(int $algorithm): ?string
48
{
49
    static $extensions = [
50
        Phar::GZ => 'zlib',
51
        Phar::BZ2 => 'bz2',
52
        Phar::NONE => null,
53
    ];
54
55
    Assertion::true(
56
        array_key_exists($algorithm, $extensions),
57
        sprintf('Unknown compression algorithm code "%d"', $algorithm)
58
    );
59
60
    return $extensions[$algorithm];
61
}
62
63
/**
64
 * @private
65
 *
66
 * @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...
67
 */
68
function get_phar_signing_algorithms(): array
69
{
70
    static $algorithms = [
71
        'MD5' => Phar::MD5,
72
        'SHA1' => Phar::SHA1,
73
        'SHA256' => Phar::SHA256,
74
        'SHA512' => Phar::SHA512,
75
        'OPENSSL' => Phar::OPENSSL,
76
    ];
77
78
    return $algorithms;
79
}
80
81
/**
82
 * @private
83
 */
84
// TODO: add more tests for this
85
function format_size(int $size): string
86
{
87
    $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
88
89
    $power = $size > 0 ? (int) floor(log($size, 1024)) : 0;
90
91
    return sprintf(
92
        '%s%s',
93
        number_format(
94
            $size / (1024 ** $power),
95
            2,
96
            '.',
97
            ','
98
        ),
99
        $units[$power]
100
    );
101
}
102
103
/**
104
 * @private Converts a memory string, e.g. '2000M' to bytes
105
 */
106
function memory_to_bytes(string $value): int
107
{
108
    $unit = strtolower($value[strlen($value) - 1]);
109
110
    $value = (int) $value;
111
    switch ($unit) {
112
        case 'g':
113
            $value *= 1024;
114
        // no break (cumulative multiplier)
115
        case 'm':
116
            $value *= 1024;
117
        // no break (cumulative multiplier)
118
        case 'k':
119
            $value *= 1024;
120
    }
121
122
    return $value;
123
}
124
125
/**
126
 * @private
127
 */
128
function register_aliases(): void
129
{
130
    // Exposes the finder used by PHP-Scoper PHAR to allow its usage in the configuration file.
131
    if (false === class_exists(\Isolated\Symfony\Component\Finder\Finder::class)) {
132
        class_alias(\Symfony\Component\Finder\Finder::class, \Isolated\Symfony\Component\Finder\Finder::class);
133
    }
134
135
    // Register compactors aliases
136
    if (false === class_exists(\Herrera\Box\Compactor\Json::class, false)) {
137
        class_alias(\KevinGH\Box\Compactor\Json::class, \Herrera\Box\Compactor\Json::class);
138
    }
139
140
    if (false === class_exists(\Herrera\Box\Compactor\Php::class, false)) {
141
        class_alias(\KevinGH\Box\Compactor\Php::class, \Herrera\Box\Compactor\Php::class);
142
    }
143
}
144
145
/**
146
 * @private
147
 */
148
function disable_parallel_processing(): void
149
{
150
    if (false == defined(_NO_PARALLEL_PROCESSING)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
151
        define(_NO_PARALLEL_PROCESSING, true);
152
    }
153
}
154
155
/**
156
 * @private
157
 */
158
function is_parallel_processing_enabled(): bool
159
{
160
    return false === defined(_NO_PARALLEL_PROCESSING) || false === constant(_NO_PARALLEL_PROCESSING);
161
}
162
163
/**
164
 * @private
165
 *
166
 * @return string Random 12 charactres long (plus the prefix) string composed of a-z characters and digits
167
 */
168
function unique_id(string $prefix): string
169
{
170
    return $prefix.bin2hex(random_bytes(6));
171
}
172