Passed
Push — master ( 43996d...1595cb )
by Kirill
04:07
created

VariablesVisitor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 25
dl 0
loc 95
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A assertExists() 0 6 2
A enter() 0 14 4
A fetch() 0 11 2
A resolve() 0 7 2
A __construct() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of Railt package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Railt\SDL\Backend;
13
14
use Phplrt\Contracts\Ast\NodeInterface;
15
use Phplrt\Source\Exception\NotAccessibleException;
16
use Phplrt\Visitor\Visitor;
17
use Railt\SDL\Exception\RuntimeErrorException;
18
use Railt\SDL\Exception\TypeErrorException;
19
use Railt\SDL\Frontend\Ast\Definition\ArgumentDefinitionNode;
20
use Railt\SDL\Frontend\Ast\Definition\InputFieldDefinitionNode;
21
use Railt\SDL\Frontend\Ast\Executable\ArgumentNode;
22
use Railt\SDL\Frontend\Ast\Value\VariableValueNode;
23
use Railt\TypeSystem\Value\ValueInterface;
24
25
/**
26
 * Class VariablesVisitor
27
 */
28
class VariablesVisitor extends Visitor
29
{
30
    /**
31
     * @var array
32
     */
33
    private array $variables;
34
35
    /**
36
     * @var ValueFactory
37
     */
38
    private ValueFactory $factory;
39
40
    /**
41
     * VariablesResolver constructor.
42
     *
43
     * @param array $variables
44
     */
45
    public function __construct(array $variables)
46
    {
47
        $this->factory = new ValueFactory();
48
        $this->variables = $variables;
49
    }
50
51
    /**
52
     * @param NodeInterface $node
53
     * @return mixed|ValueInterface|null
54
     * @throws NotAccessibleException
55
     * @throws TypeErrorException
56
     * @throws \RuntimeException
57
     */
58
    public function enter(NodeInterface $node)
59
    {
60
        switch (true) {
61
            case $node instanceof InputFieldDefinitionNode:
62
            case $node instanceof ArgumentDefinitionNode:
63
                $node->defaultValue = $this->resolve($node->defaultValue);
0 ignored issues
show
Bug introduced by
Accessing defaultValue on the interface Phplrt\Contracts\Ast\NodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
64
                break;
65
66
            case $node instanceof ArgumentNode:
67
                $node->value = $this->resolve($node->value);
68
                break;
69
        }
70
71
        return null;
72
    }
73
74
    /**
75
     * @param ValueInterface|null $value
76
     * @return ValueInterface|null
77
     * @throws NotAccessibleException
78
     * @throws TypeErrorException
79
     * @throws \RuntimeException
80
     */
81
    private function resolve(?ValueInterface $value): ?ValueInterface
82
    {
83
        if ($value instanceof VariableValueNode) {
84
            return $this->fetch($value);
85
        }
86
87
        return $value;
88
    }
89
90
    /**
91
     * @param VariableValueNode $var
92
     * @return ValueInterface
93
     * @throws NotAccessibleException
94
     * @throws TypeErrorException
95
     * @throws \RuntimeException
96
     */
97
    private function fetch(VariableValueNode $var): ValueInterface
98
    {
99
        $this->assertExists($var);
100
101
        $value = $this->variables[$var->getName()];
102
103
        if ($value instanceof VariableValueNode) {
104
            return $this->fetch($value);
105
        }
106
107
        return $this->factory->make($value, $var);
108
    }
109
110
    /**
111
     * @param VariableValueNode $var
112
     * @return void
113
     * @throws NotAccessibleException
114
     * @throws RuntimeErrorException
115
     * @throws \RuntimeException
116
     */
117
    private function assertExists(VariableValueNode $var): void
118
    {
119
        if (! isset($this->variables[$var->getName()])) {
120
            $error = \sprintf('Variable $%s not defined', $var->getName());
121
122
            throw new RuntimeErrorException($error, $var);
123
        }
124
    }
125
}
126