Passed
Push — master ( e6bc2a...a7212f )
by Kirill
04:45
created

Builder::load()   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 10
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 3
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\SDL\Builder;
11
12
use Railt\Io\Readable;
13
use Railt\Parser\Ast\RuleInterface;
14
use Railt\Reflection\AbstractDefinition;
15
use Railt\Reflection\Contracts\Definition;
16
use Railt\Reflection\Contracts\Definition\TypeDefinition;
17
use Railt\Reflection\Contracts\Document;
18
use Railt\Reflection\Exception\TypeNotFoundException;
19
use Railt\SDL\Compiler\Process;
20
use Railt\SDL\Exception\TypeNotFoundException as SDLTypeNotFoundException;
21
22
/**
23
 * Class Builder
24
 */
25
abstract class Builder implements BuilderInterface
26
{
27
    /**
28
     * @var Readable
29
     */
30
    protected $file;
31
32
    /**
33
     * @var Definition
34
     */
35
    protected $definition;
36
37
    /**
38
     * @var Document
39
     */
40
    protected $document;
41
42
    /**
43
     * @var RuleInterface
44
     */
45
    protected $ast;
46
47
    /**
48
     * @var Process
49
     */
50
    private $process;
51
52
    /**
53
     * Builder constructor.
54
     * @param Context $ctx
55
     * @param RuleInterface $ast
56
     * @param Process $process
57
     */
58
    public function __construct(Context $ctx, RuleInterface $ast, Process $process)
59
    {
60
        $this->ast = $ast;
61
        $this->process = $process;
62
        $this->file = $ctx->getFile();
63
        $this->definition = $ctx->getDefinition();
64
        $this->document = $this->definition->getDocument();
65
    }
66
67
    /**
68
     * @param string $type
69
     * @param Definition $from
70
     * @param int $offset
71
     * @return TypeDefinition
72
     * @throws SDLTypeNotFoundException
73
     */
74
    protected function load(string $type, Definition $from, int $offset): TypeDefinition
75
    {
76
        try {
77
            return $from->getDictionary()->get($type, $from);
78
        } catch (TypeNotFoundException $e) {
79
            $exception = new SDLTypeNotFoundException($e->getMessage());
80
            $exception->throwsIn($from->getFile(), $offset);
81
82
            throw $exception;
83
        }
84
    }
85
86
    /**
87
     * @param \Closure $then
88
     */
89
    protected function async(\Closure $then): void
90
    {
91
        $this->process->async($then);
92
    }
93
94
    /**
95
     * @param Definition|AbstractDefinition $definition
96
     * @return Definition
97
     */
98
    protected function bind(Definition $definition): Definition
99
    {
100
        $definition->withOffset($this->ast->getOffset());
101
102
        return $definition;
103
    }
104
105
    /**
106
     * @return string
107
     * @throws \Railt\SDL\Exception\SyntaxException
108
     */
109
    protected function getName(): string
110
    {
111
        return Utils::getName($this->file, $this->ast);
112
    }
113
114
    /**
115
     * @return string|null
116
     */
117
    protected function findName(): ?string
118
    {
119
        return Utils::findName($this->ast);
120
    }
121
}
122