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\Frontend\Builder\Definition; |
11
|
|
|
|
12
|
|
|
use Railt\Io\Readable; |
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
14
|
|
|
use Railt\SDL\Frontend\Ast\Definition\InterfaceDefinitionNode; |
15
|
|
|
use Railt\SDL\Frontend\Builder\DefinitionBuilder; |
16
|
|
|
use Railt\SDL\IR\Type; |
17
|
|
|
use Railt\SDL\IR\TypeDefinition; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class InterfaceBuilder |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
class InterfaceBuilder extends DefinitionBuilder |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param Readable $file |
26
|
|
|
* @param RuleInterface|InterfaceDefinitionNode $ast |
27
|
|
|
* @return \Generator|mixed |
28
|
|
|
*/ |
29
|
|
|
public function build(Readable $file, RuleInterface $ast) |
30
|
|
|
{ |
31
|
|
|
$interface = new TypeDefinition($ast->getFullName()); |
|
|
|
|
32
|
|
|
$interface->in($file, $ast->getOffset()); |
33
|
|
|
|
34
|
|
|
$interface->type = Type::INTERFACE; |
35
|
|
|
$interface->description = $ast->getDescription(); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$this->loadInterfaces($ast, $interface); |
|
|
|
|
38
|
|
|
|
39
|
|
|
yield from $this->loadFields($ast, $interface); |
|
|
|
|
40
|
|
|
|
41
|
|
|
return $interface; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param InterfaceDefinitionNode $ast |
47
|
|
|
* @param TypeDefinition $object |
48
|
|
|
* @return \Generator |
49
|
|
|
*/ |
50
|
|
|
protected function loadFields(InterfaceDefinitionNode $ast, TypeDefinition $object): \Generator |
51
|
|
|
{ |
52
|
|
|
$object->fields = []; |
53
|
|
|
|
54
|
|
|
foreach ($ast->getFieldNodes() as $field) { |
55
|
|
|
$object->fields[] = yield $field; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param InterfaceDefinitionNode $ast |
61
|
|
|
* @param TypeDefinition $object |
62
|
|
|
*/ |
63
|
|
|
protected function loadInterfaces(InterfaceDefinitionNode $ast, TypeDefinition $object): void |
64
|
|
|
{ |
65
|
|
|
$object->implements = []; |
66
|
|
|
|
67
|
|
|
foreach ($ast->getInterfaces() as $interface) { |
68
|
|
|
$object->implements[] = $interface; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.