1 | <?php |
||
15 | class Registry implements RegistryInterface |
||
16 | { |
||
17 | /** |
||
18 | * The array with lazy values. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | private $lazyArray; |
||
23 | |||
24 | /** |
||
25 | * The array with constructed values. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | private $constructedArray = []; |
||
30 | |||
31 | /** |
||
32 | * An array of service factories (the result of the call to 'getServices'), |
||
33 | * indexed by service provider. |
||
34 | * |
||
35 | * @var array An array<key, array<servicename, callable>> |
||
36 | */ |
||
37 | private $serviceFactories = []; |
||
38 | |||
39 | private $position = 0; |
||
40 | |||
41 | /** |
||
42 | * Initializes the registry from a list of service providers. |
||
43 | * This list of service providers can be passed as ServiceProvider instances, or simply class name, |
||
44 | * or an array of [class name, [constructor params]]. |
||
45 | * If a Puli $discovery object is passed, the registry is automatically populated with ServiceProviders from Puli. |
||
46 | * |
||
47 | * @param array $lazyArray The array with lazy values |
||
48 | * @param Discovery|null $discovery |
||
49 | */ |
||
50 | public function __construct(array $lazyArray = [], Discovery $discovery = null) |
||
58 | |||
59 | /** |
||
60 | * Discovers service provider class names using Puli. |
||
61 | * |
||
62 | * @param Discovery $discovery |
||
63 | * |
||
64 | * @return string[] Returns an array of service providers. |
||
65 | */ |
||
66 | private function discover(Discovery $discovery) /*: array*/ |
||
79 | |||
80 | /** |
||
81 | * @param string|object $className The FQCN or the instance to put in the array |
||
82 | * @param array ...$params The parameters passed to the constructor. |
||
83 | * |
||
84 | * @return int The key in the array |
||
85 | * |
||
86 | * @throws ServiceProviderRegistryInvalidArgumentException |
||
87 | */ |
||
88 | public function push($className, ...$params) |
||
101 | |||
102 | /** |
||
103 | * Whether a offset exists. |
||
104 | * |
||
105 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
106 | * |
||
107 | * @param mixed $offset <p> |
||
108 | * An offset to check for. |
||
109 | * </p> |
||
110 | * |
||
111 | * @return bool true on success or false on failure. |
||
112 | * </p> |
||
113 | * <p> |
||
114 | * The return value will be casted to boolean if non-boolean was returned. |
||
115 | * |
||
116 | * @since 5.0.0 |
||
117 | */ |
||
118 | public function offsetExists($offset) |
||
122 | |||
123 | /** |
||
124 | * Offset to retrieve. |
||
125 | * |
||
126 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
127 | * |
||
128 | * @param mixed $offset <p> |
||
129 | * The offset to retrieve. |
||
130 | * </p> |
||
131 | * |
||
132 | * @return mixed Can return all value types. |
||
133 | * |
||
134 | * @since 5.0.0 |
||
135 | */ |
||
136 | public function offsetGet($offset) |
||
158 | |||
159 | /** |
||
160 | * Offset to set. |
||
161 | * |
||
162 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
163 | * |
||
164 | * @param mixed $offset <p> |
||
165 | * The offset to assign the value to. |
||
166 | * </p> |
||
167 | * @param mixed $value <p> |
||
168 | * The value to set. |
||
169 | * </p> |
||
170 | * |
||
171 | * @since 5.0.0 |
||
172 | */ |
||
173 | public function offsetSet($offset, $value) |
||
177 | /** |
||
178 | * Offset to unset. |
||
179 | * |
||
180 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
181 | * |
||
182 | * @param mixed $offset <p> |
||
183 | * The offset to unset. |
||
184 | * </p> |
||
185 | * |
||
186 | * @since 5.0.0 |
||
187 | */ |
||
188 | public function offsetUnset($offset) |
||
193 | |||
194 | /** |
||
195 | * Returns the result of the getServices call on service provider whose key in the registry is $offset. |
||
196 | * The result is cached in the registry so 2 successive calls will trigger `getServices` only once. |
||
197 | * |
||
198 | * @param string $offset Key of the service provider in the registry |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | public function getServices($offset) |
||
210 | |||
211 | /** |
||
212 | * @param string $offset Key of the service provider in the registry |
||
213 | * @param string $serviceName Name of the service to fetch |
||
214 | * @param ContainerInterface $container |
||
215 | * @param callable|null $previous |
||
216 | * |
||
217 | * @return mixed |
||
218 | */ |
||
219 | public function createService($offset, $serviceName, ContainerInterface $container, callable $previous = null) |
||
223 | |||
224 | /** |
||
225 | * Return the current element. |
||
226 | * |
||
227 | * @link http://php.net/manual/en/iterator.current.php |
||
228 | * |
||
229 | * @return mixed Can return any type. |
||
230 | * |
||
231 | * @since 5.0.0 |
||
232 | */ |
||
233 | public function current() |
||
237 | |||
238 | /** |
||
239 | * Move forward to next element. |
||
240 | * |
||
241 | * @link http://php.net/manual/en/iterator.next.php |
||
242 | * @since 5.0.0 |
||
243 | */ |
||
244 | public function next() |
||
248 | |||
249 | /** |
||
250 | * Return the key of the current element. |
||
251 | * |
||
252 | * @link http://php.net/manual/en/iterator.key.php |
||
253 | * |
||
254 | * @return mixed scalar on success, or null on failure. |
||
255 | * |
||
256 | * @since 5.0.0 |
||
257 | */ |
||
258 | public function key() |
||
262 | |||
263 | /** |
||
264 | * Checks if current position is valid. |
||
265 | * |
||
266 | * @link http://php.net/manual/en/iterator.valid.php |
||
267 | * |
||
268 | * @return bool The return value will be casted to boolean and then evaluated. |
||
269 | * Returns true on success or false on failure. |
||
270 | * |
||
271 | * @since 5.0.0 |
||
272 | */ |
||
273 | public function valid() |
||
277 | |||
278 | /** |
||
279 | * Rewind the Iterator to the first element. |
||
280 | * |
||
281 | * @link http://php.net/manual/en/iterator.rewind.php |
||
282 | * @since 5.0.0 |
||
283 | */ |
||
284 | public function rewind() |
||
288 | } |
||
289 |
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: