|
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\SDL\Frontend\Context\ContextInterface; |
|
13
|
|
|
use Railt\SDL\IR\Type\TypeNameInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Definition |
|
17
|
|
|
*/ |
|
18
|
|
|
class Definition implements DefinitionInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var TypeNameInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $name; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ContextInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $context; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $arguments = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Definition constructor. |
|
37
|
|
|
* @param ContextInterface $context |
|
38
|
|
|
* @param TypeNameInterface $name |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(ContextInterface $context, TypeNameInterface $name) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->context = $context; |
|
43
|
|
|
$this->name = $name->in($context->getName()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return iterable|DefinitionArgumentInterface[] |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getArguments(): iterable |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->arguments; |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $name |
|
56
|
|
|
* @param TypeNameInterface $hint |
|
57
|
|
|
* @return DefinitionArgumentInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
public function addArgument(string $name, TypeNameInterface $hint): DefinitionArgumentInterface |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->arguments[$name] = new Argument($name, $hint); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $name |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public function hasArgument(string $name): bool |
|
69
|
|
|
{ |
|
70
|
|
|
return isset($this->arguments[$name]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $name |
|
75
|
|
|
* @return DefinitionArgumentInterface |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getArgument(string $name): DefinitionArgumentInterface |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->arguments[$name]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return TypeNameInterface |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getName(): TypeNameInterface |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->name; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return ContextInterface |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getContext(): ContextInterface |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->context; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
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.