Passed
Push — master ( d2779f...9acd42 )
by Edward
06:48
created

ArrayInput   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 13
c 1
b 0
f 0
dl 0
loc 75
ccs 17
cts 17
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isFinished() 0 3 1
A getIterator() 0 4 2
A getEmptyLexeme() 0 3 1
A read() 0 7 2
A getOffset() 0 3 1
A __construct() 0 4 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
     */
18
    private $data;
19
20
    /**
21
     * @var int
22
     */
23
    private $offset = 0;
24
25
    /**
26
     * @var int
27
     */
28
    private $length;
29
30 20
    public function __construct(int ...$data)
31
    {
32 20
        $this->data = $data;
33 20
        $this->length = count($data);
34 20
    }
35
36
    /**
37
     * {@inheritDoc}
38
     *
39
     * @return bool
40
     * @psalm-pure
41
     */
42 6
    public function isFinished(): bool
43
    {
44 6
        return $this->length == $this->offset;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     *
50
     * @return SymbolInterface
51
     */
52 14
    public function read(): SymbolInterface
53
    {
54 14
        if ($this->length == $this->offset) {
55 2
            throw new Exception\UnexpectedEndOfInputException($this->offset);
56
        }
57
58 13
        return new Symbol($this->offset, $this->data[$this->offset++], new NullLexeme());
59
    }
60
61
    /**
62
     * @return int
63
     * @psalm-pure
64
     */
65 3
    public function getOffset(): int
66
    {
67 3
        return $this->offset;
68
    }
69
70
    /**
71
     * @return LexemeInterface
72
     * @psalm-pure
73
     */
74 4
    public function getEmptyLexeme(): LexemeInterface
75
    {
76 4
        return new EmptyLexeme($this->offset, new NullLexeme());
77
    }
78
79
    /**
80
     * @return Iterator
81
     * @psalm-return Iterator<int,SymbolInterface>
82
     */
83 3
    public function getIterator(): Iterator
84
    {
85 3
        while (!$this->isFinished()) {
86 2
            yield $this->offset => $this->read();
87
        }
88 3
    }
89
}
90