Token   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 100
ccs 18
cts 18
cp 1
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createEoi() 0 6 1
A getId() 0 3 1
A isFinal() 0 3 1
A getOffset() 0 3 1
A getAttributes() 0 3 1
A __construct() 0 10 1
A getLexeme() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\Lexer\Runtime\Token;
6
7
use Remorhaz\Lexer\Runtime\IO\LexemeInterface;
8
9
/**
10
 * @psalm-immutable
11
 */
12
final class Token implements TokenInterface
13
{
14
15
    /**
16
     * @var int
17
     */
18
    private $id;
19
20
    /**
21
     * @var int
22
     */
23
    private $offset;
24
25
    /**
26
     * @var LexemeInterface
27
     */
28
    private $lexeme;
29
30
    /**
31
     * @var AttributeCollectionInterface
32
     */
33
    private $attributes;
34
35
    /**
36
     * @param int                               $offset
37
     * @param LexemeInterface                   $lexeme
38
     * @param AttributeCollectionInterface|null $attributes
39
     * @return TokenInterface
40
     */
41 5
    public static function createEoi(
42
        int $offset,
43
        LexemeInterface $lexeme,
44
        ?AttributeCollectionInterface $attributes = null
45
    ): TokenInterface {
46 5
        return new self(TokenInterface::ID_EOI, $offset, $lexeme, $attributes ?? new AttributeCollection());
47
    }
48
49
    /**
50
     * Token constructor.
51
     *
52
     * @param int                               $id
53
     * @param int                               $offset
54
     * @param LexemeInterface                   $lexeme
55
     * @param AttributeCollectionInterface|null $attributes
56
     */
57 12
    public function __construct(
58
        int $id,
59
        int $offset,
60
        LexemeInterface $lexeme,
61
        ?AttributeCollectionInterface $attributes = null
62
    ) {
63 12
        $this->id = $id;
64 12
        $this->offset = $offset;
65 12
        $this->lexeme = $lexeme;
66 12
        $this->attributes = $attributes ?? new AttributeCollection();
67 12
    }
68
69
    /**
70
     * @return int
71
     * @psalm-pure
72
     */
73 2
    public function getId(): int
74
    {
75 2
        return $this->id;
76
    }
77
78
    /**
79
     * @return int
80
     * @psalm-pure
81
     */
82 2
    public function getOffset(): int
83
    {
84 2
        return $this->offset;
85
    }
86
87
    /**
88
     * @return LexemeInterface
89
     * @psalm-pure
90
     */
91 2
    public function getLexeme(): LexemeInterface
92
    {
93 2
        return $this->lexeme;
94
    }
95
96
    /**
97
     * @return AttributeCollectionInterface
98
     * @psalm-pure
99
     */
100 4
    public function getAttributes(): AttributeCollectionInterface
101
    {
102 4
        return $this->attributes;
103
    }
104
105
    /**
106
     * @return bool
107
     * @psalm-pure
108
     */
109 2
    public function isFinal(): bool
110
    {
111 2
        return self::ID_EOI == $this->id;
112
    }
113
}
114