1 | <?php |
||
16 | class Registry implements RegistryInterface |
||
17 | { |
||
18 | /** |
||
19 | * The array with lazy values. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private $lazyArray; |
||
24 | |||
25 | /** |
||
26 | * The array with constructed values. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | private $constructedArray = []; |
||
31 | |||
32 | /** |
||
33 | * An array of service factories (the result of the call to 'getServices'), |
||
34 | * indexed by service provider. |
||
35 | * |
||
36 | * @var array An array<key, array<servicename, callable>> |
||
37 | */ |
||
38 | private $serviceFactories = []; |
||
39 | |||
40 | private $position = 0; |
||
41 | |||
42 | /** |
||
43 | * Initializes the registry from a list of service providers. |
||
44 | * This list of service providers can be passed as ServiceProvider instances, or simply class name, |
||
45 | * or an array of [class name, [constructor params]]. |
||
46 | * If a Puli $discovery object is passed, the registry is automatically populated with ServiceProviders from Puli. |
||
47 | * |
||
48 | * @param array $lazyArray The array with lazy values |
||
49 | * @param PuliDiscovery|null $puliDiscovery |
||
50 | */ |
||
51 | public function __construct(array $lazyArray = [], PuliDiscovery $puliDiscovery = null, TcmDiscovery $tcmDiscovery = null) |
||
65 | |||
66 | /** |
||
67 | * Discovers service provider class names using Puli. |
||
68 | * |
||
69 | * @param PuliDiscovery $discovery |
||
70 | * |
||
71 | * @return string[] Returns an array of service providers. |
||
72 | */ |
||
73 | private function discoverPuli(PuliDiscovery $discovery) /*: array*/ |
||
86 | |||
87 | /** |
||
88 | * Discovers service provider class names using thecodingmachine/discovery. |
||
89 | * |
||
90 | * @param TcmDiscovery $discovery |
||
91 | * |
||
92 | * @return string[] Returns an array of service providers. |
||
93 | */ |
||
94 | private function discoverTcm(TcmDiscovery $discovery) /*: array*/ |
||
98 | |||
99 | /** |
||
100 | * @param string|object $className The FQCN or the instance to put in the array |
||
101 | * @param array ...$params The parameters passed to the constructor. |
||
102 | * |
||
103 | * @return int The key in the array |
||
104 | * |
||
105 | * @throws ServiceProviderRegistryInvalidArgumentException |
||
106 | */ |
||
107 | public function push($className, ...$params) |
||
120 | |||
121 | /** |
||
122 | * Whether a offset exists. |
||
123 | * |
||
124 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
125 | * |
||
126 | * @param mixed $offset <p> |
||
127 | * An offset to check for. |
||
128 | * </p> |
||
129 | * |
||
130 | * @return bool true on success or false on failure. |
||
131 | * </p> |
||
132 | * <p> |
||
133 | * The return value will be casted to boolean if non-boolean was returned. |
||
134 | * |
||
135 | * @since 5.0.0 |
||
136 | */ |
||
137 | public function offsetExists($offset) |
||
141 | |||
142 | /** |
||
143 | * Offset to retrieve. |
||
144 | * |
||
145 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
146 | * |
||
147 | * @param mixed $offset <p> |
||
148 | * The offset to retrieve. |
||
149 | * </p> |
||
150 | * |
||
151 | * @return mixed Can return all value types. |
||
152 | * |
||
153 | * @since 5.0.0 |
||
154 | */ |
||
155 | public function offsetGet($offset) |
||
177 | |||
178 | /** |
||
179 | * Offset to set. |
||
180 | * |
||
181 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
182 | * |
||
183 | * @param mixed $offset <p> |
||
184 | * The offset to assign the value to. |
||
185 | * </p> |
||
186 | * @param mixed $value <p> |
||
187 | * The value to set. |
||
188 | * </p> |
||
189 | * |
||
190 | * @since 5.0.0 |
||
191 | */ |
||
192 | public function offsetSet($offset, $value) |
||
196 | /** |
||
197 | * Offset to unset. |
||
198 | * |
||
199 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
200 | * |
||
201 | * @param mixed $offset <p> |
||
202 | * The offset to unset. |
||
203 | * </p> |
||
204 | * |
||
205 | * @since 5.0.0 |
||
206 | */ |
||
207 | public function offsetUnset($offset) |
||
212 | |||
213 | /** |
||
214 | * Returns the result of the getServices call on service provider whose key in the registry is $offset. |
||
215 | * The result is cached in the registry so 2 successive calls will trigger `getServices` only once. |
||
216 | * |
||
217 | * @param string $offset Key of the service provider in the registry |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | public function getServices($offset) |
||
229 | |||
230 | /** |
||
231 | * @param string $offset Key of the service provider in the registry |
||
232 | * @param string $serviceName Name of the service to fetch |
||
233 | * @param ContainerInterface $container |
||
234 | * @param callable|null $previous |
||
235 | * |
||
236 | * @return mixed |
||
237 | */ |
||
238 | public function createService($offset, $serviceName, ContainerInterface $container, callable $previous = null) |
||
242 | |||
243 | /** |
||
244 | * Return the current element. |
||
245 | * |
||
246 | * @link http://php.net/manual/en/iterator.current.php |
||
247 | * |
||
248 | * @return mixed Can return any type. |
||
249 | * |
||
250 | * @since 5.0.0 |
||
251 | */ |
||
252 | public function current() |
||
256 | |||
257 | /** |
||
258 | * Move forward to next element. |
||
259 | * |
||
260 | * @link http://php.net/manual/en/iterator.next.php |
||
261 | * @since 5.0.0 |
||
262 | */ |
||
263 | public function next() |
||
267 | |||
268 | /** |
||
269 | * Return the key of the current element. |
||
270 | * |
||
271 | * @link http://php.net/manual/en/iterator.key.php |
||
272 | * |
||
273 | * @return mixed scalar on success, or null on failure. |
||
274 | * |
||
275 | * @since 5.0.0 |
||
276 | */ |
||
277 | public function key() |
||
281 | |||
282 | /** |
||
283 | * Checks if current position is valid. |
||
284 | * |
||
285 | * @link http://php.net/manual/en/iterator.valid.php |
||
286 | * |
||
287 | * @return bool The return value will be casted to boolean and then evaluated. |
||
288 | * Returns true on success or false on failure. |
||
289 | * |
||
290 | * @since 5.0.0 |
||
291 | */ |
||
292 | public function valid() |
||
296 | |||
297 | /** |
||
298 | * Rewind the Iterator to the first element. |
||
299 | * |
||
300 | * @link http://php.net/manual/en/iterator.rewind.php |
||
301 | * @since 5.0.0 |
||
302 | */ |
||
303 | public function rewind() |
||
307 | } |
||
308 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: