Passed
Push — master ( 512e55...80c17a )
by Lars
04:48 queued 01:48
created

Bootup   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 106
ccs 20
cts 26
cp 0.7692
rs 10
wmc 9

5 Methods

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