Passed
Push — master ( c49687...b05fa7 )
by Alec
11:36 queued 09:39
created

UnicodeVersion::refine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
0 ignored issues
show
Coding Style introduced by
Header blocks must be separated by a single blank line
Loading history...
4
// 02.03.23
5
namespace AlecRabbit\WCWidth\Kernel;
6
7
use AlecRabbit\WCWidth\Kernel\Contract\IUnicodeVersion;
0 ignored issues
show
Coding Style introduced by
Header blocks must not contain blank lines
Loading history...
8
9
use InvalidArgumentException;
10
11
use function in_array;
12
13
use const AlecRabbit\WCWidth\UNICODE_VERSIONS;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\WCWidth\UNICODE_VERSIONS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
14
15
/** @internal */
16
final class UnicodeVersion implements IUnicodeVersion
17
{
18
    private static ?string $useVersion = null;
19
20
    // @codeCoverageIgnoreStart
21
    private function __construct()
22
    {
23
        // no instances
24
    }
25
26
    // @codeCoverageIgnoreEnd
27
28
    public static function setVersion(string $version): void
29
    {
30
        self::$useVersion = self::doRefine($version);
31
    }
32
33
    private static function doRefine(?string $version): string
34
    {
35
        if ('latest' === $version || null === $version) {
36
            $version = self::latest();
37
        }
38
39
        self::assertVersion($version);
40
41
        return
42
            $version;
43
    }
44
45
    public static function latest(): string
46
    {
47
        return
48
            UNICODE_VERSIONS[array_key_last(UNICODE_VERSIONS)];
49
    }
50
51
    private static function assertVersion(string $version): void
52
    {
53
        if (!in_array($version, UNICODE_VERSIONS, true)) {
54
            throw new InvalidArgumentException(
55
                sprintf(
56
                    'Unknown Unicode version: %s',
57
                    $version
58
                )
59
            );
60
        }
61
    }
62
63
    public static function getVersion(): ?string
64
    {
65
        return self::$useVersion;
66
    }
67
68
    public static function reset(): void
69
    {
70
        self::$useVersion = null;
71
    }
72
73
    public static function refine(?string $version): string
74
    {
75
        if (self::$useVersion) {
76
            return self::$useVersion;
77
        }
78
79
        return self::doRefine($version);
80
    }
81
82
}