Test Failed
Push — master ( 08b170...e6bc2a )
by Kirill
02:02
created

Compiler::runCompiler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
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;
11
12
use Railt\Io\File;
13
use Railt\Io\Readable;
14
use Railt\Reflection\Contracts\Definition;
15
use Railt\Reflection\Contracts\Document as DocumentInterface;
16
use Railt\Reflection\Contracts\Reflection as ReflectionInterface;
17
use Railt\Reflection\Dictionary\CallbackDictionary;
18
use Railt\Reflection\Document;
19
use Railt\Reflection\Reflection;
20
use Railt\SDL\Compiler\Backend;
21
use Railt\SDL\Compiler\Frontend;
22
use Railt\SDL\Compiler\Process;
23
use Railt\SDL\Exception\InternalErrorException;
24
use Railt\SDL\Exception\SyntaxException;
25
use Railt\SDL\Exception\TypeException;
26
27
/**
28
 * Class Compiler
29
 */
30
class Compiler implements CompilerInterface
31
{
32
    /**
33
     * @var ReflectionInterface
34
     */
35
    private $reflection;
36
37
    /**
38
     * @var Frontend
39
     */
40
    private $frontend;
41
42
    /**
43
     * @var Backend
44
     */
45
    private $backend;
46
47
    /**
48
     * @var Process
49
     */
50
    private $process;
51
52
    /**
53
     * Compiler constructor.
54
     */
55
    public function __construct()
56
    {
57
        $this->process = new Process();
58
        $this->frontend = new Frontend();
59
        $this->backend = new Backend($this->process);
60
        $this->reflection = new Reflection(new CallbackDictionary($this->typeLoader()));
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Railt\Reflection\Re...y($this->typeLoader())) of type object<Railt\Reflection\Reflection> is incompatible with the declared type object<Railt\Reflection\Contracts\Reflection> of property $reflection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
    }
62
63
    /**
64
     * @return \Closure
65
     */
66
    private function typeLoader(): \Closure
67
    {
68
        return function (string $type, Definition $from = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $from is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
            $this->compile(File::fromPathname($type . '.graphqls'));
0 ignored issues
show
Bug introduced by
It seems like \Railt\Io\File::fromPathname($type . '.graphqls') targeting Railt\Io\File::fromPathname() can also be of type object<Railt\Io\File>; however, Railt\SDL\Compiler::compile() does only seem to accept object<Railt\Io\Readable>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
70
        };
71
    }
72
73
    /**
74
     * @param Readable $schema
75
     * @return DocumentInterface
76
     * @throws InternalErrorException
77
     * @throws SyntaxException
78
     * @throws TypeException
79
     */
80
    public function compile(Readable $schema): DocumentInterface
81 283
    {
82
        $document = $this->runCompiler($schema);
83 283
84 283
        $this->process->run();
85 283
86 283
        return $document;
87 283
    }
88
89 283
    /**
90
     * @param Readable $schema
91 283
     * @return DocumentInterface
92 283
     * @throws InternalErrorException
93
     * @throws SyntaxException
94
     * @throws TypeException
95
     */
96
    private function runCompiler(Readable $schema): DocumentInterface
97
    {
98
        return $this->run(clone $this->reflection, $schema);
99
    }
100 283
101
    /**
102
     * @param ReflectionInterface $reflection
103 283
     * @param Readable $schema
104
     * @return DocumentInterface
105
     * @throws InternalErrorException
106
     * @throws SyntaxException
107
     * @throws TypeException
108 283
     */
109
    private function run(ReflectionInterface $reflection, Readable $schema): DocumentInterface
110
    {
111
        return $this->backend->each(
112
            $this->backend->context($this->document($reflection, $schema)),
113
            $this->frontend->exec($schema)
114
        );
115 283
    }
116
117 283
    /**
118 283
     * @param ReflectionInterface $reflection
119
     * @param Readable $schema
120
     * @return DocumentInterface
121 283
     */
122 283
    private function document(ReflectionInterface $reflection, Readable $schema): DocumentInterface
123
    {
124
        return new Document($reflection, $schema);
125 283
    }
126
127
    /**
128
     * @param Readable $schema
129
     * @return ReflectionInterface
130
     * @throws InternalErrorException
131
     * @throws SyntaxException
132 283
     * @throws TypeException
133
     */
134 283
    public function preload(Readable $schema): ReflectionInterface
135
    {
136
        $this->run($this->reflection, $schema);
137
138
        return $this->reflection;
139
    }
140
}
141