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

TokenInput   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 72.21%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getEmptyLexeme() 0 3 1
A __construct() 0 3 1
A isFinished() 0 3 1
A getOffset() 0 3 1
A getIterator() 0 4 2
A read() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\Lexer\Runtime\Token;
6
7
use Iterator;
8
use IteratorAggregate;
9
use Remorhaz\Lexer\Runtime\IO\Exception;
10
use Remorhaz\Lexer\Runtime\IO\LexemeInterface;
11
use Remorhaz\Lexer\Runtime\IO\Symbol;
12
use Remorhaz\Lexer\Runtime\IO\SymbolInterface;
13
use Remorhaz\Lexer\Runtime\IO\SymbolReaderInterface;
14
15
use function is_int;
16
17
final class TokenInput implements IteratorAggregate, SymbolReaderInterface
18
{
19
20
    public const ATTRIBUTE_SYMBOL_CODE = 'symbol.code';
21
22
    /**
23
     * @var TokenReaderInterface
24
     */
25
    private $tokenReader;
26
27 6
    public function __construct(TokenReaderInterface $tokenReader)
28
    {
29 6
        $this->tokenReader = $tokenReader;
30 6
    }
31
32 3
    public function read(): SymbolInterface
33
    {
34 3
        $token = $this->tokenReader->read();
35
        /** @psalm-var mixed $symbolCode */
36 3
        $symbolCode = $token->getAttribute(self::ATTRIBUTE_SYMBOL_CODE);
37 3
        if (is_int($symbolCode)) {
38 2
            return new Symbol($token->getOffset(), $symbolCode, $token->getLexeme());
39
        }
40
41 1
        throw new Exception\InvalidSymbolCodeException($symbolCode);
42
    }
43
44
    /**
45
     * @return int
46
     * @psalm-pure
47
     */
48 1
    public function getOffset(): int
49
    {
50 1
        return $this->tokenReader->getOffset();
51
    }
52
53
    /**
54
     * @return bool
55
     * @psalm-pure
56
     */
57 2
    public function isFinished(): bool
58
    {
59 2
        return $this->tokenReader->isFinished();
60
    }
61
62
    /**
63
     * @return LexemeInterface
64
     * @psalm-pure
65
     */
66
    public function getEmptyLexeme(): LexemeInterface
67
    {
68
        return $this->tokenReader->getEmptyLexeme();
69
    }
70
71
    /**
72
     * @return Iterator
73
     * @psalm-return Iterator<int,SymbolInterface>
74
     */
75
    public function getIterator(): Iterator
76
    {
77
        while (!$this->isFinished()) {
78
            yield $this->getOffset() => $this->read();
79
        }
80
    }
81
}
82