Complex classes like ModuleInstall 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 ModuleInstall, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class ModuleInstall |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @const INSTALL_SCRIPT_VERSION : Script's version |
||
| 14 | */ |
||
| 15 | const INSTALL_SCRIPT_VERSION = '3.0.0'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string $projectPath : Path to root bfw project |
||
| 19 | */ |
||
| 20 | protected $projectPath = ''; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string $bfwConfigPath : Path to bfw config directory |
||
| 24 | */ |
||
| 25 | protected $bfwConfigPath = ''; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string $bfwModulePath : Path to bfw modules directory |
||
| 29 | */ |
||
| 30 | protected $bfwModulePath = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var boolean $forceReinstall : Force complete reinstall of module |
||
| 34 | */ |
||
| 35 | protected $forceReinstall = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string $name : Module name |
||
| 39 | */ |
||
| 40 | protected $name = ''; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string $sourcePath : Path to module which be installed |
||
| 44 | */ |
||
| 45 | protected $sourcePath = ''; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string $sourceSrcPath : Path to directory contains file to install |
||
| 49 | * in project module directory |
||
| 50 | */ |
||
| 51 | protected $sourceSrcPath = ''; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string $sourceConfigPath : Path to directory contains config file |
||
| 55 | * to install in projet config directory |
||
| 56 | */ |
||
| 57 | protected $sourceConfigPath = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array $configFiles : List of config file |
||
| 61 | * to copy on the config directory |
||
| 62 | */ |
||
| 63 | protected $configFilesList = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string|bool $sourceInstallScript : Script to run for a specific |
||
| 67 | * install of the module |
||
| 68 | */ |
||
| 69 | protected $sourceInstallScript = ''; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string $targetSrcPath : Path to directory where module will be |
||
| 73 | * installed |
||
| 74 | */ |
||
| 75 | protected $targetSrcPath = ''; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string $targetConfigPath : Path to directory where config files |
||
| 79 | * will be installed |
||
| 80 | */ |
||
| 81 | protected $targetConfigPath = ''; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Constructor |
||
| 85 | * |
||
| 86 | * @param string $projectPath Path to root bfw project |
||
| 87 | * @param string $modulePath Path to module which be installed |
||
| 88 | */ |
||
| 89 | public function __construct($projectPath, $modulePath) |
||
| 90 | { |
||
| 91 | $this->projectPath = $projectPath; |
||
| 92 | $this->sourcePath = $modulePath; |
||
| 93 | |||
| 94 | $this->bfwConfigPath = $this->projectPath.'/app/config/'; |
||
| 95 | $this->bfwModulePath = $this->projectPath.'/app/modules/'; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get accessor to module name |
||
| 100 | * |
||
| 101 | * @return string : Module name |
||
| 102 | */ |
||
| 103 | public function getName() |
||
| 104 | { |
||
| 105 | return $this->name; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get accessor to source module path |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function getSourcePath() |
||
| 114 | { |
||
| 115 | return $this->sourcePath; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get accessor to the install script file or list |
||
| 120 | * |
||
| 121 | * @return string|array |
||
| 122 | */ |
||
| 123 | public function getSourceInstallScript() |
||
| 124 | { |
||
| 125 | return $this->sourceInstallScript; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Find the module name and declare path to target directories install. |
||
| 130 | * |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | protected function findModuleName() |
||
| 134 | { |
||
| 135 | $pathExplode = explode('/', $this->sourcePath); |
||
| 136 | $this->name = $pathExplode[(count($pathExplode) - 1)]; |
||
| 137 | |||
| 138 | $this->targetSrcPath = $this->bfwModulePath.$this->name; |
||
| 139 | $this->targetConfigPath = $this->bfwConfigPath.$this->name; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get infos for this module from BFW Module class |
||
| 144 | * It's a separate method for easy override. |
||
| 145 | * |
||
| 146 | * @return \stdClass |
||
| 147 | */ |
||
| 148 | protected function getInfosFromModule() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Load module informations from files |
||
| 155 | * |
||
| 156 | * @return void |
||
| 157 | */ |
||
| 158 | public function loadInfos() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Run module installation |
||
| 201 | * |
||
| 202 | * @param boolean $reinstall : If we force reinstall module |
||
| 203 | * |
||
| 204 | * @return void |
||
| 205 | */ |
||
| 206 | public function install($reinstall) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Create symlink in bfw project module directory |
||
| 223 | * |
||
| 224 | * @return void |
||
| 225 | * |
||
| 226 | * @throws Exception : If remove symlink fail for reinstall option |
||
| 227 | */ |
||
| 228 | protected function createSymbolicLink() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Create a directory in bfw project config directory for this module and |
||
| 265 | * copy all config files declared in this directory |
||
| 266 | * |
||
| 267 | * @return void |
||
| 268 | */ |
||
| 269 | protected function copyConfigFiles() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Create a directory in bfw project config directory for this module |
||
| 305 | * |
||
| 306 | * @return boolean : If directory exist. |
||
| 307 | * |
||
| 308 | * @throws Exception : If remove directory fail for reinstall option |
||
| 309 | */ |
||
| 310 | protected function createConfigDirectory() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Supprime les dossiers récursivement |
||
| 355 | * |
||
| 356 | * @param string $dirPath Le chemin vers le dossier |
||
| 357 | * |
||
| 358 | * @return boolean |
||
| 359 | */ |
||
| 360 | protected static function removeDirectory($dirPath) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Copy a config file into config directory for this module |
||
| 394 | * |
||
| 395 | * @param string $configFileName : The config filename |
||
| 396 | * |
||
| 397 | * @return void |
||
| 398 | * |
||
| 399 | * @throws Exception If copy fail or if source file not exist |
||
| 400 | */ |
||
| 401 | protected function copyConfigFile($configFileName) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Run specific module install script if declared |
||
| 432 | * |
||
| 433 | * @return void |
||
| 434 | */ |
||
| 435 | protected function checkInstallScript() |
||
| 461 | |||
| 462 | public function runInstall($scriptName) { |
||
| 468 | } |
||
| 469 |