|
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; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Io\Readable; |
|
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
14
|
|
|
use Railt\SDL\Exception\InternalException; |
|
15
|
|
|
use Railt\SDL\IR\Definition; |
|
16
|
|
|
use Railt\SDL\IR\Type; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class DocumentBuilder |
|
20
|
|
|
*/ |
|
21
|
|
|
class DocumentBuilder extends DefinitionBuilder |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @param Readable $file |
|
25
|
|
|
* @param RuleInterface $ast |
|
26
|
|
|
* @return \Generator|mixed|Definition|\Railt\SDL\IR\TypeDefinition|\Railt\SDL\IR\TypeInvocation |
|
27
|
|
|
*/ |
|
28
|
|
|
public function build(Readable $file, RuleInterface $ast) |
|
29
|
|
|
{ |
|
30
|
|
|
$document = new Definition(); |
|
31
|
|
|
$document->in($file); |
|
32
|
|
|
|
|
33
|
|
|
$document->type = Type::DOCUMENT; |
|
34
|
|
|
|
|
35
|
|
|
$document->definitions = |
|
36
|
|
|
$document->extensions = |
|
37
|
|
|
$document->directives = []; |
|
38
|
|
|
|
|
39
|
|
|
foreach ($ast->getChildren() as $child) { |
|
40
|
|
|
if ($result = yield $child) { |
|
41
|
|
|
$this->append($document, $result); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $document; |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Definition $document |
|
50
|
|
|
* @param Definition $value |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
private function append(Definition $document, Definition $value): void |
|
54
|
|
|
{ |
|
55
|
|
|
switch (true) { |
|
56
|
|
|
case $this->isTypeDefinition($value): |
|
57
|
|
|
$document->definitions[] = $value; |
|
58
|
|
|
break; |
|
59
|
|
|
|
|
60
|
|
|
case $this->isTypeExtension($value): |
|
61
|
|
|
$document->extensions[] = $value; |
|
62
|
|
|
break; |
|
63
|
|
|
|
|
64
|
|
|
case $this->isDirective($value): |
|
65
|
|
|
$document->directives[] = $value; |
|
66
|
|
|
break; |
|
67
|
|
|
|
|
68
|
|
|
default: |
|
69
|
|
|
throw new InternalException( |
|
70
|
|
|
'Unrecognized struct ' . $value->toJson(\JSON_PRETTY_PRINT) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param Definition $definition |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
private function isTypeDefinition(Definition $definition): bool |
|
80
|
|
|
{ |
|
81
|
|
|
return $definition->type && $definition->type->isIndependent(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param Definition $definition |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
private function isTypeExtension(Definition $definition): bool |
|
89
|
|
|
{ |
|
90
|
|
|
return $definition->type && $definition->type->isExtension(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param Definition $definition |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
private function isDirective(Definition $definition): bool |
|
98
|
|
|
{ |
|
99
|
|
|
return $definition->type === null; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.