Completed
Push — master ( 8a5799...ed0b0a )
by Kirill
07:58
created

TokenDelegate::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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\Compiler\Grammar\Delegate;
11
12
use Railt\Lexer\LexerInterface;
13
use Railt\Lexer\SimpleLexerInterface;
14
use Railt\Parser\Ast\Delegate;
15
use Railt\Parser\Ast\Rule;
16
use Railt\Parser\Environment;
17
18
/**
19
 * Class TokenDelegate
20
 */
21
class TokenDelegate extends Rule implements Delegate
22
{
23
    /**
24
     * @param Environment $env
25
     */
26
    public function boot(Environment $env): void
27
    {
28
        /** @var SimpleLexerInterface $lexer */
29
        $lexer = $env->get(LexerInterface::class);
30
31
        $lexer->add($this->getTokenName(), $this->getTokenPattern());
32
33
        if (! $this->isKept()) {
34
            $lexer->skip($this->getTokenName());
35
        }
36
    }
37
38
    /**
39
     * @return bool
40
     */
41
    protected function isKept(): bool
42
    {
43
        return $this->getChild(0)->getName() === 'T_TOKEN';
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    protected function getTokenName(): string
50
    {
51
        return $this->getChild(0)->getValue(0);
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on Railt\Parser\Ast\NodeInterface. Did you maybe mean getValues()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    protected function getTokenPattern(): string
58
    {
59
        return $this->getChild(0)->getValue(1);
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on Railt\Parser\Ast\NodeInterface. Did you maybe mean getValues()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
60
    }
61
}
62