Test Failed
Push — master ( b7ab7b...94ee03 )
by Kirill
02:59
created

SymbolTable   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 74
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A declare() 0 8 1
A fetch() 0 9 2
A getIterator() 0 6 2
A __debugInfo() 0 12 2
A count() 0 4 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\IR;
11
12
use Railt\SDL\Exception\CompilerException;
13
use Railt\SDL\Exception\NotFoundException;
14
use Railt\SDL\IR\SymbolTable\VarSymbol;
15
use Railt\SDL\IR\SymbolTable\VarSymbolInterface;
16
17
/**
18
 * Class SymbolTable
19
 */
20
class SymbolTable implements SymbolTableInterface
21
{
22
    /**
23
     * @var int
24
     */
25
    private $id = 0;
26
27
    /**
28
     * @var array|VarSymbolInterface[]
29
     */
30
    private $variables = [];
31
32
    /**
33
     * @param VarSymbolInterface $record
34
     * @return int
35
     */
36
    public function declare(VarSymbolInterface $record): int
37
    {
38
        $id = $this->id++;
39
40
        $this->variables[$id] = $record;
41
42
        return $id;
43
    }
44
45
    /**
46
     * @param int $addr
47
     * @return VarSymbolInterface
48
     * @throws NotFoundException
49
     */
50
    public function fetch(int $addr): VarSymbolInterface
51
    {
52
        if (! isset($this->variables[$addr])) {
53
            $error = \sprintf('Mismatched variable address 0x%08x', $addr);
54
            throw new NotFoundException($error);
55
        }
56
57
        return $this->variables[$addr];
58
    }
59
60
    /**
61
     * @return VarSymbol[]|\Traversable<int,VarSymbol>
0 ignored issues
show
Documentation introduced by
The doc-type VarSymbol[]|\Traversable<int,VarSymbol> could not be parsed: Expected "|" or "end of type", but got "<" at position 24. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
62
     */
63
    public function getIterator(): \Traversable
64
    {
65
        foreach ($this->variables as $id => $symbol) {
66
            yield $id => $symbol;
67
        }
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function __debugInfo(): array
74
    {
75
        $variables = [];
76
        foreach ($this->variables as $id => $var) {
77
            $variables[$id] = (string)$var;
78
        }
79
80
        return [
81
            'size'      => $this->count(),
82
            'variables' => $variables
83
        ];
84
    }
85
86
    /**
87
     * @return int
88
     */
89
    public function count(): int
90
    {
91
        return \count($this->variables);
92
    }
93
}
94