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 |
||
| 26 | abstract class Minify |
||
| 27 | { |
||
| 28 | const LEVEL_AUTODETECT = 0; |
||
| 29 | const LEVEL_JOIN = 1; |
||
| 30 | const LEVEL_FULL = 2; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The data to be minified. |
||
| 34 | * |
||
| 35 | * @var string[] |
||
| 36 | */ |
||
| 37 | protected $data = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Array of patterns to match. |
||
| 41 | * |
||
| 42 | * @var string[] |
||
| 43 | */ |
||
| 44 | protected $patterns = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * This array will hold content of strings and regular expressions that have |
||
| 48 | * been extracted from the JS source code, so we can reliably match "code", |
||
| 49 | * without having to worry about potential "code-like" characters inside. |
||
| 50 | * |
||
| 51 | * @var string[] |
||
| 52 | */ |
||
| 53 | public $extracted = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * One of types above. Minified sources can skip minification for improved speed. |
||
| 57 | * |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | protected $optimizationLevel = self::LEVEL_AUTODETECT; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Init the minify class - optionally, code may be passed along already. |
||
| 64 | */ |
||
| 65 | public function __construct(/* $data = null, ... */) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Add a file or straight-up code to be minified. |
||
| 75 | * |
||
| 76 | * @param string|string[] $data |
||
| 77 | * |
||
| 78 | * @return static |
||
| 79 | */ |
||
| 80 | public function add($data /* $data = null, ... */) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Minify the data & (optionally) saves it to a file. |
||
| 115 | * |
||
| 116 | * @param string[optional] $path Path to write the data to |
||
| 117 | * |
||
| 118 | * @return string The minified data |
||
| 119 | */ |
||
| 120 | public function minify($path = null) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Minify & gzip the data & (optionally) saves it to a file. |
||
| 134 | * |
||
| 135 | * @param string[optional] $path Path to write the data to |
||
| 136 | * @param int[optional] $level Compression level, from 0 to 9 |
||
| 137 | * |
||
| 138 | * @return string The minified & gzipped data |
||
| 139 | */ |
||
| 140 | public function gzip($path = null, $level = 9) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Minify the data & write it to a CacheItemInterface object. |
||
| 155 | * |
||
| 156 | * @param CacheItemInterface $item Cache item to write the data to |
||
| 157 | * |
||
| 158 | * @return CacheItemInterface Cache item with the minifier data |
||
| 159 | */ |
||
| 160 | public function cache(CacheItemInterface $item) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Option setter. |
||
| 170 | * |
||
| 171 | * @param int $level |
||
| 172 | * |
||
| 173 | * @return $this |
||
| 174 | */ |
||
| 175 | public function setOptimalizationLevel($level) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Minify the data. |
||
| 184 | * |
||
| 185 | * @param string[optional] $path Path to write the data to |
||
| 186 | * |
||
| 187 | * @return string The minified data |
||
| 188 | */ |
||
| 189 | abstract public function execute($path = null); |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Load data. |
||
| 193 | * |
||
| 194 | * @param string $data Either a path to a file or the content itself |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | protected function load($data) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Save to file. |
||
| 215 | * |
||
| 216 | * @param string $content The minified data |
||
| 217 | * @param string $path The path to save the minified data to |
||
| 218 | * |
||
| 219 | * @throws IOException |
||
| 220 | */ |
||
| 221 | protected function save($content, $path) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Register a pattern to execute against the source content. |
||
| 232 | * |
||
| 233 | * @param string $pattern PCRE pattern |
||
| 234 | * @param string|callable $replacement Replacement value for matched pattern |
||
| 235 | */ |
||
| 236 | protected function registerPattern($pattern, $replacement = '') |
||
| 243 | |||
| 244 | /** |
||
| 245 | * We can't "just" run some regular expressions against JavaScript: it's a |
||
| 246 | * complex language. E.g. having an occurrence of // xyz would be a comment, |
||
| 247 | * unless it's used within a string. Of you could have something that looks |
||
| 248 | * like a 'string', but inside a comment. |
||
| 249 | * The only way to accurately replace these pieces is to traverse the JS one |
||
| 250 | * character at a time and try to find whatever starts first. |
||
| 251 | * |
||
| 252 | * @param string $content The content to replace patterns in |
||
| 253 | * |
||
| 254 | * @return string The (manipulated) content |
||
| 255 | */ |
||
| 256 | protected function replace($content) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * This is where a pattern is matched against $content and the matches |
||
| 330 | * are replaced by their respective value. |
||
| 331 | * This function will be called plenty of times, where $content will always |
||
| 332 | * move up 1 character. |
||
| 333 | * |
||
| 334 | * @param string $pattern Pattern to match |
||
| 335 | * @param string|callable $replacement Replacement value |
||
| 336 | * @param string $content Content to match pattern against |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | protected function replacePattern($pattern, $replacement, $content) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Strings are a pattern we need to match, in order to ignore potential |
||
| 351 | * code-like content inside them, but we just want all of the string |
||
| 352 | * content to remain untouched. |
||
| 353 | * |
||
| 354 | * This method will replace all string content with simple STRING# |
||
| 355 | * placeholder text, so we've rid all strings from characters that may be |
||
| 356 | * misinterpreted. Original string content will be saved in $this->extracted |
||
| 357 | * and after doing all other minifying, we can restore the original content |
||
| 358 | * via restoreStrings(). |
||
| 359 | * |
||
| 360 | * @param string[optional] $chars |
||
| 361 | * @param string[optional] $placeholderPrefix |
||
| 362 | */ |
||
| 363 | protected function extractStrings($chars = '\'"', $placeholderPrefix = '') |
||
| 400 | |||
| 401 | /** |
||
| 402 | * This method will restore all extracted data (strings, regexes) that were |
||
| 403 | * replaced with placeholder text in extract*(). The original content was |
||
| 404 | * saved in $this->extracted. |
||
| 405 | * |
||
| 406 | * @param string $content |
||
| 407 | * |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | protected function restoreExtractedData($content) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Check if the path is a regular file and can be read. |
||
| 426 | * |
||
| 427 | * @param string $path |
||
| 428 | * |
||
| 429 | * @return bool |
||
| 430 | */ |
||
| 431 | protected function canImportFile($path) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Attempts to open file specified by $path for writing. |
||
| 448 | * |
||
| 449 | * @param string $path The path to the file |
||
| 450 | * |
||
| 451 | * @return resource Specifier for the target file |
||
| 452 | * |
||
| 453 | * @throws IOException |
||
| 454 | */ |
||
| 455 | protected function openFileForWriting($path) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. |
||
| 466 | * |
||
| 467 | * @param resource $handler The resource to write to |
||
| 468 | * @param string $content The content to write |
||
| 469 | * @param string $path The path to the file (for exception printing only) |
||
| 470 | * |
||
| 471 | * @throws IOException |
||
| 472 | */ |
||
| 473 | protected function writeToFile($handler, $content, $path = '') |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param string $sourceFilename |
||
| 482 | * |
||
| 483 | * @return bool |
||
| 484 | */ |
||
| 485 | protected function shouldMinifyFully($sourceFilename) |
||
| 496 | } |
||
| 497 |
If you suppress an error, we recommend checking for the error condition explicitly: