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 |
||
| 17 | abstract class Minify |
||
| 18 | { |
||
| 19 | const LEVEL_AUTODETECT = 0; |
||
| 20 | const LEVEL_JOIN = 1; |
||
| 21 | const LEVEL_FULL = 2; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The data to be minified. |
||
| 25 | * |
||
| 26 | * @var string[] |
||
| 27 | */ |
||
| 28 | protected $data = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Array of patterns to match. |
||
| 32 | * |
||
| 33 | * @var string[] |
||
| 34 | */ |
||
| 35 | protected $patterns = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * This array will hold content of strings and regular expressions that have |
||
| 39 | * been extracted from the JS source code, so we can reliably match "code", |
||
| 40 | * without having to worry about potential "code-like" characters inside. |
||
| 41 | * |
||
| 42 | * @var string[] |
||
| 43 | */ |
||
| 44 | public $extracted = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * One of types above. Minified sources can skip minification for improved speed. |
||
| 48 | * |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | protected $optimizationLevel = self::LEVEL_AUTODETECT; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Init the minify class - optionally, code may be passed along already. |
||
| 55 | */ |
||
| 56 | public function __construct(/* $data = null, ... */) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Add a file or straight-up code to be minified. |
||
| 66 | * |
||
| 67 | * @param string|string[] $data |
||
| 68 | * |
||
| 69 | * @return static |
||
| 70 | */ |
||
| 71 | public function add($data /* $data = null, ... */) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Minify the data & (optionally) saves it to a file. |
||
| 106 | * |
||
| 107 | * @param string[optional] $path Path to write the data to |
||
| 108 | * |
||
| 109 | * @return string The minified data |
||
| 110 | */ |
||
| 111 | public function minify($path = null) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Minify & gzip the data & (optionally) saves it to a file. |
||
| 125 | * |
||
| 126 | * @param string[optional] $path Path to write the data to |
||
| 127 | * @param int[optional] $level Compression level, from 0 to 9 |
||
| 128 | * |
||
| 129 | * @return string The minified & gzipped data |
||
| 130 | */ |
||
| 131 | public function gzip($path = null, $level = 9) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Minify the data & write it to a CacheItemInterface object. |
||
| 146 | * |
||
| 147 | * @param CacheItemInterface $item Cache item to write the data to |
||
| 148 | * |
||
| 149 | * @return CacheItemInterface Cache item with the minifier data |
||
| 150 | */ |
||
| 151 | public function cache(CacheItemInterface $item) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Option setter. |
||
| 161 | * |
||
| 162 | * @param int $level |
||
| 163 | * |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function setOptimalizationLevel($level) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Minify the data. |
||
| 175 | * |
||
| 176 | * @param string[optional] $path Path to write the data to |
||
| 177 | * |
||
| 178 | * @return string The minified data |
||
| 179 | */ |
||
| 180 | abstract public function execute($path = null); |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Load data. |
||
| 184 | * |
||
| 185 | * @param string $data Either a path to a file or the content itself |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | protected function load($data) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Save to file. |
||
| 206 | * |
||
| 207 | * @param string $content The minified data |
||
| 208 | * @param string $path The path to save the minified data to |
||
| 209 | * |
||
| 210 | * @throws IOException |
||
| 211 | */ |
||
| 212 | protected function save($content, $path) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Register a pattern to execute against the source content. |
||
| 223 | * |
||
| 224 | * @param string $pattern PCRE pattern |
||
| 225 | * @param string|callable $replacement Replacement value for matched pattern |
||
| 226 | */ |
||
| 227 | protected function registerPattern($pattern, $replacement = '') |
||
| 234 | |||
| 235 | /** |
||
| 236 | * We can't "just" run some regular expressions against JavaScript: it's a |
||
| 237 | * complex language. E.g. having an occurrence of // xyz would be a comment, |
||
| 238 | * unless it's used within a string. Of you could have something that looks |
||
| 239 | * like a 'string', but inside a comment. |
||
| 240 | * The only way to accurately replace these pieces is to traverse the JS one |
||
| 241 | * character at a time and try to find whatever starts first. |
||
| 242 | * |
||
| 243 | * @param string $content The content to replace patterns in |
||
| 244 | * |
||
| 245 | * @return string The (manipulated) content |
||
| 246 | */ |
||
| 247 | protected function replace($content) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * This is where a pattern is matched against $content and the matches |
||
| 321 | * are replaced by their respective value. |
||
| 322 | * This function will be called plenty of times, where $content will always |
||
| 323 | * move up 1 character. |
||
| 324 | * |
||
| 325 | * @param string $pattern Pattern to match |
||
| 326 | * @param string|callable $replacement Replacement value |
||
| 327 | * @param string $content Content to match pattern against |
||
| 328 | * |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | protected function replacePattern($pattern, $replacement, $content) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Strings are a pattern we need to match, in order to ignore potential |
||
| 342 | * code-like content inside them, but we just want all of the string |
||
| 343 | * content to remain untouched. |
||
| 344 | * |
||
| 345 | * This method will replace all string content with simple STRING# |
||
| 346 | * placeholder text, so we've rid all strings from characters that may be |
||
| 347 | * misinterpreted. Original string content will be saved in $this->extracted |
||
| 348 | * and after doing all other minifying, we can restore the original content |
||
| 349 | * via restoreStrings(). |
||
| 350 | * |
||
| 351 | * @param string[optional] $chars |
||
| 352 | * @param string[optional] $placeholderPrefix |
||
| 353 | */ |
||
| 354 | protected function extractStrings($chars = '\'"', $placeholderPrefix = '') |
||
| 391 | |||
| 392 | /** |
||
| 393 | * This method will restore all extracted data (strings, regexes) that were |
||
| 394 | * replaced with placeholder text in extract*(). The original content was |
||
| 395 | * saved in $this->extracted. |
||
| 396 | * |
||
| 397 | * @param string $content |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | protected function restoreExtractedData($content) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Check if the path is a regular file and can be read. |
||
| 417 | * |
||
| 418 | * @param string $path |
||
| 419 | * |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | protected function canImportFile($path) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Attempts to open file specified by $path for writing. |
||
| 429 | * |
||
| 430 | * @param string $path The path to the file |
||
| 431 | * |
||
| 432 | * @return resource Specifier for the target file |
||
| 433 | * |
||
| 434 | * @throws IOException |
||
| 435 | */ |
||
| 436 | protected function openFileForWriting($path) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. |
||
| 447 | * |
||
| 448 | * @param resource $handler The resource to write to |
||
| 449 | * @param string $content The content to write |
||
| 450 | * @param string $path The path to the file (for exception printing only) |
||
| 451 | * |
||
| 452 | * @throws IOException |
||
| 453 | */ |
||
| 454 | protected function writeToFile($handler, $content, $path = '') |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param string $sourceFilename |
||
| 463 | * |
||
| 464 | * @return bool |
||
| 465 | */ |
||
| 466 | protected function shouldMinifyFully($sourceFilename) |
||
| 477 | } |
||
| 478 |
If you suppress an error, we recommend checking for the error condition explicitly: