ArrayInput::getOffset()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\Lexer\Runtime\IO;
6
7
use Iterator;
8
use IteratorAggregate;
9
10
use function count;
11
12
final class ArrayInput implements IteratorAggregate, SymbolReaderInterface
13
{
14
15
    /**
16
     * @var int[]
17
     * @psalm-var array<int, int>
18
     */
19
    private $data;
20
21
    /**
22
     * @var int
23
     */
24
    private $offset = 0;
25
26
    /**
27
     * @var int
28
     */
29
    private $length;
30
31 20
    public function __construct(int ...$data)
32
    {
33 20
        $this->data = $data;
34 20
        $this->length = count($data);
35 20
    }
36
37
    /**
38
     * {@inheritDoc}
39
     *
40
     * @return bool
41
     * @psalm-pure
42
     */
43 6
    public function isFinished(): bool
44
    {
45 6
        return $this->length == $this->offset;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     *
51
     * @return SymbolInterface
52
     */
53 14
    public function read(): SymbolInterface
54
    {
55 14
        if ($this->length == $this->offset) {
56 2
            throw new Exception\UnexpectedEndOfInputException($this->offset);
57
        }
58
59 13
        return new Symbol($this->offset, $this->data[$this->offset++], new NullLexeme());
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     *
65
     * @return int
66
     * @psalm-pure
67
     */
68 3
    public function getOffset(): int
69
    {
70 3
        return $this->offset;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     *
76
     * @return LexemeInterface
77
     * @psalm-pure
78
     */
79 4
    public function getEmptyLexeme(): LexemeInterface
80
    {
81 4
        return new EmptyLexeme($this->offset, new NullLexeme());
82
    }
83
84
    /**
85
     * @return Iterator
86
     * @psalm-return Iterator<int, SymbolInterface>
87
     */
88 3
    public function getIterator(): Iterator
89
    {
90 3
        while (!$this->isFinished()) {
91 2
            yield $this->offset => $this->read();
92
        }
93 3
    }
94
}
95