Passed
Push — master ( c01dfe...f39ce2 )
by Edward
04:04
created

Nfa::setSymbolTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Remorhaz\UniLex\RegExp\FSM;
4
5
use Remorhaz\UniLex\Exception;
6
7
use function count;
8
9
class Nfa
10
{
11
12
    private $stateMap;
13
14
    private $epsilonTransitionMap;
15
16
    private $symbolTransitionMap;
17
18
    private $symbolTable;
19
20
    public function getStateMap(): StateMap
21
    {
22
        if (!isset($this->stateMap)) {
23
            $this->stateMap = new StateMap();
24
        }
25
26
        return $this->stateMap;
27
    }
28
29
    public function joinStartStates(): void
30
    {
31
        $startStateList = $this->getStateMap()->getStartStateList();
32
        if (count($startStateList) < 2) {
33
            return;
34
        }
35
        $newStartStateId = $this->getStateMap()->createState([]);
0 ignored issues
show
Bug introduced by
array() of type array is incompatible with the type boolean expected by parameter $value of Remorhaz\UniLex\RegExp\FSM\StateMap::createState(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $newStartStateId = $this->getStateMap()->createState(/** @scrutinizer ignore-type */ []);
Loading history...
36
        foreach ($startStateList as $oldStartStateId) {
37
            $this->getEpsilonTransitionMap()->addTransition($newStartStateId, $oldStartStateId, true);
38
        }
39
        $this->getStateMap()->replaceStartStateList($newStartStateId);
40
    }
41
42
    public function getEpsilonTransitionMap(): TransitionMap
43
    {
44
        if (!isset($this->epsilonTransitionMap)) {
45
            $this->epsilonTransitionMap = new TransitionMap($this->getStateMap());
46
        }
47
48
        return $this->epsilonTransitionMap;
49
    }
50
51
    public function getSymbolTransitionMap(): TransitionMap
52
    {
53
        if (!isset($this->symbolTransitionMap)) {
54
            $this->symbolTransitionMap = new TransitionMap($this->getStateMap());
55
        }
56
57
        return $this->symbolTransitionMap;
58
    }
59
60
    public function getSymbolTable(): SymbolTable
61
    {
62
        if (!isset($this->symbolTable)) {
63
            $this->symbolTable = new SymbolTable();
64
        }
65
66
        return $this->symbolTable;
67
    }
68
69
    public function setSymbolTable(SymbolTable $symbolTable): void
70
    {
71
        if (isset($this->symbolTable)) {
72
            throw new Exception("Symbol table already exists in DFA");
73
        }
74
        $this->symbolTable = $symbolTable;
75
    }
76
}
77