TokenPosition::getLength()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\UniLex\Lexer;
6
7
use Remorhaz\UniLex\Exception;
8
9
/**
10
 * Describes token position in symbol buffer.
11
 */
12
class TokenPosition
13
{
14
    /**
15
     * Offset of the first symbol of token.
16
     *
17
     * @var int
18
     */
19
    private $startOffset;
20
21
    /**
22
     * Offset of the next symbol after token.
23
     *
24
     * @var int
25
     */
26
    private $finishOffset;
27
28
    /**
29
     * Constructor.
30
     * .
31
     * @param int $startOffset
32
     * @param int $finishOffset
33
     * @throws Exception
34
     */
35
    public function __construct(int $startOffset, int $finishOffset)
36
    {
37
        if ($startOffset < 0) {
38
            throw new Exception("Negative start offset in token position: {$startOffset}");
39
        }
40
        if ($finishOffset < $startOffset) {
41
            throw new Exception("Finish offset lesser than start in token position: {$finishOffset} < {$startOffset}");
42
        }
43
        $this->startOffset = $startOffset;
44
        $this->finishOffset = $finishOffset;
45
    }
46
47
    /**
48
     * Returns offset of the first symbol of token.
49
     *
50
     * @return int
51
     */
52
    public function getStartOffset(): int
53
    {
54
        return $this->startOffset;
55
    }
56
57
    /**
58
     * Returns the offset of the next symbol after token.
59
     *
60
     * @return int
61
     */
62
    public function getFinishOffset(): int
63
    {
64
        return $this->finishOffset;
65
    }
66
67
    /**
68
     * Returns length of the token in symbols.
69
     *
70
     * @return int
71
     */
72
    public function getLength(): int
73
    {
74
        return $this->finishOffset - $this->startOffset;
75
    }
76
}
77