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\Compiler\Builder\Definition; |
11
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\RuleInterface; |
13
|
|
|
use Railt\Reflection\Contracts\Definition; |
14
|
|
|
use Railt\Reflection\Definition\ObjectDefinition; |
15
|
|
|
use Railt\SDL\Compiler\Ast\Definition\ObjectDefinitionNode; |
16
|
|
|
use Railt\SDL\Compiler\Builder\Builder; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ObjectBuilder |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
class ObjectBuilder extends Builder |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param RuleInterface|ObjectDefinitionNode $rule |
25
|
|
|
* @param Definition $parent |
26
|
|
|
* @return Definition |
27
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
28
|
|
|
*/ |
29
|
1 |
|
public function build(RuleInterface $rule, Definition $parent): Definition |
30
|
|
|
{ |
31
|
1 |
|
$object = new ObjectDefinition($parent->getDocument(), $rule->getTypeName()); |
|
|
|
|
32
|
|
|
|
33
|
1 |
|
$object->withOffset($rule->getOffset()); |
34
|
1 |
|
$object->withDescription($rule->getDescription()); |
|
|
|
|
35
|
|
|
|
36
|
1 |
|
foreach ($rule->getFields() as $ast) { |
|
|
|
|
37
|
|
|
$object->withField($this->dependent($ast, $object)); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
$this->when->resolving(function () use ($rule, $object) { |
41
|
1 |
|
foreach ($rule->getImplementations() as $interface) { |
|
|
|
|
42
|
1 |
|
$object->withInterface($interface->getTypeName()); |
|
|
|
|
43
|
|
|
} |
44
|
1 |
|
}); |
45
|
|
|
|
46
|
1 |
|
$this->when->runtime(function () use ($rule, $object) { |
47
|
1 |
|
foreach ($rule->getDirectives() as $ast) { |
|
|
|
|
48
|
|
|
$object->withDirective($this->dependent($ast, $object)); |
49
|
|
|
} |
50
|
1 |
|
}); |
51
|
|
|
|
52
|
1 |
|
return $object; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
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.