Complex classes like Container often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class Container implements ContainerInterface, ArrayAccess |
||
48 | { |
||
49 | /** |
||
50 | * @var self Store for last container instance |
||
51 | */ |
||
52 | protected static $instance; |
||
53 | |||
54 | /** |
||
55 | * @var array Store for services. |
||
56 | */ |
||
57 | protected $services = []; |
||
58 | |||
59 | /** |
||
60 | * @var array Store for shared services. |
||
61 | */ |
||
62 | protected $shared = []; |
||
63 | |||
64 | /** |
||
65 | * Returns last container instance. |
||
66 | * |
||
67 | * @return Container Last container instance. |
||
68 | */ |
||
69 | public static function instance() |
||
73 | |||
74 | /** |
||
75 | * Initialisation |
||
76 | */ |
||
77 | public function __construct() |
||
81 | |||
82 | /** |
||
83 | * Merge two containers into one. |
||
84 | * |
||
85 | * @param ServiceProviderInterface $provider Service provider. |
||
86 | * |
||
87 | * @return self |
||
88 | */ |
||
89 | public function register(ServiceProviderInterface $provider) |
||
94 | |||
95 | /** |
||
96 | * Registers a service in the container. |
||
97 | * |
||
98 | * @param string $id Service id. |
||
99 | * @param mixed $definition Service definition. |
||
100 | * @param bool $shared Shared or not. |
||
101 | * |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function set($id, $definition, $shared = false) |
||
112 | |||
113 | /** |
||
114 | * Normalize service name. |
||
115 | * |
||
116 | * @param string $id Service id. |
||
117 | * |
||
118 | * @return string Normalized name. |
||
119 | */ |
||
120 | protected function normalize($id) |
||
124 | |||
125 | /** |
||
126 | * Registers a shared service in the container. |
||
127 | * |
||
128 | * @param string $id Service id. |
||
129 | * @param mixed $definition Service definition. |
||
130 | * |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function shared($id, $definition) |
||
137 | |||
138 | /** |
||
139 | * Check whether the service is shared or not. |
||
140 | * |
||
141 | * @param string $id Service id. |
||
142 | * |
||
143 | * @return bool True if shared, false - not. |
||
144 | */ |
||
145 | public function isShared($id) |
||
149 | |||
150 | /** |
||
151 | * Sets if the service is shared or not. |
||
152 | * |
||
153 | * @param string $id Service id. |
||
154 | * @param bool $shared Shared or not. |
||
155 | * |
||
156 | * @return self |
||
157 | * @throws NotFoundException No entry was found for this identifier. |
||
158 | */ |
||
159 | public function setShared($id, $shared = true) |
||
168 | |||
169 | /** |
||
170 | * Finds an entry of the container by its identifier and returns it. |
||
171 | * |
||
172 | * @param string $id Identifier of the entry to look for. |
||
173 | * @param array $parameters Parameter for service construct. |
||
174 | * |
||
175 | * @throws NotFoundException No entry was found for this identifier. |
||
176 | * @throws ContainerException Error while retrieving the entry. |
||
177 | * |
||
178 | * @return mixed Entry. |
||
179 | */ |
||
180 | public function get($id, array $parameters = []) |
||
201 | |||
202 | /** |
||
203 | * Resolves the service. |
||
204 | * |
||
205 | * @param mixed $definition The definition of service. |
||
206 | * @param array $parameters Parameters for service construct. |
||
207 | * |
||
208 | * @return mixed Entry. |
||
209 | * @throws ContainerException Error while retrieving the entry. |
||
210 | * @throws NotFoundException No entry was found for this identifier. |
||
211 | */ |
||
212 | protected function resolveService($definition, array $parameters = []) |
||
240 | |||
241 | /** |
||
242 | * Resolve callback dependencies and executes him. |
||
243 | * |
||
244 | * @param callable $callback |
||
245 | * @param array $parameters |
||
246 | * |
||
247 | * @return mixed |
||
248 | * @throws ContainerException Error while retrieving the entry. |
||
249 | * @throws NotFoundException No entry was found for this identifier. |
||
250 | */ |
||
251 | public function call(callable $callback, array $parameters = []) |
||
270 | |||
271 | /** |
||
272 | * Resolve parameters of service. |
||
273 | * |
||
274 | * @param array $dependencies |
||
275 | * @param array $parameters |
||
276 | * |
||
277 | * @return array Resolved parameters. |
||
278 | * @throws ContainerException Error while retrieving the entry. |
||
279 | * @throws NotFoundException No entry was found for this identifier. |
||
280 | */ |
||
281 | protected function resolveOptions(array $dependencies, array $parameters) |
||
310 | |||
311 | /** |
||
312 | * Removes a service in the services container. |
||
313 | * |
||
314 | * @param string $id Service id. |
||
315 | * |
||
316 | * @return void |
||
317 | */ |
||
318 | public function remove($id) |
||
322 | |||
323 | /** |
||
324 | * Returns true if the container can return an entry for the given identifier. |
||
325 | * Returns false otherwise. |
||
326 | * |
||
327 | * @param string $id Identifier of the entry to look for. |
||
328 | * |
||
329 | * @return bool |
||
330 | */ |
||
331 | public function has($id) |
||
335 | |||
336 | /** |
||
337 | * Allows to register a shared service using the array syntax. |
||
338 | * |
||
339 | * @param string $id Service id. |
||
340 | * @param mixed $definition Service definition. |
||
341 | * |
||
342 | * @return self |
||
343 | */ |
||
344 | public function offsetSet($id, $definition) |
||
348 | |||
349 | /** |
||
350 | * Finds an entry of the container by its identifier and returns it. |
||
351 | * |
||
352 | * @param string $id Identifier of the entry to look for. |
||
353 | * |
||
354 | * @throws NotFoundException No entry was found for this identifier. |
||
355 | * @throws ContainerException Error while retrieving the entry. |
||
356 | * |
||
357 | * @return mixed Entry. |
||
358 | */ |
||
359 | public function offsetGet($id) |
||
363 | |||
364 | /** |
||
365 | * Removes a service from the services container using the array syntax. |
||
366 | * |
||
367 | * @param string $id Service id. |
||
368 | * |
||
369 | * @return void |
||
370 | */ |
||
371 | public function offsetUnset($id) |
||
375 | |||
376 | /** |
||
377 | * Returns true if the container can return an entry for the given identifier. |
||
378 | * Returns false otherwise. |
||
379 | * |
||
380 | * @param string $id Identifier of the entry to look for. |
||
381 | * |
||
382 | * @return bool |
||
383 | */ |
||
384 | public function offsetExists($id) |
||
388 | |||
389 | /** |
||
390 | * Allows to register a shared service using the array syntax. |
||
391 | * |
||
392 | * @param string $id Service id. |
||
393 | * @param mixed $definition Service definition. |
||
394 | * |
||
395 | * @return self |
||
396 | */ |
||
397 | public function __set($id, $definition) |
||
401 | |||
402 | /** |
||
403 | * Finds an entry of the container by its identifier and returns it. |
||
404 | * |
||
405 | * @param string $id Identifier of the entry to look for. |
||
406 | * |
||
407 | * @throws NotFoundException No entry was found for this identifier. |
||
408 | * @throws ContainerException Error while retrieving the entry. |
||
409 | * |
||
410 | * @return mixed Entry. |
||
411 | */ |
||
412 | public function __get($id) |
||
416 | |||
417 | /** |
||
418 | * Removes a service from the services container using the array syntax. |
||
419 | * |
||
420 | * @param string $id Service id. |
||
421 | * |
||
422 | * @return void |
||
423 | */ |
||
424 | public function __unset($id) |
||
428 | |||
429 | /** |
||
430 | * Returns true if the container can return an entry for the given identifier. |
||
431 | * Returns false otherwise. |
||
432 | * |
||
433 | * @param string $id Identifier of the entry to look for. |
||
434 | * |
||
435 | * @return bool |
||
436 | */ |
||
437 | public function __isset($id) |
||
441 | } |