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 |
||
| 20 | class Container |
||
| 21 | { |
||
| 22 | // The Dependency Injection Container |
||
| 23 | private $coreContainer = null; |
||
| 24 | |||
| 25 | // The Dependency Injection Container |
||
| 26 | private $sentryContainer = null; |
||
| 27 | |||
| 28 | // The only instance of the Container (Singleton) |
||
| 29 | private static $xInstance = null; |
||
| 30 | |||
| 31 | public static function getInstance() |
||
| 39 | |||
| 40 | private function __construct() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get the container provided by the integrated framework |
||
| 51 | * |
||
| 52 | * @return ContainerInterface |
||
| 53 | */ |
||
| 54 | public function getSentryContainer() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Set the container provided by the integrated framework |
||
| 61 | * |
||
| 62 | * @param ContainerInterface $container The container implementation |
||
| 63 | * |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | public function setSentryContainer(ContainerInterface $container) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Set the parameters and create the objects in the dependency injection container |
||
| 73 | * |
||
| 74 | * @param string $sTranslationDir The translation directory |
||
| 75 | * @param string $sTemplateDir The template directory |
||
| 76 | * |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | private function init($sTranslationDir, $sTemplateDir) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get a class instance |
||
| 170 | * |
||
| 171 | * @return object The class instance |
||
| 172 | */ |
||
| 173 | public function get($sClass) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Set a DI closure |
||
| 184 | * |
||
| 185 | * @param string $sClass The full class name |
||
| 186 | * @param Closure $xClosure The closure |
||
| 187 | * |
||
| 188 | * @return void |
||
| 189 | */ |
||
| 190 | public function set($sClass, $xClosure) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get the plugin manager |
||
| 197 | * |
||
| 198 | * @return object The plugin manager |
||
| 199 | */ |
||
| 200 | public function getPluginManager() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get the request manager |
||
| 207 | * |
||
| 208 | * @return object The request manager |
||
| 209 | */ |
||
| 210 | public function getRequestManager() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get the request factory |
||
| 217 | * |
||
| 218 | * @return object The request factory |
||
| 219 | */ |
||
| 220 | public function getRequestFactory() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get the parameter factory |
||
| 227 | * |
||
| 228 | * @return object The parameter factory |
||
| 229 | */ |
||
| 230 | public function getParameterFactory() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get the response manager |
||
| 237 | * |
||
| 238 | * @return object The response manager |
||
| 239 | */ |
||
| 240 | public function getResponseManager() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get the config manager |
||
| 247 | * |
||
| 248 | * @return Jaxon\Utils\Config\Config The config manager |
||
| 249 | */ |
||
| 250 | public function getConfig() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Create a new the config manager |
||
| 257 | * |
||
| 258 | * @return Jaxon\Utils\Config\Config The config manager |
||
| 259 | */ |
||
| 260 | public function newConfig() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get the minifier |
||
| 267 | * |
||
| 268 | * @return object The minifier |
||
| 269 | */ |
||
| 270 | public function getMinifier() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get the translator |
||
| 277 | * |
||
| 278 | * @return object The translator |
||
| 279 | */ |
||
| 280 | public function getTranslator() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get the template engine |
||
| 287 | * |
||
| 288 | * @return object The template engine |
||
| 289 | */ |
||
| 290 | public function getTemplate() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get the validator |
||
| 297 | * |
||
| 298 | * @return object The validator |
||
| 299 | */ |
||
| 300 | public function getValidator() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get the paginator |
||
| 307 | * |
||
| 308 | * @return object The paginator |
||
| 309 | */ |
||
| 310 | public function getPaginator() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Set the pagination renderer |
||
| 317 | * |
||
| 318 | * @param object $xRenderer The pagination renderer |
||
| 319 | * |
||
| 320 | * @return void |
||
| 321 | */ |
||
| 322 | public function setPaginationRenderer($xRenderer) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get the event dispatcher |
||
| 329 | * |
||
| 330 | * @return object The event dispatcher |
||
| 331 | */ |
||
| 332 | public function getEventDispatcher() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get the Global Response object |
||
| 339 | * |
||
| 340 | * @return object The Global Response object |
||
| 341 | */ |
||
| 342 | public function getResponse() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Create a new Jaxon response object |
||
| 349 | * |
||
| 350 | * @return \Jaxon\Response\Response The new Jaxon response object |
||
| 351 | */ |
||
| 352 | public function newResponse() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get the main Jaxon object |
||
| 359 | * |
||
| 360 | * @return object The Jaxon object |
||
| 361 | */ |
||
| 362 | public function getJaxon() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get the Jaxon library version number |
||
| 369 | * |
||
| 370 | * @return string The version number |
||
| 371 | */ |
||
| 372 | public function getVersion() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get the Sentry instance |
||
| 379 | * |
||
| 380 | * @return object The Sentry instance |
||
| 381 | */ |
||
| 382 | public function getSentry() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Set the Sentry instance |
||
| 389 | * |
||
| 390 | * @param object $xSentry The Sentry instance |
||
| 391 | * |
||
| 392 | * @return void |
||
| 393 | */ |
||
| 394 | public function setSentry($xSentry) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Get the Armada instance |
||
| 401 | * |
||
| 402 | * @return object The Armada instance |
||
| 403 | */ |
||
| 404 | public function getArmada() |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Set the Armada instance |
||
| 411 | * |
||
| 412 | * @param object $xArmada The Armada instance |
||
| 413 | * |
||
| 414 | * @return void |
||
| 415 | */ |
||
| 416 | public function setArmada($xArmada) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Set the view renderers data |
||
| 423 | * |
||
| 424 | * @param array $aRenderers Array of renderer names with namespace as key |
||
| 425 | * |
||
| 426 | * @return void |
||
| 427 | */ |
||
| 428 | public function initViewRenderers($aRenderers) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Set the view namespaces data |
||
| 435 | * |
||
| 436 | * @param array $aNamespaces Array of namespaces with renderer name as key |
||
| 437 | * |
||
| 438 | * @return void |
||
| 439 | */ |
||
| 440 | public function initViewNamespaces($aNamespaces, $sDefaultNamespace) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Add a view renderer |
||
| 448 | * |
||
| 449 | * @param string $sId The unique identifier of the view renderer |
||
| 450 | * @param Closure $xClosure A closure to create the view instance |
||
| 451 | * |
||
| 452 | * @return void |
||
| 453 | */ |
||
| 454 | public function addViewRenderer($sId, $xClosure) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Get the view object |
||
| 478 | * |
||
| 479 | * @param string $sId The unique identifier of the view renderer |
||
| 480 | * |
||
| 481 | * @return object The view object |
||
| 482 | */ |
||
| 483 | public function getViewRenderer($sId = '') |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Get the session object |
||
| 496 | * |
||
| 497 | * @return object The session object |
||
| 498 | */ |
||
| 499 | public function getSessionManager() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Set the session |
||
| 506 | * |
||
| 507 | * @param Closure $xClosure A closure to create the session instance |
||
| 508 | * |
||
| 509 | * @return void |
||
| 510 | */ |
||
| 511 | public function setSessionManager($xClosure) |
||
| 515 | } |
||
| 516 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.