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 |
||
| 39 | class Container |
||
| 40 | { |
||
| 41 | // The Dependency Injection Container |
||
| 42 | private $coreContainer = null; |
||
| 43 | |||
| 44 | // The Dependency Injection Container |
||
| 45 | private $sentryContainer = null; |
||
| 46 | |||
| 47 | // The only instance of the Container (Singleton) |
||
| 48 | private static $xInstance = null; |
||
| 49 | |||
| 50 | public static function getInstance() |
||
| 58 | |||
| 59 | private function __construct() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get the container provided by the integrated framework |
||
| 70 | * |
||
| 71 | * @return ContainerInterface |
||
| 72 | */ |
||
| 73 | public function getSentryContainer() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Set the container provided by the integrated framework |
||
| 80 | * |
||
| 81 | * @param ContainerInterface $container The container implementation |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | public function setSentryContainer(ContainerInterface $container) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Set the parameters and create the objects in the dependency injection container |
||
| 92 | * |
||
| 93 | * @param string $sTranslationDir The translation directory |
||
| 94 | * @param string $sTemplateDir The template directory |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | private function init($sTranslationDir, $sTemplateDir) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get a class instance |
||
| 208 | * |
||
| 209 | * @return object The class instance |
||
| 210 | */ |
||
| 211 | public function get($sClass) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set a DI closure |
||
| 222 | * |
||
| 223 | * @param string $sClass The full class name |
||
| 224 | * @param Closure $xClosure The closure |
||
| 225 | * |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | public function set($sClass, $xClosure) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get the plugin manager |
||
| 235 | * |
||
| 236 | * @return Jaxon\Plugin\Manager |
||
| 237 | */ |
||
| 238 | public function getPluginManager() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get the request handler |
||
| 245 | * |
||
| 246 | * @return Jaxon\Request\Handler |
||
| 247 | */ |
||
| 248 | public function getRequestHandler() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the request factory |
||
| 255 | * |
||
| 256 | * @return Jaxon\Factory\Request |
||
| 257 | */ |
||
| 258 | public function getRequestFactory() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get the parameter factory |
||
| 265 | * |
||
| 266 | * @return Jaxon\Factory\Parameter |
||
| 267 | */ |
||
| 268 | public function getParameterFactory() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get the response manager |
||
| 275 | * |
||
| 276 | * @return Jaxon\Response\Manager |
||
| 277 | */ |
||
| 278 | public function getResponseManager() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get the code generator |
||
| 285 | * |
||
| 286 | * @return Jaxon\Code\Generator |
||
| 287 | */ |
||
| 288 | public function getCodeGenerator() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get the config manager |
||
| 295 | * |
||
| 296 | * @return Jaxon\Config\Config |
||
| 297 | */ |
||
| 298 | public function getConfig() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Create a new the config manager |
||
| 305 | * |
||
| 306 | * @return Jaxon\Config\Config The config manager |
||
| 307 | */ |
||
| 308 | public function newConfig() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get the dialog wrapper |
||
| 315 | * |
||
| 316 | * @return Jaxon\Dialog\Config |
||
| 317 | */ |
||
| 318 | public function getDialog() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get the minifier |
||
| 325 | * |
||
| 326 | * @return Jaxon\Utils\Template\Minifier |
||
| 327 | */ |
||
| 328 | public function getMinifier() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Get the translator |
||
| 335 | * |
||
| 336 | * @return Jaxon\Utils\Translation\Translator |
||
| 337 | */ |
||
| 338 | public function getTranslator() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get the template engine |
||
| 345 | * |
||
| 346 | * @return Jaxon\Utils\Template\Template |
||
| 347 | */ |
||
| 348 | public function getTemplate() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get the validator |
||
| 355 | * |
||
| 356 | * @return Jaxon\Utils\Validation\Validator |
||
| 357 | */ |
||
| 358 | public function getValidator() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get the paginator |
||
| 365 | * |
||
| 366 | * @return Jaxon\Utils\Pagination\Paginator |
||
| 367 | */ |
||
| 368 | public function getPaginator() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Set the pagination renderer |
||
| 375 | * |
||
| 376 | * @param Jaxon\Utils\Pagination\Renderer $xRenderer The pagination renderer |
||
| 377 | * |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | public function setPaginationRenderer(PaginationRenderer $xRenderer) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get the event dispatcher |
||
| 387 | * |
||
| 388 | * @return Lemon\Event\EventDispatcher |
||
| 389 | */ |
||
| 390 | public function getEventDispatcher() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get the global Response object |
||
| 397 | * |
||
| 398 | * @return Jaxon\Response\Response |
||
| 399 | */ |
||
| 400 | public function getResponse() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Create a new Jaxon response object |
||
| 407 | * |
||
| 408 | * @return Jaxon\Response\Response |
||
| 409 | */ |
||
| 410 | public function newResponse() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get the main Jaxon object |
||
| 417 | * |
||
| 418 | * @return Jaxon\Jaxon |
||
| 419 | */ |
||
| 420 | public function getJaxon() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Get the Jaxon library version number |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | public function getVersion() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Get the Sentry instance |
||
| 437 | * |
||
| 438 | * @return Jaxon\Sentry\Sentry |
||
| 439 | */ |
||
| 440 | public function getSentry() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Set the Sentry instance |
||
| 447 | * |
||
| 448 | * @param Jaxon\Sentry\Sentry $xSentry The Sentry instance |
||
| 449 | * |
||
| 450 | * @return void |
||
| 451 | */ |
||
| 452 | public function setSentry($xSentry) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Get the Armada instance |
||
| 459 | * |
||
| 460 | * @return Jaxon\Armada\Armada |
||
| 461 | */ |
||
| 462 | public function getArmada() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Set the Armada instance |
||
| 469 | * |
||
| 470 | * @param Jaxon\Armada\Armada $xArmada The Armada instance |
||
| 471 | * |
||
| 472 | * @return void |
||
| 473 | */ |
||
| 474 | public function setArmada($xArmada) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Set the view renderers data |
||
| 481 | * |
||
| 482 | * @param array $aRenderers Array of renderer names with namespace as key |
||
| 483 | * |
||
| 484 | * @return void |
||
| 485 | */ |
||
| 486 | public function initViewRenderers($aRenderers) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Set the view namespaces data |
||
| 493 | * |
||
| 494 | * @param array $aNamespaces Array of namespaces with renderer name as key |
||
| 495 | * |
||
| 496 | * @return void |
||
| 497 | */ |
||
| 498 | public function initViewNamespaces($aNamespaces, $sDefaultNamespace) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Add a view renderer |
||
| 506 | * |
||
| 507 | * @param string $sId The unique identifier of the view renderer |
||
| 508 | * @param Closure $xClosure A closure to create the view instance |
||
| 509 | * |
||
| 510 | * @return void |
||
| 511 | */ |
||
| 512 | public function addViewRenderer($sId, $xClosure) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Get the view renderer |
||
| 536 | * |
||
| 537 | * @param string $sId The unique identifier of the view renderer |
||
| 538 | * |
||
| 539 | * @return Jaxon\Sentry\Interfaces\View |
||
| 540 | */ |
||
| 541 | public function getViewRenderer($sId = '') |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Get the session object |
||
| 554 | * |
||
| 555 | * @return Jaxon\Sentry\Interfaces\Session |
||
| 556 | */ |
||
| 557 | public function getSessionManager() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Set the session |
||
| 564 | * |
||
| 565 | * @param Closure $xClosure A closure to create the session instance |
||
| 566 | * |
||
| 567 | * @return void |
||
| 568 | */ |
||
| 569 | public function setSessionManager($xClosure) |
||
| 573 | } |
||
| 574 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.