Passed
Branch feature/first-release (43e0cc)
by Andrea Marco
10:48
created

Tokenizer::setTokensMap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Cerbero\JsonParser\Tokens;
4
5
/**
6
 * The tokenizer.
7
 *
8
 */
9
final class Tokenizer
10
{
11
    /**
12
     * The singleton instance.
13
     *
14
     * @var static
15
     */
16
    private static self $instance;
17
18
    /**
19
     * The map of token instances by type.
20
     *
21
     * @var array<int, Token>
22
     */
23
    private array $tokensMap;
24
25
    /**
26
     * Retrieve the singleton instance
27
     *
28
     * @return static
29
     */
30 126
    public static function instance(): static
31
    {
32 126
        return static::$instance ??= new static();
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $instance to at least protected.
Loading history...
33
    }
34
35
    /**
36
     * Instantiate the class.
37
     *
38
     */
39 1
    private function __construct()
40
    {
41 1
        $this->setTokensMap();
42
    }
43
44
    /**
45
     * Set the tokens map
46
     *
47
     * @return void
48
     */
49 1
    private function setTokensMap(): void
50
    {
51 1
        $instances = [];
52
53 1
        foreach (Tokens::MAP as $type => $class) {
54 1
            $this->tokensMap[$type] = $instances[$class] ??= new $class();
55
        }
56
    }
57
58
    /**
59
     * Turn the given value into a token
60
     *
61
     * @param string $value
62
     * @return Token
63
     */
64 126
    public function toToken(string $value): Token
65
    {
66 126
        $type = Tokens::TYPES[$value[0]];
67
68 126
        return $this->tokensMap[$type]->setValue($value);
69
    }
70
}
71