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 |
||
| 11 | class Minify |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Cache bust file path |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $strCacheBustFile; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Public folder path |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $strPublicFolderPath; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * File types supported |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $arrFileTypes; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Destination file extension |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $strDestinationExtension; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Destination folder |
||
| 43 | * |
||
| 44 | * @var |
||
| 45 | */ |
||
| 46 | protected $strDestinationFolder; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Destination file |
||
| 50 | * |
||
| 51 | * @var |
||
| 52 | */ |
||
| 53 | protected $strDestinationFile; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Set env to dev |
||
| 57 | * |
||
| 58 | * @var |
||
| 59 | */ |
||
| 60 | protected $blnIsDev; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Create a new Minifier Instance |
||
| 64 | */ |
||
| 65 | public function __construct() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Set env to Dev |
||
| 73 | * |
||
| 74 | * @param bool $bln |
||
| 75 | * @return $this |
||
| 76 | */ |
||
| 77 | public function setDev($bln = true) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Set Class Variables |
||
| 85 | * |
||
| 86 | * @return null |
||
| 87 | */ |
||
| 88 | public function init() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Set Cache bust file |
||
| 104 | * |
||
| 105 | * @param string $strFile |
||
| 106 | * @return $this |
||
| 107 | */ |
||
| 108 | public function setCacheBustFile($strFile = 'autominifier-cache-bust.txt') |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set public folder |
||
| 116 | * |
||
| 117 | * @param string $strFolder |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | public function setPublicFolder($strFolder = '/../../../../public') |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Set test folder |
||
| 129 | * |
||
| 130 | * @return $this |
||
| 131 | */ |
||
| 132 | public function setTest() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get Public folder |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function getPublicFolder() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Build minified file |
||
| 155 | * |
||
| 156 | * @var $strFolder string |
||
| 157 | * @var $strFile string |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function js($strFolder = '/js', $strFile = 'app.min.js') |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Build CSS minified file |
||
| 167 | * |
||
| 168 | * @var $strFolder string |
||
| 169 | * @var $strFile string |
||
| 170 | * @return bool|null|string |
||
| 171 | */ |
||
| 172 | public function css($strFolder = '/css', $strFile = 'app.min.css') |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Generate minified file |
||
| 179 | * |
||
| 180 | * @param string $strType |
||
| 181 | * @param string $strFolder |
||
| 182 | * @param string $strFile |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getMinifiedFile($strType = 'css', $strFolder = '/css', $strFile = 'app.min.css') |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Process Built |
||
| 196 | * |
||
| 197 | * @return bool|null|string |
||
| 198 | */ |
||
| 199 | public function process() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Set destination folder |
||
| 218 | * |
||
| 219 | * @param $strFolder |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | public function setDestinationFolder($strFolder) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Set destination file |
||
| 230 | * |
||
| 231 | * @param $strFile |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | public function setDestinationFile($strFile) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get destination folder |
||
| 242 | * |
||
| 243 | * @return mixed |
||
| 244 | */ |
||
| 245 | public function getDestinationFolder() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set destination file |
||
| 252 | * |
||
| 253 | * @return mixed |
||
| 254 | */ |
||
| 255 | public function getDestinationFile() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Save Cache Bust |
||
| 262 | * |
||
| 263 | * @var string $strApplicationFileContents |
||
| 264 | * @return null |
||
| 265 | */ |
||
| 266 | public function saveCacheBust($strApplicationFileContents = '') |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get Cache Bust |
||
| 288 | * |
||
| 289 | * @return bool|string |
||
| 290 | */ |
||
| 291 | public function getCacheBust() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Determine if we are in development environment |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | public function isDevEnv() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Determine if it's .dev domain |
||
| 329 | * |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | public function isDevDomain() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Is is localhost |
||
| 340 | * |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | public function isLocalhost() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Create minified and concatenated file |
||
| 355 | * |
||
| 356 | * @param $strApplicationFileContents |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | public function createApplicationFile($strApplicationFileContents) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Get application file name |
||
| 367 | * |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | public function getAppFileName() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Set destination file extension |
||
| 377 | * |
||
| 378 | * @param $strDestinationExtension |
||
| 379 | */ |
||
| 380 | public function setDestinationExtensionType($strDestinationExtension) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get destination file type extension |
||
| 387 | * |
||
| 388 | * @return mixed |
||
| 389 | */ |
||
| 390 | public function getDestinationExtension() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Build file |
||
| 397 | * |
||
| 398 | * @param $arrOriginExtensions |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | public function build($arrOriginExtensions) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Get minified Content |
||
| 443 | * |
||
| 444 | * @param $strFile |
||
| 445 | * @return bool|string |
||
| 446 | */ |
||
| 447 | public function getMinifiedContent($strFile) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Minify content |
||
| 460 | * |
||
| 461 | * @param $strFile |
||
| 462 | * @return bool|string |
||
| 463 | */ |
||
| 464 | public function minifyContent($strFile) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Minify JS |
||
| 479 | * |
||
| 480 | * @param $strFile |
||
| 481 | * @return bool|string |
||
| 482 | */ |
||
| 483 | public function minifyJs($strFile) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Minify CSS |
||
| 490 | * |
||
| 491 | * @param $strFile |
||
| 492 | * @return bool|string |
||
| 493 | */ |
||
| 494 | public function minifyCss($strFile) |
||
| 498 | } |
||
| 499 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: