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 |
||
| 58 | class Container |
||
| 59 | { |
||
| 60 | /** |
||
| 61 | * The Dependency Injection Container |
||
| 62 | * |
||
| 63 | * @var \Pimple\Container |
||
| 64 | */ |
||
| 65 | private $libContainer = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The Dependency Injection Container |
||
| 69 | * |
||
| 70 | * @var \Jaxon\Contracts\Container |
||
| 71 | */ |
||
| 72 | private $appContainer = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The class constructor |
||
| 76 | */ |
||
| 77 | public function __construct() |
||
| 78 | { |
||
| 79 | $this->libContainer = new \Pimple\Container(); |
||
| 80 | $this->libContainer[Container::class] = $this; |
||
| 81 | |||
| 82 | $sTranslationDir = realpath(__DIR__ . '/../../../translations'); |
||
| 83 | $sTemplateDir = realpath(__DIR__ . '/../../../templates'); |
||
| 84 | $this->init($sTranslationDir, $sTemplateDir); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get the container provided by the integrated framework |
||
| 89 | * |
||
| 90 | * @return ContainerContract |
||
| 91 | */ |
||
| 92 | public function getAppContainer() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Set the container provided by the integrated framework |
||
| 99 | * |
||
| 100 | * @param ContainerContract $container The container implementation |
||
| 101 | * |
||
| 102 | * @return void |
||
| 103 | */ |
||
| 104 | public function setAppContainer(ContainerContract $container) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Set the parameters and create the objects in the dependency injection container |
||
| 111 | * |
||
| 112 | * @param string $sTranslationDir The translation directory |
||
| 113 | * @param string $sTemplateDir The template directory |
||
| 114 | * |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | private function init($sTranslationDir, $sTemplateDir) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get a class instance |
||
| 268 | * |
||
| 269 | * @return object The class instance |
||
| 270 | */ |
||
| 271 | public function get($sClass) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Set a DI closure |
||
| 282 | * |
||
| 283 | * @param string $sClass The full class name |
||
| 284 | * @param Closure $xClosure The closure |
||
| 285 | * |
||
| 286 | * @return void |
||
| 287 | */ |
||
| 288 | public function set($sClass, Closure $xClosure) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Set an alias |
||
| 297 | * |
||
| 298 | * @param string $sClass The class name |
||
| 299 | * @param string $sAlias The alias name |
||
| 300 | * |
||
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | public function alias($sClass, $sAlias) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Set an alias |
||
| 312 | * |
||
| 313 | * @param string|ReflectionClass $xClass The class name or the reflection class |
||
| 314 | * |
||
| 315 | * @return null|object |
||
| 316 | */ |
||
| 317 | public function make($xClass) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get the plugin manager |
||
| 345 | * |
||
| 346 | * @return PluginManager |
||
| 347 | */ |
||
| 348 | public function getPluginManager() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get the request handler |
||
| 355 | * |
||
| 356 | * @return RequestHandler |
||
| 357 | */ |
||
| 358 | public function getRequestHandler() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get the request factory |
||
| 365 | * |
||
| 366 | * @return RequestFactory |
||
| 367 | */ |
||
| 368 | public function getRequestFactory() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get the parameter factory |
||
| 375 | * |
||
| 376 | * @return ParameterFactory |
||
| 377 | */ |
||
| 378 | public function getParameterFactory() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get the response manager |
||
| 385 | * |
||
| 386 | * @return ResponseManager |
||
| 387 | */ |
||
| 388 | public function getResponseManager() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Get the code generator |
||
| 395 | * |
||
| 396 | * @return CodeGenerator |
||
| 397 | */ |
||
| 398 | public function getCodeGenerator() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get the callable registry |
||
| 405 | * |
||
| 406 | * @return CallableRegistry |
||
| 407 | */ |
||
| 408 | public function getCallableRegistry() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get the config manager |
||
| 415 | * |
||
| 416 | * @return Config |
||
| 417 | */ |
||
| 418 | public function getConfig() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Create a new the config manager |
||
| 425 | * |
||
| 426 | * @param array $aOptions The options array |
||
| 427 | * @param string $sKeys The keys of the options in the array |
||
| 428 | * |
||
| 429 | * @return Config The config manager |
||
| 430 | */ |
||
| 431 | public function newConfig(array $aOptions = [], $sKeys = '') |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get the dialog wrapper |
||
| 438 | * |
||
| 439 | * @return Dialog |
||
| 440 | */ |
||
| 441 | public function getDialog() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Get the minifier |
||
| 448 | * |
||
| 449 | * @return Minifier |
||
| 450 | */ |
||
| 451 | public function getMinifier() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Get the translator |
||
| 458 | * |
||
| 459 | * @return Translator |
||
| 460 | */ |
||
| 461 | public function getTranslator() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get the template engine |
||
| 468 | * |
||
| 469 | * @return TemplateEngine |
||
| 470 | */ |
||
| 471 | public function getTemplateEngine() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Get the validator |
||
| 478 | * |
||
| 479 | * @return Validator |
||
| 480 | */ |
||
| 481 | public function getValidator() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Get the paginator |
||
| 488 | * |
||
| 489 | * @return Paginator |
||
| 490 | */ |
||
| 491 | public function getPaginator() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the event dispatcher |
||
| 498 | * |
||
| 499 | * @return EventDispatcher |
||
| 500 | */ |
||
| 501 | public function getEventDispatcher() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get the global Response object |
||
| 508 | * |
||
| 509 | * @return Response |
||
| 510 | */ |
||
| 511 | public function getResponse() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Create a new Jaxon response object |
||
| 518 | * |
||
| 519 | * @return Response |
||
| 520 | */ |
||
| 521 | public function newResponse() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get the App instance |
||
| 528 | * |
||
| 529 | * @return App |
||
| 530 | */ |
||
| 531 | public function getApp() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Get the App bootstrap |
||
| 538 | * |
||
| 539 | * @return Bootstrap |
||
| 540 | */ |
||
| 541 | public function getBootstrap() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Get the view manager |
||
| 548 | * |
||
| 549 | * @return ViewManager |
||
| 550 | */ |
||
| 551 | public function getViewManager() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Get the view facade |
||
| 558 | * |
||
| 559 | * @return ViewRenderer |
||
| 560 | */ |
||
| 561 | public function getViewRenderer() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Get the session manager |
||
| 568 | * |
||
| 569 | * @return SessionContract |
||
| 570 | */ |
||
| 571 | public function getSessionManager() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Set the session manager |
||
| 578 | * |
||
| 579 | * @param Closure $xClosure A closure to create the session manager instance |
||
| 580 | * |
||
| 581 | * @return void |
||
| 582 | */ |
||
| 583 | public function setSessionManager(Closure $xClosure) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Set the callable class request factory |
||
| 590 | * |
||
| 591 | * @param string $sClassName The callable class name |
||
| 592 | * @param CallableObject $xCallableObject The corresponding callable object |
||
| 593 | * |
||
| 594 | * @return void |
||
| 595 | */ |
||
| 596 | public function setCallableClassRequestFactory($sClassName, CallableObject $xCallableObject) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Get the callable class request factory |
||
| 605 | * |
||
| 606 | * @param string $sClassName The callable class name |
||
| 607 | * |
||
| 608 | * @return CallableClassRequestFactory |
||
| 609 | */ |
||
| 610 | public function getCallableClassRequestFactory($sClassName) |
||
| 614 | } |
||
| 615 |