Token   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getShortcut() 0 3 1
A getAttribute() 0 6 2
A setAttribute() 0 6 2
A isEoi() 0 3 1
A attributeExists() 0 3 1
A __construct() 0 4 1
A getType() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\UniLex\Lexer;
6
7
use Remorhaz\UniLex\AttributeListInterface;
8
use Remorhaz\UniLex\Exception;
9
use Remorhaz\UniLex\Parser\AttributeListShortcut;
10
11
class Token implements AttributeListInterface
12
{
13
    private $type;
14
15
    private $isEoi;
16
17
    private $attributeList = [];
18
19
    public function __construct(int $type, bool $isEoi)
20
    {
21
        $this->type = $type;
22
        $this->isEoi = $isEoi;
23
    }
24
25
    public function getType(): int
26
    {
27
        return $this->type;
28
    }
29
30
    public function isEoi(): bool
31
    {
32
        return $this->isEoi;
33
    }
34
35
    /**
36
     * @param string $name
37
     * @param mixed $value
38
     * @throws Exception
39
     */
40
    public function setAttribute(string $name, $value): void
41
    {
42
        if (isset($this->attributeList[$name])) {
43
            throw new Exception("Token attribute '{$name}' is already set");
44
        }
45
        $this->attributeList[$name] = $value;
46
    }
47
48
    /**
49
     * @param string $name
50
     * @return mixed
51
     * @throws Exception
52
     */
53
    public function getAttribute(string $name)
54
    {
55
        if (!$this->attributeExists($name)) {
56
            throw new Exception("Token attribute '{$name}' is not defined");
57
        }
58
        return $this->attributeList[$name];
59
    }
60
61
    /**
62
     * @param string $name
63
     * @return bool
64
     */
65
    public function attributeExists(string $name): bool
66
    {
67
        return isset($this->attributeList[$name]);
68
    }
69
70
    /**
71
     * @return array|AttributeListShortcut
72
     */
73
    public function getShortcut(): AttributeListShortcut
74
    {
75
        return new AttributeListShortcut($this);
76
    }
77
}
78