Bootup::get_random_bytes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7
/**
8
 * @psalm-immutable
9
 */
10
class Bootup
11
{
12
    /**
13
     * Normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.
14
     *
15
     * @param mixed  $input
16
     * @param int    $normalization_form
17
     * @param string $leading_combining
18
     *
19
     * @return mixed
20
     */
21
    public static function filterString(
22
        $input,
23
        int $normalization_form = \Normalizer::NFC,
24
        string $leading_combining = '◌'
25
    ) {
26
        return UTF8::filter(
27
            $input,
28
            $normalization_form,
29
            $leading_combining
30
        );
31
    }
32
33
    /**
34
     * Get random bytes via "random_bytes()"
35
     *
36
     * @param int $length <p>output length</p>
37
     *
38
     * @throws \Exception if it was not possible to gather sufficient entropy
39
     *
40
     * @return false|string
41
     *                      <strong>false</strong> on error
42
     */
43 1
    public static function get_random_bytes($length)
44
    {
45 1
        if (!$length) {
46 1
            return false;
47
        }
48
49 1
        $length = (int) $length;
50
51 1
        if ($length <= 0) {
52 1
            return false;
53
        }
54
55 1
        return \random_bytes($length);
56
    }
57
58
    /**
59
     * Constant FILTER_SANITIZE_STRING polyfill for PHP > 8.1
60
     *
61
     * INFO: https://stackoverflow.com/a/69207369/1155858
62
     *
63
     * @param string $str
64
     *
65
     * @return false|string
66
     */
67 12
    public static function filter_sanitize_string_polyfill(string $str)
68
    {
69 12
        $str = \preg_replace('/\x00|<[^>]*>?/', '', $str);
70 12
        if ($str === null) {
71
            return false;
72
        }
73
74 12
        return \str_replace(["'", '"'], ['&#39;', '&#34;'], $str);
75
    }
76
77
    /**
78
     * @return bool
79
     */
80 1
    public static function initAll(): bool
81
    {
82 1
        $result = \ini_set('default_charset', 'UTF-8');
83
84
        // everything else is init via composer, so we are done here ...
85
86 1
        return $result !== false;
87
    }
88
89
    /**
90
     * Determines if the current version of PHP is equal to or greater than the supplied value.
91
     *
92
     * @param string $version <p>e.g. "7.1"<p>
93
     *
94
     * @return bool
95
     *              <p>Return <strong>true</strong> if the current version is $version or greater.</p>
96
     *
97
     * @psalm-pure
98
     */
99 29
    public static function is_php($version): bool
100
    {
101
        /**
102
         * @psalm-suppress ImpureStaticVariable
103
         *
104
         * @var bool[]
105
         */
106
        static $_IS_PHP;
107
108 29
        $version = (string) $version;
109
110 29
        if (!isset($_IS_PHP[$version])) {
111 4
            $_IS_PHP[$version] = \version_compare(\PHP_VERSION, $version, '>=');
112
        }
113
114 29
        return $_IS_PHP[$version];
115
    }
116
}
117