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 | /** |
||
| 29 | * The data to be minified. |
||
| 30 | * |
||
| 31 | * @var string[] |
||
| 32 | */ |
||
| 33 | protected $data = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Array of patterns to match. |
||
| 37 | * |
||
| 38 | * @var string[] |
||
| 39 | */ |
||
| 40 | protected $patterns = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * This array will hold content of strings and regular expressions that have |
||
| 44 | * been extracted from the JS source code, so we can reliably match "code", |
||
| 45 | * without having to worry about potential "code-like" characters inside. |
||
| 46 | * |
||
| 47 | * @var string[] |
||
| 48 | */ |
||
| 49 | public $extracted = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Path from where resources like images, fonts etc. can be imported. |
||
| 53 | * Minify will be default look for resources based on its path defined in CSS, |
||
| 54 | * if your structure of files is different because of different folder structure, |
||
| 55 | * you can specify base directory. |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $resourcesBasePath = ""; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Init the minify class - optionally, code may be passed along already. |
||
| 62 | */ |
||
| 63 | public function __construct(/* $data = null, ... */) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Add a file or straight-up code to be minified. |
||
| 73 | * |
||
| 74 | * @param string|string[] $data |
||
| 75 | * |
||
| 76 | * @return static |
||
| 77 | */ |
||
| 78 | public function add($data /* $data = null, ... */) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Add a file to be minified. |
||
| 113 | * |
||
| 114 | * @param string|string[] $data |
||
| 115 | * |
||
| 116 | * @return static |
||
| 117 | * |
||
| 118 | * @throws IOException |
||
| 119 | */ |
||
| 120 | public function addFile($data /* $data = null, ... */) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Minify the data & (optionally) saves it to a file. |
||
| 151 | * |
||
| 152 | * @param string[optional] $path Path to write the data to |
||
| 153 | * |
||
| 154 | * @return string The minified data |
||
| 155 | */ |
||
| 156 | public function minify($path = null) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Minify & gzip the data & (optionally) saves it to a file. |
||
| 170 | * |
||
| 171 | * @param string[optional] $path Path to write the data to |
||
| 172 | * @param int[optional] $level Compression level, from 0 to 9 |
||
| 173 | * |
||
| 174 | * @return string The minified & gzipped data |
||
| 175 | */ |
||
| 176 | public function gzip($path = null, $level = 9) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Minify the data & write it to a CacheItemInterface object. |
||
| 191 | * |
||
| 192 | * @param CacheItemInterface $item Cache item to write the data to |
||
| 193 | * |
||
| 194 | * @return CacheItemInterface Cache item with the minifier data |
||
| 195 | */ |
||
| 196 | public function cache(CacheItemInterface $item) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Path from where resources like images, fonts etc. can be imported. |
||
| 206 | * Minify will be default look for resources based on its path defined in CSS, |
||
| 207 | * if your structure of files is different because of different folder structure, |
||
| 208 | * you can specify base directory. |
||
| 209 | * @var string |
||
| 210 | */ |
||
| 211 | public function setResourcesBasePath(string $path) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Minify the data. |
||
| 218 | * |
||
| 219 | * @param string[optional] $path Path to write the data to |
||
| 220 | * |
||
| 221 | * @return string The minified data |
||
| 222 | */ |
||
| 223 | abstract public function execute($path = null); |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Load data. |
||
| 227 | * |
||
| 228 | * @param string $data Either a path to a file or the content itself |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | protected function load($data) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Save to file. |
||
| 249 | * |
||
| 250 | * @param string $content The minified data |
||
| 251 | * @param string $path The path to save the minified data to |
||
| 252 | * |
||
| 253 | * @throws IOException |
||
| 254 | */ |
||
| 255 | protected function save($content, $path) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Register a pattern to execute against the source content. |
||
| 266 | * |
||
| 267 | * @param string $pattern PCRE pattern |
||
| 268 | * @param string|callable $replacement Replacement value for matched pattern |
||
| 269 | */ |
||
| 270 | protected function registerPattern($pattern, $replacement = '') |
||
| 277 | |||
| 278 | /** |
||
| 279 | * We can't "just" run some regular expressions against JavaScript: it's a |
||
| 280 | * complex language. E.g. having an occurrence of // xyz would be a comment, |
||
| 281 | * unless it's used within a string. Of you could have something that looks |
||
| 282 | * like a 'string', but inside a comment. |
||
| 283 | * The only way to accurately replace these pieces is to traverse the JS one |
||
| 284 | * character at a time and try to find whatever starts first. |
||
| 285 | * |
||
| 286 | * @param string $content The content to replace patterns in |
||
| 287 | * |
||
| 288 | * @return string The (manipulated) content |
||
| 289 | */ |
||
| 290 | protected function replace($content) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * This is where a pattern is matched against $content and the matches |
||
| 369 | * are replaced by their respective value. |
||
| 370 | * This function will be called plenty of times, where $content will always |
||
| 371 | * move up 1 character. |
||
| 372 | * |
||
| 373 | * @param string $pattern Pattern to match |
||
| 374 | * @param string|callable $replacement Replacement value |
||
| 375 | * @param string $content Content to match pattern against |
||
| 376 | * |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | protected function replacePattern($pattern, $replacement, $content) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Strings are a pattern we need to match, in order to ignore potential |
||
| 390 | * code-like content inside them, but we just want all of the string |
||
| 391 | * content to remain untouched. |
||
| 392 | * |
||
| 393 | * This method will replace all string content with simple STRING# |
||
| 394 | * placeholder text, so we've rid all strings from characters that may be |
||
| 395 | * misinterpreted. Original string content will be saved in $this->extracted |
||
| 396 | * and after doing all other minifying, we can restore the original content |
||
| 397 | * via restoreStrings(). |
||
| 398 | * |
||
| 399 | * @param string[optional] $chars |
||
| 400 | * @param string[optional] $placeholderPrefix |
||
| 401 | */ |
||
| 402 | protected function extractStrings($chars = '\'"', $placeholderPrefix = '') |
||
| 439 | |||
| 440 | /** |
||
| 441 | * This method will restore all extracted data (strings, regexes) that were |
||
| 442 | * replaced with placeholder text in extract*(). The original content was |
||
| 443 | * saved in $this->extracted. |
||
| 444 | * |
||
| 445 | * @param string $content |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | protected function restoreExtractedData($content) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Check if the path is a regular file and can be read. |
||
| 465 | * |
||
| 466 | * @param string $path |
||
| 467 | * |
||
| 468 | * @return bool |
||
| 469 | */ |
||
| 470 | protected function canImportFile($path) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Attempts to open file specified by $path for writing. |
||
| 487 | * |
||
| 488 | * @param string $path The path to the file |
||
| 489 | * |
||
| 490 | * @return resource Specifier for the target file |
||
| 491 | * |
||
| 492 | * @throws IOException |
||
| 493 | */ |
||
| 494 | protected function openFileForWriting($path) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. |
||
| 505 | * |
||
| 506 | * @param resource $handler The resource to write to |
||
| 507 | * @param string $content The content to write |
||
| 508 | * @param string $path The path to the file (for exception printing only) |
||
| 509 | * |
||
| 510 | * @throws IOException |
||
| 511 | */ |
||
| 512 | protected function writeToFile($handler, $content, $path = '') |
||
| 518 | } |
||
| 519 |
If you suppress an error, we recommend checking for the error condition explicitly: