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) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get a class instance |
||
| 166 | * |
||
| 167 | * @return object The class instance |
||
| 168 | */ |
||
| 169 | public function get($sClass) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Set a DI closure |
||
| 180 | * |
||
| 181 | * @param string $sClass The full class name |
||
| 182 | * @param Closure $xClosure The closure |
||
| 183 | * |
||
| 184 | * @return void |
||
| 185 | */ |
||
| 186 | public function set($sClass, $xClosure) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the plugin manager |
||
| 193 | * |
||
| 194 | * @return object The plugin manager |
||
| 195 | */ |
||
| 196 | public function getPluginManager() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the request manager |
||
| 203 | * |
||
| 204 | * @return object The request manager |
||
| 205 | */ |
||
| 206 | public function getRequestManager() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the request factory |
||
| 213 | * |
||
| 214 | * @return object The request factory |
||
| 215 | */ |
||
| 216 | public function getRequestFactory() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get the response manager |
||
| 223 | * |
||
| 224 | * @return object The response manager |
||
| 225 | */ |
||
| 226 | public function getResponseManager() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get the config manager |
||
| 233 | * |
||
| 234 | * @return Jaxon\Utils\Config\Config The config manager |
||
| 235 | */ |
||
| 236 | public function getConfig() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Create a new the config manager |
||
| 243 | * |
||
| 244 | * @return Jaxon\Utils\Config\Config The config manager |
||
| 245 | */ |
||
| 246 | public function newConfig() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get the minifier |
||
| 253 | * |
||
| 254 | * @return object The minifier |
||
| 255 | */ |
||
| 256 | public function getMinifier() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get the translator |
||
| 263 | * |
||
| 264 | * @return object The translator |
||
| 265 | */ |
||
| 266 | public function getTranslator() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get the template engine |
||
| 273 | * |
||
| 274 | * @return object The template engine |
||
| 275 | */ |
||
| 276 | public function getTemplate() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get the validator |
||
| 283 | * |
||
| 284 | * @return object The validator |
||
| 285 | */ |
||
| 286 | public function getValidator() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the paginator |
||
| 293 | * |
||
| 294 | * @return object The paginator |
||
| 295 | */ |
||
| 296 | public function getPaginator() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Set the pagination renderer |
||
| 303 | * |
||
| 304 | * @param object $xRenderer The pagination renderer |
||
| 305 | * |
||
| 306 | * @return void |
||
| 307 | */ |
||
| 308 | public function setPaginationRenderer($xRenderer) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get the event dispatcher |
||
| 315 | * |
||
| 316 | * @return object The event dispatcher |
||
| 317 | */ |
||
| 318 | public function getEventDispatcher() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get the Global Response object |
||
| 325 | * |
||
| 326 | * @return object The Global Response object |
||
| 327 | */ |
||
| 328 | public function getResponse() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Create a new Jaxon response object |
||
| 335 | * |
||
| 336 | * @return \Jaxon\Response\Response The new Jaxon response object |
||
| 337 | */ |
||
| 338 | public function newResponse() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get the main Jaxon object |
||
| 345 | * |
||
| 346 | * @return object The Jaxon object |
||
| 347 | */ |
||
| 348 | public function getJaxon() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get the Jaxon library version number |
||
| 355 | * |
||
| 356 | * @return string The version number |
||
| 357 | */ |
||
| 358 | public function getVersion() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get the Sentry instance |
||
| 365 | * |
||
| 366 | * @return object The Sentry instance |
||
| 367 | */ |
||
| 368 | public function getSentry() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Set the Sentry instance |
||
| 375 | * |
||
| 376 | * @param object $xSentry The Sentry instance |
||
| 377 | * |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | public function setSentry($xSentry) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get the Armada instance |
||
| 387 | * |
||
| 388 | * @return object The Armada instance |
||
| 389 | */ |
||
| 390 | public function getArmada() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Set the Armada instance |
||
| 397 | * |
||
| 398 | * @param object $xArmada The Armada instance |
||
| 399 | * |
||
| 400 | * @return void |
||
| 401 | */ |
||
| 402 | public function setArmada($xArmada) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Set the view renderers data |
||
| 409 | * |
||
| 410 | * @param array $aRenderers Array of renderer names with namespace as key |
||
| 411 | * |
||
| 412 | * @return void |
||
| 413 | */ |
||
| 414 | public function initViewRenderers($aRenderers) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Set the view namespaces data |
||
| 421 | * |
||
| 422 | * @param array $aNamespaces Array of namespaces with renderer name as key |
||
| 423 | * |
||
| 424 | * @return void |
||
| 425 | */ |
||
| 426 | public function initViewNamespaces($aNamespaces, $sDefaultNamespace) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Add a view renderer |
||
| 434 | * |
||
| 435 | * @param string $sId The unique identifier of the view renderer |
||
| 436 | * @param Closure $xClosure A closure to create the view instance |
||
| 437 | * |
||
| 438 | * @return void |
||
| 439 | */ |
||
| 440 | public function addViewRenderer($sId, $xClosure) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Get the view object |
||
| 464 | * |
||
| 465 | * @param string $sId The unique identifier of the view renderer |
||
| 466 | * |
||
| 467 | * @return object The view object |
||
| 468 | */ |
||
| 469 | public function getViewRenderer($sId = '') |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Get the session object |
||
| 482 | * |
||
| 483 | * @return object The session object |
||
| 484 | */ |
||
| 485 | public function getSessionManager() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Set the session |
||
| 492 | * |
||
| 493 | * @param Closure $xClosure A closure to create the session instance |
||
| 494 | * |
||
| 495 | * @return void |
||
| 496 | */ |
||
| 497 | public function setSessionManager($xClosure) |
||
| 501 | } |
||
| 502 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.