Complex classes like ModuleManager 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 ModuleManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 52 | class ModuleManager | ||
| 53 | { | ||
| 54 | /** | ||
| 55 | * @access protected | ||
| 56 | * @var Framework | ||
| 57 | */ | ||
| 58 | protected $framework; | ||
| 59 | |||
| 60 | /** | ||
| 61 | * @access protected | ||
| 62 | * @var \Zepi\Turbo\Backend\ObjectBackendAbstract | ||
| 63 | */ | ||
| 64 | protected $moduleObjectBackend; | ||
| 65 | |||
| 66 | /** | ||
| 67 | * @access protected | ||
| 68 | * @var array | ||
| 69 | */ | ||
| 70 | protected $moduleDirectories = array(); | ||
| 71 | |||
| 72 | /** | ||
| 73 | * @access protected | ||
| 74 | * @var array | ||
| 75 | */ | ||
| 76 | protected $activatedModules = array(); | ||
| 77 | |||
| 78 | /** | ||
| 79 | * @access protected | ||
| 80 | * @var array | ||
| 81 | */ | ||
| 82 | protected $modules = array(); | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Constructs the object | ||
| 86 | * | ||
| 87 | * @access public | ||
| 88 | * @param \Zepi\Turbo\Framework $framework | ||
| 89 | * @param \Zepi\Turbo\Backend\ObjectBackendAbstract $moduleObjectBackend | ||
| 90 | */ | ||
| 91 | public function __construct(Framework $framework, ObjectBackendAbstract $moduleObjectBackend) | ||
| 96 | |||
| 97 | /** | ||
| 98 | * Initializes the module system. Loads the activated modules from the | ||
| 99 | * object backend and loads all modules. | ||
| 100 | * | ||
| 101 | * @access public | ||
| 102 | */ | ||
| 103 | public function initializeModuleSystem() | ||
| 116 | |||
| 117 | /** | ||
| 118 | * Adds a directory as module directory | ||
| 119 | * | ||
| 120 | * @access public | ||
| 121 | * @param string $directory | ||
| 122 | * @param string $excludePattern | ||
| 123 | * @return boolean | ||
| 124 | */ | ||
| 125 | public function registerModuleDirectory($directory, $excludePattern = '/\/tests\//') | ||
| 135 | |||
| 136 | /** | ||
| 137 | * Returns all activated modules. | ||
| 138 | * | ||
| 139 | * @access public | ||
| 140 | * @return array | ||
| 141 | */ | ||
| 142 | public function getModules() | ||
| 146 | |||
| 147 | /** | ||
| 148 | * Returns the module for the given module namespace or false, | ||
| 149 | * if the module wasn't initialized. | ||
| 150 | * | ||
| 151 | * @access public | ||
| 152 | * @param string $namespace | ||
| 153 | * @return ModuleAbstract|boolean | ||
| 154 | */ | ||
| 155 | public function getModule($namespace) | ||
| 163 | |||
| 164 | /** | ||
| 165 | * Searches and activates the module with the given | ||
| 166 | * namespace. The register method of the module will | ||
| 167 | * be executed. | ||
| 168 | * | ||
| 169 | * @access public | ||
| 170 | * @param string $namespace | ||
| 171 | * @param boolean $activateDependencies | ||
| 172 | * @return boolean | ||
| 173 | * | ||
| 174 | * @throws Zepi\Turbo\Exception Can not find the module "$namespace". | ||
| 175 | */ | ||
| 176 | public function activateModule($namespace, $activateDependencies = false) | ||
| 204 | |||
| 205 | /** | ||
| 206 | * Deactivates an activated module. The deregister method | ||
| 207 | * of the module will be executed. | ||
| 208 | * | ||
| 209 | * @access public | ||
| 210 | * @param string $namespace | ||
| 211 | * @return boolean | ||
| 212 | */ | ||
| 213 | public function deactivateModule($namespace) | ||
| 241 | |||
| 242 | /** | ||
| 243 | * Searches the module for the given class name. | ||
| 244 | * | ||
| 245 | * @access public | ||
| 246 | * @param string $className | ||
| 247 | * @return boolean|Module | ||
| 248 | */ | ||
| 249 | public function getModuleByClassName($className) | ||
| 269 | |||
| 270 | /** | ||
| 271 | * Iterates trough the modules and activates each of the | ||
| 272 | * modules. | ||
| 273 | * | ||
| 274 | * @access public | ||
| 275 | */ | ||
| 276 | public function reactivateModules() | ||
| 286 | |||
| 287 | /** | ||
| 288 | * Returns an object with the properties for the given path. | ||
| 289 | * | ||
| 290 | * @access public | ||
| 291 | * @param string $path | ||
| 292 | * @return \stdClass | ||
| 293 | */ | ||
| 294 | public function getModuleProperties($path) | ||
| 298 | |||
| 299 | /** | ||
| 300 | * Returns an object with the properties of the module from | ||
| 301 | * the Module.json file in the given path. | ||
| 302 | * | ||
| 303 | * @access protected | ||
| 304 | * @param string $path | ||
| 305 | * @return \stdClass | ||
| 306 | * | ||
| 307 | * @throws Zepi\Turbo\Exception Cannot find Module.json in the path "$path". | ||
| 308 | */ | ||
| 309 | protected function parseModuleJson($path) | ||
| 319 | |||
| 320 | /** | ||
| 321 | * Returns the namespace for the module in the given path. | ||
| 322 | * | ||
| 323 | * @access protected | ||
| 324 | * @param string $path | ||
| 325 | * @return string | ||
| 326 | * | ||
| 327 | * @throws Zepi\Turbo\Exception The namespace is not set in the module properties for the Module in "$path". | ||
| 328 | */ | ||
| 329 | protected function getNamespaceFromModuleJson($path) | ||
| 339 | |||
| 340 | /** | ||
| 341 | * Initializes the module. This creates an new Module object if the | ||
| 342 | * given module path is valid. The function returns the initialized | ||
| 343 | * module or false, if the module can't be initialized. | ||
| 344 | * | ||
| 345 | * @access protected | ||
| 346 | * @param string $path | ||
| 347 | * @param boolean $activateDependencies | ||
| 348 | * @return ModuleAbstract | ||
| 349 | * | ||
| 350 | * @throws Zepi\Turbo\Exception The module "$path" is not valid | ||
| 351 | */ | ||
| 352 | protected function initializeModule($path, $activateDependencies = false) | ||
| 382 | |||
| 383 | /** | ||
| 384 | * Loads the Module.json and checks it for dependencies. If the module has | ||
| 385 | * dependencies the function will verify the modules and activate them | ||
| 386 | * if the parameter $activateDependencies is set to true. | ||
| 387 | * | ||
| 388 | * @access public | ||
| 389 | * @param string $moduleNamespace | ||
| 390 | * @param string $path | ||
| 391 | * @param boolean $activateDependencies | ||
| 392 | */ | ||
| 393 | protected function handleModuleDependencies($moduleNamespace, $path, $activateDependencies) | ||
| 410 | |||
| 411 | /** | ||
| 412 | * Handles all required dependencies. | ||
| 413 | * | ||
| 414 | * @access public | ||
| 415 | * @param string $moduleNamespace | ||
| 416 | * @param array $dependencies | ||
| 417 | * @param boolean $activateDependencies | ||
| 418 | * | ||
| 419 | * @throws Zepi\Turbo\Exception Can not activate the module "$moduleNamespace". The module requires the module "$dependencyModuleNamespace" which isn't activated. | ||
| 420 | */ | ||
| 421 | protected function handleRequiredDependencies($moduleNamespace, $dependencies, $activateDependencies) | ||
| 436 | |||
| 437 | /** | ||
| 438 | * Iterates over the available module directories and searches the | ||
| 439 | * target namespace in the module directories. If the namespace is | ||
| 440 | * found the function return the path to the module. | ||
| 441 | * | ||
| 442 | * @access protected | ||
| 443 | * @param string $namespace | ||
| 444 | * @return string | ||
| 445 | */ | ||
| 446 | protected function searchModulePath($namespace) | ||
| 473 | |||
| 474 | /** | ||
| 475 | * Saves the activated modules in the object backend. | ||
| 476 | * | ||
| 477 | * @access protected | ||
| 478 | */ | ||
| 479 | protected function saveActivatedModules() | ||
| 483 | } | ||
| 484 |