Complex classes like Minify 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 Minify, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Minify |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var Environment |
||
| 19 | */ |
||
| 20 | protected $objEnvironment; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var |
||
| 24 | */ |
||
| 25 | protected $objConfig; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Cache bust file path |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $strCacheBustFile; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Public folder path |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $strPublicFolderPath; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * File types supported |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $arrFileTypes; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Destination file extension |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $strDestinationExtension; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Destination folder |
||
| 57 | * |
||
| 58 | * @var |
||
| 59 | */ |
||
| 60 | protected $strDestinationFolder; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Destination file |
||
| 64 | * |
||
| 65 | * @var |
||
| 66 | */ |
||
| 67 | protected $strDestinationFile; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Set env to dev |
||
| 71 | * |
||
| 72 | * @var |
||
| 73 | */ |
||
| 74 | protected $blnIsDev; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Create a new Minifier Instance |
||
| 78 | * |
||
| 79 | * Minify constructor. |
||
| 80 | */ |
||
| 81 | public function __construct() |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Set Class Variables |
||
| 93 | * |
||
| 94 | * @return null |
||
| 95 | */ |
||
| 96 | public function init() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Build minified file |
||
| 112 | * |
||
| 113 | * @var $strFolder string |
||
| 114 | * @var $strFile string |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function js($strFolder = '/js', $strFile = 'app.min.js') |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Build CSS minified file |
||
| 124 | * |
||
| 125 | * @var $strFolder string |
||
| 126 | * @var $strFile string |
||
| 127 | * @return bool|null|string |
||
| 128 | */ |
||
| 129 | public function css($strFolder = '/css', $strFile = 'app.min.css') |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Generate minified file |
||
| 136 | * |
||
| 137 | * @param string $strType |
||
| 138 | * @param string $strFolder |
||
| 139 | * @param string $strFile |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function getMinifiedFile($strType = 'css', $strFolder = '/css', $strFile = 'app.min.css') |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Process Built |
||
| 154 | * |
||
| 155 | * @return bool|null|string |
||
| 156 | */ |
||
| 157 | public function process() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Build file |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function build() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get files to minify and concatenate |
||
| 202 | * |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | public function getFiles() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get minified Content |
||
| 223 | * |
||
| 224 | * @param $strFileName |
||
| 225 | * @return bool|string |
||
| 226 | */ |
||
| 227 | public function getMinifiedContent($strFileName) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Determine if it's a valid file name |
||
| 248 | * |
||
| 249 | * @param $strFileName |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | public function isValidFileName($strFileName) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Minify content |
||
| 273 | * |
||
| 274 | * @param $strFile |
||
| 275 | * @return bool|string |
||
| 276 | */ |
||
| 277 | public function minifyContent($strFile) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Save Cache Bust |
||
| 292 | * |
||
| 293 | * @var string $strApplicationFileContents |
||
| 294 | * @return null |
||
| 295 | */ |
||
| 296 | public function saveCacheBust($strApplicationFileContents = '') |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get Cache Bust |
||
| 318 | * |
||
| 319 | * @return bool|string |
||
| 320 | */ |
||
| 321 | public function getCacheBust() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Set env to Dev |
||
| 334 | * |
||
| 335 | * @param bool $bln |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | public function setDev($bln = true) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Load configuration |
||
| 347 | */ |
||
| 348 | public function loadConfig() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Set Cache bust file |
||
| 367 | * |
||
| 368 | * @return $this |
||
| 369 | */ |
||
| 370 | public function setCacheBustFile() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Set public folder |
||
| 378 | * |
||
| 379 | * @param string $strFolder |
||
| 380 | * @return $this |
||
| 381 | */ |
||
| 382 | public function setPublicFolder($strFolder = '/../../../../public') |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Set test folder |
||
| 391 | * |
||
| 392 | * @return $this |
||
| 393 | */ |
||
| 394 | public function setTest() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Set destination folder |
||
| 407 | * |
||
| 408 | * @param $strFolder |
||
| 409 | * @return $this |
||
| 410 | */ |
||
| 411 | public function setDestinationFolder($strFolder) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Set destination file |
||
| 419 | * |
||
| 420 | * @param $strFile |
||
| 421 | * @return $this |
||
| 422 | */ |
||
| 423 | public function setDestinationFile($strFile) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Set destination file extension |
||
| 431 | * |
||
| 432 | * @param $strDestinationExtension |
||
| 433 | */ |
||
| 434 | public function setDestinationExtensionType($strDestinationExtension) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get Public folder |
||
| 441 | * |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | public function getPublicFolder() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Get destination folder |
||
| 451 | * |
||
| 452 | * @return mixed |
||
| 453 | */ |
||
| 454 | public function getDestinationFolder() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Set destination file |
||
| 461 | * |
||
| 462 | * @return mixed |
||
| 463 | */ |
||
| 464 | public function getDestinationFile() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get application file name |
||
| 471 | * |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | public function getAppFileName() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Get destination file type extension |
||
| 481 | * |
||
| 482 | * @return mixed |
||
| 483 | */ |
||
| 484 | public function getDestinationExtension() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Get files folder |
||
| 491 | * |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | public function getFilesFolder() |
||
| 498 | } |
||
| 499 |