TokenBuilder::setBooleanAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\Lexer\Runtime\Token;
6
7
use Remorhaz\Lexer\Runtime\IO\LexemeInterface;
8
use Remorhaz\Lexer\Runtime\Unicode\AsciiString;
9
use Remorhaz\Lexer\Runtime\Unicode\UnicodeString;
10
11
/**
12
 * @psalm-external-mutation-free
13
 */
14
final class TokenBuilder
15
{
16
17
    /**
18
     * @var int
19
     */
20
    private $id;
21
22
    /**
23
     * @var int
24
     */
25
    private $offset;
26
27
    /**
28
     * @var LexemeInterface
29
     */
30
    private $lexeme;
31
32
    /**
33
     * @var AttributeInterface[]
34
     * @psalm-var array<string, AttributeInterface>
35
     */
36
    private $attributes = [];
37
38
    /**
39
     * @param int             $id
40
     * @param int             $offset
41
     * @param LexemeInterface $lexeme
42
     */
43 16
    public function __construct(int $id, int $offset, LexemeInterface $lexeme)
44
    {
45 16
        $this->id = $id;
46 16
        $this->offset = $offset;
47 16
        $this->lexeme = $lexeme;
48 16
    }
49
50
    /**
51
     * @param string             $name
52
     * @param AttributeInterface $attribute
53
     * @return $this
54
     */
55 12
    private function setAttribute(string $name, AttributeInterface $attribute): self
56
    {
57 12
        $this->attributes[$name] = $attribute;
58
59 12
        return $this;
60
    }
61
62
    /**
63
     * @param string $name
64
     * @param bool   $value
65
     * @return $this
66
     */
67 3
    public function setBooleanAttribute(string $name, bool $value): self
68
    {
69 3
        return $this->setAttribute($name, new BooleanAttribute($value));
70
    }
71
72
    /**
73
     * @param string $name
74
     * @param int    $value
75
     * @return $this
76
     */
77 4
    public function setIntegerAttribute(string $name, int $value): self
78
    {
79 4
        return $this->setAttribute($name, new IntegerAttribute($value));
80
    }
81
82
    /**
83
     * @param string $name
84
     * @param string $value
85
     * @return $this
86
     */
87 2
    public function setAsciiAttribute(string $name, string $value): self
88
    {
89 2
        $string = new AsciiString($value);
90
91 2
        return $this->setAttribute($name, new StringAttribute($string));
92
    }
93
94
    /**
95
     * @param string $name
96
     * @param int    ...$characters
97
     * @return $this
98
     */
99 3
    public function setUnicodeAttribute(string $name, int ...$characters): self
100
    {
101 3
        $string = new UnicodeString(...$characters);
102
103 3
        return $this->setAttribute($name, new StringAttribute($string));
104
    }
105
106
    /**
107
     * @param int $code
108
     * @return $this
109
     */
110 2
    public function setSymbolCode(int $code): self
111
    {
112 2
        return $this->setIntegerAttribute(TokenInput::ATTRIBUTE_SYMBOL_CODE, $code);
113
    }
114
115
    /**
116
     * @return TokenInterface
117
     * @psalm-pure
118
     */
119 11
    public function build(): TokenInterface
120
    {
121 11
        return new Token($this->id, $this->offset, $this->lexeme, new AttributeCollection($this->attributes));
122
    }
123
}
124