Backend::linker()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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;
13
14
use GraphQL\Contracts\TypeSystem\SchemaInterface;
15
use Phplrt\Visitor\Traverser;
16
use Phplrt\Visitor\TraverserInterface;
17
use Railt\SDL\Backend\ContextInterface;
18
use Railt\SDL\Backend\HashTable\VariablesVisitor;
19
use Railt\SDL\Backend\HashTableInterface;
20
use Railt\SDL\Backend\Linker\LinkerVisitor;
0 ignored issues
show
Bug introduced by
The type Railt\SDL\Backend\Linker\LinkerVisitor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Railt\SDL\Backend\SymbolTableBuilderVisitor;
22
use Railt\SDL\Frontend\Ast\Node;
23
24
/**
25
 * Class Backend
26
 */
27
class Backend
28
{
29
    /**
30
     * @var Compiler
31
     */
32
    private Compiler $compiler;
33
34
    /**
35
     * @var TraverserInterface
36
     */
37
    private TraverserInterface $traverser;
38
39
    /**
40
     * @var ContextInterface
41
     */
42
    private ContextInterface $ctx;
43
44
    /**
45
     * Backend constructor.
46
     *
47
     * @param Compiler $compiler
48
     * @param ContextInterface $ctx
49
     */
50
    public function __construct(Compiler $compiler, ContextInterface $ctx)
51
    {
52
        $this->ctx = $ctx;
53
        $this->compiler = $compiler;
54
55
        $this->traverser = new Traverser([
56
            $compiler->getSpecification(),
57
            new SymbolTableBuilderVisitor($ctx),
58
        ]);
59
    }
60
61
    /**
62
     * @param iterable|Node[] $ast
63
     * @param HashTableInterface $vars
64
     * @return SchemaInterface
65
     * @throws \Throwable
66
     */
67
    public function run(iterable $ast, HashTableInterface $vars): SchemaInterface
68
    {
69
        // Precompile
70
        $ast = $this->precompile($ast, $vars);
71
72
        // Link types
73
        $ast = $this->linker($ast);
0 ignored issues
show
Unused Code introduced by
The assignment to $ast is dead and can be removed.
Loading history...
74
75
        return $this->ctx->getSchema();
76
    }
77
78
    /**
79
     * @param iterable $ast
80
     * @param HashTableInterface $vars
81
     * @return iterable
82
     */
83
    private function precompile(iterable $ast, HashTableInterface $vars): iterable
84
    {
85
        return (clone $this->traverser)
86
            ->with(new VariablesVisitor($vars))
87
            ->traverse($ast);
88
    }
89
90
    /**
91
     * @param iterable $ast
92
     * @return iterable
93
     */
94
    private function linker(iterable $ast): iterable
95
    {
96
        $traverser = new Traverser([
97
            new LinkerVisitor($this->ctx, $this->compiler->getLinker()),
98
        ]);
99
100
        return $traverser->traverse($ast);
101
    }
102
}
103