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\Reflection; |
11
|
|
|
|
12
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
13
|
|
|
use Railt\Reflection\Contracts\Dictionary; |
14
|
|
|
use Railt\Reflection\Contracts\Document as DocumentInterface; |
15
|
|
|
use Railt\Reflection\Contracts\Reflection as ReflectionInterface; |
16
|
|
|
use Railt\Reflection\Dictionary\ProxyDictionary; |
17
|
|
|
use Railt\Reflection\Dictionary\SimpleDictionary; |
18
|
|
|
use Railt\Reflection\Exception\ReflectionException; |
19
|
|
|
use Railt\Reflection\Introspection\IntrospectionDocument; |
20
|
|
|
use Railt\Reflection\Stdlib\StdlibDocument; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Reflection |
24
|
|
|
*/ |
25
|
|
|
class Reflection extends ProxyDictionary implements ReflectionInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
public const LOAD_STDLIB = 0x02; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
public const LOAD_INTROSPECTION = 0x04; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array|DocumentInterface[] |
39
|
|
|
*/ |
40
|
|
|
protected $documents = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Reflection constructor. |
44
|
|
|
* @param Dictionary|null $parent |
45
|
|
|
* @param int $options |
46
|
|
|
* @throws Exception\TypeConflictException |
47
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
48
|
|
|
*/ |
49
|
9 |
|
public function __construct(Dictionary $parent = null, int $options = self::LOAD_INTROSPECTION | self::LOAD_STDLIB) |
50
|
|
|
{ |
51
|
9 |
|
parent::__construct($parent ?? new SimpleDictionary()); |
52
|
|
|
|
53
|
9 |
|
$this->boot($options); |
54
|
9 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param int $options |
58
|
|
|
* @return void |
59
|
|
|
* @throws Exception\TypeConflictException |
60
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
61
|
|
|
*/ |
62
|
9 |
|
private function boot(int $options): void |
63
|
|
|
{ |
64
|
9 |
|
if ($options & self::LOAD_STDLIB) { |
65
|
9 |
|
$this->addDocument(new StdlibDocument($this)); |
66
|
|
|
} |
67
|
|
|
|
68
|
9 |
|
if ($options & self::LOAD_INTROSPECTION) { |
69
|
9 |
|
$this->addDocument(new IntrospectionDocument($this)); |
70
|
|
|
} |
71
|
9 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param DocumentInterface $document |
75
|
|
|
*/ |
76
|
17 |
|
public function addDocument(DocumentInterface $document): void |
77
|
|
|
{ |
78
|
17 |
|
$this->documents[$document->getName()] = $document; |
79
|
17 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return iterable |
83
|
|
|
*/ |
84
|
4 |
|
public function getDocuments(): iterable |
85
|
|
|
{ |
86
|
4 |
|
return \array_values($this->documents); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param TypeDefinition $type |
91
|
|
|
* @return Dictionary |
92
|
|
|
*/ |
93
|
17 |
|
public function add(TypeDefinition $type): Dictionary |
94
|
|
|
{ |
95
|
17 |
|
$this->addDocument($type->getDocument()); |
96
|
|
|
|
97
|
17 |
|
return parent::add($type); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.