Complex classes like DeploystrategyAbstract 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 DeploystrategyAbstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | abstract class DeploystrategyAbstract |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The path mappings to map project's directories to magento's directory structure |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $mappings = array(); |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The current mapping of the deployment iteration |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $currentMapping = array(); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The List of entries which files should not get deployed |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $ignoredMappings = array(); |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * The magento installation's base directory |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $destDir; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The module's base directory |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $sourceDir; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * If set overrides existing files |
||
| 53 | * |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $isForced = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * List of files/folders which were |
||
| 60 | * created via any of the deploy methods |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $deployedFiles = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * List of files/folders which were |
||
| 68 | * remove via this remove operation |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $removedFiles = array(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var Filesystem |
||
| 76 | */ |
||
| 77 | protected $filesystem; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Constructor |
||
| 81 | * |
||
| 82 | * @param string $sourceDir |
||
| 83 | * @param string $destDir |
||
| 84 | */ |
||
| 85 | 71 | public function __construct($sourceDir, $destDir) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Executes the deployment strategy for each mapping |
||
| 94 | * |
||
| 95 | * @return \MagentoHackathon\Composer\Magento\Deploystrategy\DeploystrategyAbstract |
||
| 96 | */ |
||
| 97 | 16 | public function deploy() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * beforeDeploy |
||
| 111 | * |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | 10 | protected function beforeDeploy() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * afterDeploy |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | 10 | protected function afterDeploy() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Removes the module's files in the given path from the target dir |
||
| 131 | * |
||
| 132 | * @return \MagentoHackathon\Composer\Magento\Deploystrategy\DeploystrategyAbstract |
||
| 133 | */ |
||
| 134 | 6 | public function clean() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * beforeClean |
||
| 148 | * |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | 6 | protected function beforeClean() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * afterClean |
||
| 158 | * |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | 6 | protected function afterClean() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Returns the destination dir of the magento module |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | 48 | protected function getDestDir() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Returns the current path of the extension |
||
| 178 | * |
||
| 179 | * @return mixed |
||
| 180 | */ |
||
| 181 | 47 | protected function getSourceDir() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * If set overrides existing files |
||
| 188 | * |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | 4 | public function isForced() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Setter for isForced property |
||
| 198 | * |
||
| 199 | * @param bool $forced |
||
| 200 | */ |
||
| 201 | 16 | public function setIsForced($forced = true) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Returns the path mappings to map project's directories to magento's directory structure |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | 18 | public function getMappings() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Sets path mappings to map project's directories to magento's directory structure |
||
| 218 | * |
||
| 219 | * @param array $mappings |
||
| 220 | */ |
||
| 221 | 17 | public function setMappings(array $mappings) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Gets the current mapping used on the deployment iteration |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | 16 | public function getCurrentMapping() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Sets the current mapping used on the deployment iteration |
||
| 238 | * |
||
| 239 | * @param array $mapping |
||
| 240 | */ |
||
| 241 | 41 | public function setCurrentMapping($mapping) |
|
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * sets the current ignored mappings |
||
| 249 | * |
||
| 250 | * @param $ignoredMappings |
||
| 251 | */ |
||
| 252 | 11 | public function setIgnoredMappings($ignoredMappings) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * gets the current ignored mappings |
||
| 259 | * |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function getIgnoredMappings() |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $destination |
||
| 270 | * |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | 47 | protected function isDestinationIgnored($destination) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Add a key value pair to mapping |
||
| 288 | */ |
||
| 289 | 3 | public function addMapping($key, $value) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $path |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | 47 | protected function removeLeadingSlash($path) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $path |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | 47 | protected function removeTrailingSlash($path) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $path |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | protected function removeLeadingAndTrailingSlash($path) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Normalize mapping parameters using a glob wildcard. |
||
| 323 | * |
||
| 324 | * Delegate the creation of the module's files in the given destination. |
||
| 325 | * |
||
| 326 | * @param string $source |
||
| 327 | * @param string $dest |
||
| 328 | * |
||
| 329 | * @throws \ErrorException |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | 47 | public function create($source, $dest) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Remove (unlink) the destination file |
||
| 393 | * |
||
| 394 | * @param string $source |
||
| 395 | * @param string $dest |
||
| 396 | * |
||
| 397 | * @throws \ErrorException |
||
| 398 | */ |
||
| 399 | 4 | public function remove($source, $dest) |
|
| 400 | { |
||
| 401 | 4 | $sourcePath = $this->getSourceDir() . '/' . ltrim($this->removeTrailingSlash($source), '\\/'); |
|
| 402 | 4 | $destPath = $this->getDestDir() . '/' . ltrim($dest, '\\/'); |
|
| 403 | |||
| 404 | // If source doesn't exist, check if it's a glob expression, otherwise we have nothing we can do |
||
| 405 | 4 | if (!file_exists($sourcePath)) { |
|
| 406 | // Handle globing |
||
| 407 | $matches = glob($sourcePath); |
||
| 408 | if (!empty($matches)) { |
||
| 409 | foreach ($matches as $match) { |
||
| 410 | $newDest = substr($destPath . '/' . basename($match), strlen($this->getDestDir())); |
||
| 411 | $newDest = ltrim($newDest, ' \\/'); |
||
| 412 | $this->remove(substr($match, strlen($this->getSourceDir()) + 1), $newDest); |
||
| 413 | } |
||
| 414 | } |
||
| 415 | return; |
||
| 416 | } |
||
| 417 | |||
| 418 | // MP Avoid removing whole folders in case the modman file is not 100% well-written |
||
| 419 | // e.g. app/etc/modules/Testmodule.xml app/etc/modules/ installs correctly, |
||
| 420 | // but would otherwise delete the whole app/etc/modules folder! |
||
| 421 | 4 | if (basename($sourcePath) !== basename($destPath)) { |
|
| 422 | $destPath .= '/' . basename($source); |
||
| 423 | } |
||
| 424 | 4 | $this->filesystem->remove($destPath); |
|
| 425 | 4 | $this->addRemovedFile($destPath); |
|
| 426 | 4 | } |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Remove an empty directory branch up to $stopDir, or stop at the first non-empty parent. |
||
| 430 | * |
||
| 431 | * @param string $dir |
||
| 432 | * @param string $stopDir |
||
| 433 | */ |
||
| 434 | 4 | public function rmEmptyDirsRecursive($dir, $stopDir = null) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Create the module's files in the given destination. |
||
| 468 | * |
||
| 469 | * NOTE: source and dest have to be passed as relative directories, like they are listed in the mapping |
||
| 470 | * |
||
| 471 | * @param string $source |
||
| 472 | * @param string $dest |
||
| 473 | * |
||
| 474 | * @return bool |
||
| 475 | */ |
||
| 476 | abstract protected function createDelegate($source, $dest); |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Add a file/folder to the list of deployed files |
||
| 480 | * @param string $file |
||
| 481 | */ |
||
| 482 | 38 | public function addDeployedFile($file) |
|
| 483 | { |
||
| 484 | 38 | $destination = str_replace('\\', '/', $this->getDestDir()); |
|
| 485 | //strip of destination deploy |
||
| 486 | 38 | $quoted = preg_quote($destination, '/'); |
|
| 487 | 38 | $file = preg_replace(sprintf('/^%s/', $quoted), '', $file); |
|
| 488 | 38 | $this->deployedFiles[] = $file; |
|
| 489 | 38 | } |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Add a file/folder to the list of removed files |
||
| 493 | * @param string $file |
||
| 494 | */ |
||
| 495 | 4 | public function addRemovedFile($file) |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Get all the deployed files |
||
| 504 | * |
||
| 505 | * @return array |
||
| 506 | */ |
||
| 507 | 10 | public function getDeployedFiles() |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Get all the removed files |
||
| 514 | * |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | 3 | public function getRemovedFiles() |
|
| 521 | } |
||
| 522 |