Test Failed
Push — master ( 3ca5bd...939415 )
by Kirill
02:50
created

MultistateLexer::getNextState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\Lexer\Driver;
11
12
use Railt\Lexer\MultistateLexerInterface;
13
14
/**
15
 * Class MultistateLexer
16
 */
17
abstract class MultistateLexer extends Lexer implements MultistateLexerInterface
18
{
19
    /**
20
     * @var int
21
     */
22
    protected const DEFAULT_STATE = 0x00;
23
24
    /**
25
     * @var array|int[]
26
     */
27
    protected $states = [];
28
29
    /**
30
     * @var array|int[]
31
     */
32
    protected $transitions = [];
33
34
    /**
35
     * @param string $token
36
     * @param int $state
37
     * @param int|null $nextState
38
     * @return MultistateLexerInterface
39
     */
40
    public function state(string $token, int $state, int $nextState = null): MultistateLexerInterface
41
    {
42
        $this->states[$token] = $state;
43
44
        if ($nextState !== null) {
45
            $this->transitions[$token] = $nextState;
46
        }
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param string $token
53
     * @return int
54
     */
55
    protected function getTokenState(string $token): int
56
    {
57
        return $this->states[$token] ?? static::DEFAULT_STATE;
58
    }
59
60
    /**
61
     * @param string $token
62
     * @return int
63
     */
64
    protected function getNextState(string $token): int
65
    {
66
        return $this->transitions[$this->getTokenState($token)] ?? static::DEFAULT_STATE;
67
    }
68
}
69