Tokenizer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 10
c 5
b 0
f 0
dl 0
loc 60
ccs 11
cts 11
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setTokensMap() 0 6 2
A toToken() 0 5 1
A instance() 0 3 1
A __construct() 0 3 1
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 self
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 self
29
     */
30 361
    public static function instance(): self
31
    {
32 361
        return self::$instance ??= new self();
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 357
    public function toToken(string $value): Token
65
    {
66 357
        $type = Tokens::TYPES[$value[0]];
67
68 357
        return $this->tokensMap[$type]->setValue($value);
69
    }
70
}
71