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 |
||
38 | class Container |
||
39 | { |
||
40 | // The Dependency Injection Container |
||
41 | private $coreContainer = null; |
||
42 | |||
43 | // The Dependency Injection Container |
||
44 | private $sentryContainer = null; |
||
45 | |||
46 | // The only instance of the Container (Singleton) |
||
47 | private static $xInstance = null; |
||
48 | |||
49 | public static function getInstance() |
||
57 | |||
58 | private function __construct() |
||
66 | |||
67 | /** |
||
68 | * Get the container provided by the integrated framework |
||
69 | * |
||
70 | * @return ContainerInterface |
||
71 | */ |
||
72 | public function getSentryContainer() |
||
76 | |||
77 | /** |
||
78 | * Set the container provided by the integrated framework |
||
79 | * |
||
80 | * @param ContainerInterface $container The container implementation |
||
81 | * |
||
82 | * @return void |
||
83 | */ |
||
84 | public function setSentryContainer(ContainerInterface $container) |
||
88 | |||
89 | /** |
||
90 | * Set the parameters and create the objects in the dependency injection container |
||
91 | * |
||
92 | * @param string $sTranslationDir The translation directory |
||
93 | * @param string $sTemplateDir The template directory |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | private function init($sTranslationDir, $sTemplateDir) |
||
200 | |||
201 | /** |
||
202 | * Get a class instance |
||
203 | * |
||
204 | * @return object The class instance |
||
205 | */ |
||
206 | public function get($sClass) |
||
214 | |||
215 | /** |
||
216 | * Set a DI closure |
||
217 | * |
||
218 | * @param string $sClass The full class name |
||
219 | * @param Closure $xClosure The closure |
||
220 | * |
||
221 | * @return void |
||
222 | */ |
||
223 | public function set($sClass, $xClosure) |
||
227 | |||
228 | /** |
||
229 | * Get the plugin manager |
||
230 | * |
||
231 | * @return Jaxon\Plugin\Manager |
||
232 | */ |
||
233 | public function getPluginManager() |
||
237 | |||
238 | /** |
||
239 | * Get the request handler |
||
240 | * |
||
241 | * @return Jaxon\Request\Handler |
||
242 | */ |
||
243 | public function getRequestHandler() |
||
247 | |||
248 | /** |
||
249 | * Get the request factory |
||
250 | * |
||
251 | * @return Jaxon\Factory\Request |
||
252 | */ |
||
253 | public function getRequestFactory() |
||
257 | |||
258 | /** |
||
259 | * Get the response manager |
||
260 | * |
||
261 | * @return Jaxon\Response\Manager |
||
262 | */ |
||
263 | public function getResponseManager() |
||
267 | |||
268 | /** |
||
269 | * Get the code generator |
||
270 | * |
||
271 | * @return Jaxon\Code\Generator |
||
272 | */ |
||
273 | public function getCodeGenerator() |
||
277 | |||
278 | /** |
||
279 | * Get the config manager |
||
280 | * |
||
281 | * @return Jaxon\Config\Config |
||
282 | */ |
||
283 | public function getConfig() |
||
287 | |||
288 | /** |
||
289 | * Create a new the config manager |
||
290 | * |
||
291 | * @return Jaxon\Config\Config The config manager |
||
292 | */ |
||
293 | public function newConfig() |
||
297 | |||
298 | /** |
||
299 | * Get the dialog wrapper |
||
300 | * |
||
301 | * @return Jaxon\Dialog\Config |
||
302 | */ |
||
303 | public function getDialog() |
||
307 | |||
308 | /** |
||
309 | * Get the minifier |
||
310 | * |
||
311 | * @return Jaxon\Utils\Template\Minifier |
||
312 | */ |
||
313 | public function getMinifier() |
||
317 | |||
318 | /** |
||
319 | * Get the translator |
||
320 | * |
||
321 | * @return Jaxon\Utils\Translation\Translator |
||
322 | */ |
||
323 | public function getTranslator() |
||
327 | |||
328 | /** |
||
329 | * Get the template engine |
||
330 | * |
||
331 | * @return Jaxon\Utils\Template\Template |
||
332 | */ |
||
333 | public function getTemplate() |
||
337 | |||
338 | /** |
||
339 | * Get the validator |
||
340 | * |
||
341 | * @return Jaxon\Utils\Validation\Validator |
||
342 | */ |
||
343 | public function getValidator() |
||
347 | |||
348 | /** |
||
349 | * Get the paginator |
||
350 | * |
||
351 | * @return Jaxon\Utils\Pagination\Paginator |
||
352 | */ |
||
353 | public function getPaginator() |
||
357 | |||
358 | /** |
||
359 | * Set the pagination renderer |
||
360 | * |
||
361 | * @param Jaxon\Utils\Pagination\Renderer $xRenderer The pagination renderer |
||
362 | * |
||
363 | * @return void |
||
364 | */ |
||
365 | public function setPaginationRenderer(PaginationRenderer $xRenderer) |
||
369 | |||
370 | /** |
||
371 | * Get the event dispatcher |
||
372 | * |
||
373 | * @return Lemon\Event\EventDispatcher |
||
374 | */ |
||
375 | public function getEventDispatcher() |
||
379 | |||
380 | /** |
||
381 | * Get the global Response object |
||
382 | * |
||
383 | * @return Jaxon\Response\Response |
||
384 | */ |
||
385 | public function getResponse() |
||
389 | |||
390 | /** |
||
391 | * Create a new Jaxon response object |
||
392 | * |
||
393 | * @return Jaxon\Response\Response |
||
394 | */ |
||
395 | public function newResponse() |
||
399 | |||
400 | /** |
||
401 | * Get the main Jaxon object |
||
402 | * |
||
403 | * @return Jaxon\Jaxon |
||
404 | */ |
||
405 | public function getJaxon() |
||
409 | |||
410 | /** |
||
411 | * Get the Jaxon library version number |
||
412 | * |
||
413 | * @return string |
||
414 | */ |
||
415 | public function getVersion() |
||
419 | |||
420 | /** |
||
421 | * Get the Sentry instance |
||
422 | * |
||
423 | * @return Jaxon\Sentry\Sentry |
||
424 | */ |
||
425 | public function getSentry() |
||
429 | |||
430 | /** |
||
431 | * Set the Sentry instance |
||
432 | * |
||
433 | * @param Jaxon\Sentry\Sentry $xSentry The Sentry instance |
||
434 | * |
||
435 | * @return void |
||
436 | */ |
||
437 | public function setSentry($xSentry) |
||
441 | |||
442 | /** |
||
443 | * Get the Armada instance |
||
444 | * |
||
445 | * @return Jaxon\Armada\Armada |
||
446 | */ |
||
447 | public function getArmada() |
||
451 | |||
452 | /** |
||
453 | * Set the Armada instance |
||
454 | * |
||
455 | * @param Jaxon\Armada\Armada $xArmada The Armada instance |
||
456 | * |
||
457 | * @return void |
||
458 | */ |
||
459 | public function setArmada($xArmada) |
||
463 | |||
464 | /** |
||
465 | * Set the view renderers data |
||
466 | * |
||
467 | * @param array $aRenderers Array of renderer names with namespace as key |
||
468 | * |
||
469 | * @return void |
||
470 | */ |
||
471 | public function initViewRenderers($aRenderers) |
||
475 | |||
476 | /** |
||
477 | * Set the view namespaces data |
||
478 | * |
||
479 | * @param array $aNamespaces Array of namespaces with renderer name as key |
||
480 | * |
||
481 | * @return void |
||
482 | */ |
||
483 | public function initViewNamespaces($aNamespaces, $sDefaultNamespace) |
||
488 | |||
489 | /** |
||
490 | * Add a view renderer |
||
491 | * |
||
492 | * @param string $sId The unique identifier of the view renderer |
||
493 | * @param Closure $xClosure A closure to create the view instance |
||
494 | * |
||
495 | * @return void |
||
496 | */ |
||
497 | public function addViewRenderer($sId, $xClosure) |
||
518 | |||
519 | /** |
||
520 | * Get the view renderer |
||
521 | * |
||
522 | * @param string $sId The unique identifier of the view renderer |
||
523 | * |
||
524 | * @return Jaxon\Sentry\Interfaces\View |
||
525 | */ |
||
526 | public function getViewRenderer($sId = '') |
||
536 | |||
537 | /** |
||
538 | * Get the session object |
||
539 | * |
||
540 | * @return Jaxon\Sentry\Interfaces\Session |
||
541 | */ |
||
542 | public function getSessionManager() |
||
546 | |||
547 | /** |
||
548 | * Set the session |
||
549 | * |
||
550 | * @param Closure $xClosure A closure to create the session instance |
||
551 | * |
||
552 | * @return void |
||
553 | */ |
||
554 | public function setSessionManager($xClosure) |
||
558 | } |
||
559 |