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 | * Init the minify class - optionally, code may be passed along already. |
||
| 53 | */ |
||
| 54 | public function __construct(/* $data = null, ... */) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Add a file or straight-up code to be minified. |
||
| 64 | * |
||
| 65 | * @param string|string[] $data |
||
| 66 | * |
||
| 67 | * @return static |
||
| 68 | */ |
||
| 69 | public function add($data /* $data = null, ... */) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Add a file to be minified. |
||
| 104 | * |
||
| 105 | * @param string|string[] $data |
||
| 106 | * |
||
| 107 | * @return static |
||
| 108 | * |
||
| 109 | * @throws IOException |
||
| 110 | */ |
||
| 111 | public function addFile($data /* $data = null, ... */) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Minify the data & (optionally) saves it to a file. |
||
| 142 | * |
||
| 143 | * @param string[optional] $path Path to write the data to |
||
| 144 | * |
||
| 145 | * @return string The minified data |
||
| 146 | */ |
||
| 147 | public function minify($path = null) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Minify & gzip the data & (optionally) saves it to a file. |
||
| 161 | * |
||
| 162 | * @param string[optional] $path Path to write the data to |
||
| 163 | * @param int[optional] $level Compression level, from 0 to 9 |
||
| 164 | * |
||
| 165 | * @return string The minified & gzipped data |
||
| 166 | */ |
||
| 167 | public function gzip($path = null, $level = 9) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Minify the data & write it to a CacheItemInterface object. |
||
| 182 | * |
||
| 183 | * @param CacheItemInterface $item Cache item to write the data to |
||
| 184 | * |
||
| 185 | * @return CacheItemInterface Cache item with the minifier data |
||
| 186 | */ |
||
| 187 | public function cache(CacheItemInterface $item) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Minify the data. |
||
| 197 | * |
||
| 198 | * @param string[optional] $path Path to write the data to |
||
| 199 | * |
||
| 200 | * @return string The minified data |
||
| 201 | */ |
||
| 202 | abstract public function execute($path = null); |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Load data. |
||
| 206 | * |
||
| 207 | * @param string $data Either a path to a file or the content itself |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | protected function load($data) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Save to file. |
||
| 228 | * |
||
| 229 | * @param string $content The minified data |
||
| 230 | * @param string $path The path to save the minified data to |
||
| 231 | * |
||
| 232 | * @throws IOException |
||
| 233 | */ |
||
| 234 | protected function save($content, $path) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Register a pattern to execute against the source content. |
||
| 245 | * |
||
| 246 | * @param string $pattern PCRE pattern |
||
| 247 | * @param string|callable $replacement Replacement value for matched pattern |
||
| 248 | */ |
||
| 249 | protected function registerPattern($pattern, $replacement = '') |
||
| 256 | |||
| 257 | /** |
||
| 258 | * We can't "just" run some regular expressions against JavaScript: it's a |
||
| 259 | * complex language. E.g. having an occurrence of // xyz would be a comment, |
||
| 260 | * unless it's used within a string. Of you could have something that looks |
||
| 261 | * like a 'string', but inside a comment. |
||
| 262 | * The only way to accurately replace these pieces is to traverse the JS one |
||
| 263 | * character at a time and try to find whatever starts first. |
||
| 264 | * |
||
| 265 | * @param string $content The content to replace patterns in |
||
| 266 | * |
||
| 267 | * @return string The (manipulated) content |
||
| 268 | */ |
||
| 269 | protected function replace($content) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * This is where a pattern is matched against $content and the matches |
||
| 348 | * are replaced by their respective value. |
||
| 349 | * This function will be called plenty of times, where $content will always |
||
| 350 | * move up 1 character. |
||
| 351 | * |
||
| 352 | * @param string $pattern Pattern to match |
||
| 353 | * @param string|callable $replacement Replacement value |
||
| 354 | * @param string $content Content to match pattern against |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | protected function replacePattern($pattern, $replacement, $content) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Strings are a pattern we need to match, in order to ignore potential |
||
| 369 | * code-like content inside them, but we just want all of the string |
||
| 370 | * content to remain untouched. |
||
| 371 | * |
||
| 372 | * This method will replace all string content with simple STRING# |
||
| 373 | * placeholder text, so we've rid all strings from characters that may be |
||
| 374 | * misinterpreted. Original string content will be saved in $this->extracted |
||
| 375 | * and after doing all other minifying, we can restore the original content |
||
| 376 | * via restoreStrings(). |
||
| 377 | * |
||
| 378 | * @param string[optional] $chars |
||
| 379 | * @param string[optional] $placeholderPrefix |
||
| 380 | */ |
||
| 381 | protected function extractStrings($chars = '\'"', $placeholderPrefix = '') |
||
| 418 | |||
| 419 | /** |
||
| 420 | * This method will restore all extracted data (strings, regexes) that were |
||
| 421 | * replaced with placeholder text in extract*(). The original content was |
||
| 422 | * saved in $this->extracted. |
||
| 423 | * |
||
| 424 | * @param string $content |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | protected function restoreExtractedData($content) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Check if the path is a regular file and can be read. |
||
| 444 | * |
||
| 445 | * @param string $path |
||
| 446 | * |
||
| 447 | * @return bool |
||
| 448 | */ |
||
| 449 | protected function canImportFile($path) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Attempts to open file specified by $path for writing. |
||
| 466 | * |
||
| 467 | * @param string $path The path to the file |
||
| 468 | * |
||
| 469 | * @return resource Specifier for the target file |
||
| 470 | * |
||
| 471 | * @throws IOException |
||
| 472 | */ |
||
| 473 | protected function openFileForWriting($path) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. |
||
| 484 | * |
||
| 485 | * @param resource $handler The resource to write to |
||
| 486 | * @param string $content The content to write |
||
| 487 | * @param string $path The path to the file (for exception printing only) |
||
| 488 | * |
||
| 489 | * @throws IOException |
||
| 490 | */ |
||
| 491 | protected function writeToFile($handler, $content, $path = '') |
||
| 497 | } |
||
| 498 |
If you suppress an error, we recommend checking for the error condition explicitly: