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\Definition; |
11
|
|
|
|
12
|
|
|
use Railt\SDL\Frontend\Context\ContextInterface; |
13
|
|
|
use Railt\SDL\IR\SymbolTable\PrimitiveInterface; |
14
|
|
|
use Railt\SDL\IR\Type\TypeNameInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class InvocationPrimitive |
18
|
|
|
*/ |
19
|
|
|
class Invocation implements InvocationInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var TypeNameInterface |
23
|
|
|
*/ |
24
|
|
|
private $name; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $arguments = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ContextInterface |
33
|
|
|
*/ |
34
|
|
|
private $from; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* InvocationPrimitive constructor. |
38
|
|
|
* @param TypeNameInterface $name |
39
|
|
|
* @param ContextInterface $from |
40
|
|
|
*/ |
41
|
|
|
public function __construct(TypeNameInterface $name, ContextInterface $from) |
42
|
|
|
{ |
43
|
|
|
$this->name = $name; |
44
|
|
|
$this->from = $from; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return ContextInterface |
49
|
|
|
*/ |
50
|
|
|
public function getContext(): ContextInterface |
51
|
|
|
{ |
52
|
|
|
return $this->from; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return iterable|InvocationInterface[] |
57
|
|
|
*/ |
58
|
|
|
public function getArguments(): iterable |
59
|
|
|
{ |
60
|
|
|
return $this->arguments; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $name |
65
|
|
|
* @param Invocation|PrimitiveInterface $value |
66
|
|
|
* @return InvocationInterface |
67
|
|
|
*/ |
68
|
|
|
public function addArgument(string $name, $value): InvocationInterface |
69
|
|
|
{ |
70
|
|
|
$this->arguments[$name] = $value; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return TypeNameInterface |
77
|
|
|
*/ |
78
|
|
|
public function getName(): TypeNameInterface |
79
|
|
|
{ |
80
|
|
|
return $this->name; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function __toString(): string |
87
|
|
|
{ |
88
|
|
|
if ($this->hasArguments()) { |
89
|
|
|
$arguments = []; |
90
|
|
|
|
91
|
|
|
foreach ($this->arguments as $name => $value) { |
92
|
|
|
$arguments[] = $name . ': ' . $value; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return \sprintf('%s<%s>', $this->name->getFullyQualifiedName(), \implode(', ', $arguments)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->name->getFullyQualifiedName(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function hasArguments(): bool |
105
|
|
|
{ |
106
|
|
|
return \count($this->arguments) > 0; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.