1 | <?php |
||
17 | class RefResolver |
||
18 | { |
||
19 | /** |
||
20 | * @var object |
||
21 | */ |
||
22 | private $definition; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $uri; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $directory; |
||
33 | |||
34 | /** |
||
35 | * @var YamlParser |
||
36 | */ |
||
37 | private $yamlParser; |
||
38 | |||
39 | /** |
||
40 | * @param object $definition |
||
41 | * @param string $uri |
||
42 | * @param YamlParser $yamlParser |
||
43 | */ |
||
44 | public function __construct($definition, $uri, YamlParser $yamlParser = null) |
||
55 | |||
56 | /** |
||
57 | * @return object |
||
58 | */ |
||
59 | public function getDefinition() |
||
63 | |||
64 | /** |
||
65 | * Resolve all references |
||
66 | * |
||
67 | * @return object |
||
68 | */ |
||
69 | public function resolve() |
||
75 | |||
76 | /** |
||
77 | * Revert to original state |
||
78 | * |
||
79 | * @return object |
||
80 | */ |
||
81 | public function unresolve() |
||
87 | |||
88 | /** |
||
89 | * @param object|array $current |
||
90 | * @param object $document |
||
91 | * @param string $uri |
||
92 | * |
||
93 | * @throws InvalidReferenceException |
||
94 | * @throws ResourceNotReadableException |
||
95 | */ |
||
96 | private function resolveRecursively(&$current, $document = null, $uri = null) |
||
131 | |||
132 | /** |
||
133 | * @param object|array $current |
||
134 | * @param object|array $parent |
||
135 | * |
||
136 | * @return void |
||
137 | */ |
||
138 | private function unresolveRecursively(&$current, &$parent = null) |
||
149 | |||
150 | /** |
||
151 | * @param string $path |
||
152 | * @param object $document |
||
153 | * @param string $uri |
||
154 | * |
||
155 | * @return mixed |
||
156 | * @throws InvalidReferenceException |
||
157 | */ |
||
158 | private function lookup($path, $document, $uri = null) |
||
172 | |||
173 | /** |
||
174 | * @param array $segments |
||
175 | * @param object $context |
||
176 | * |
||
177 | * @return mixed |
||
178 | */ |
||
179 | private function lookupRecursively(array $segments, $context) |
||
192 | |||
193 | /** |
||
194 | * @param string $uri |
||
195 | * |
||
196 | * @return object |
||
197 | * @throws ResourceNotReadableException |
||
198 | */ |
||
199 | private function loadExternal($uri) |
||
218 | |||
219 | |||
220 | /** |
||
221 | * @param array $uriSegs |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | private function normalizeUri(array $uriSegs) |
||
233 | |||
234 | /** |
||
235 | * @param string $uri |
||
236 | * |
||
237 | * @return array |
||
238 | */ |
||
239 | private function parseUri($uri) |
||
260 | } |
||
261 |
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.