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 | * Add data with path and content without doing any check. | ||
| 142 | * | ||
| 143 | * @param string $path File path to be minified. | ||
| 144 | * @param string $content Content to be minified. | ||
| 145 | * | ||
| 146 | * @return static | ||
| 147 | */ | ||
| 148 | 	public function addData( $path, $content ) { | ||
| 153 | |||
| 154 | /** | ||
| 155 | * Minify the data & (optionally) saves it to a file. | ||
| 156 | * | ||
| 157 | * @param string[optional] $path Path to write the data to | ||
| 158 | * | ||
| 159 | * @return string The minified data | ||
| 160 | */ | ||
| 161 | public function minify($path = null) | ||
| 172 | |||
| 173 | /** | ||
| 174 | * Minify & gzip the data & (optionally) saves it to a file. | ||
| 175 | * | ||
| 176 | * @param string[optional] $path Path to write the data to | ||
| 177 | * @param int[optional] $level Compression level, from 0 to 9 | ||
| 178 | * | ||
| 179 | * @return string The minified & gzipped data | ||
| 180 | */ | ||
| 181 | public function gzip($path = null, $level = 9) | ||
| 193 | |||
| 194 | /** | ||
| 195 | * Minify the data & write it to a CacheItemInterface object. | ||
| 196 | * | ||
| 197 | * @param CacheItemInterface $item Cache item to write the data to | ||
| 198 | * | ||
| 199 | * @return CacheItemInterface Cache item with the minifier data | ||
| 200 | */ | ||
| 201 | public function cache(CacheItemInterface $item) | ||
| 208 | |||
| 209 | /** | ||
| 210 | * Minify the data. | ||
| 211 | * | ||
| 212 | * @param string[optional] $path Path to write the data to | ||
| 213 | * | ||
| 214 | * @return string The minified data | ||
| 215 | */ | ||
| 216 | abstract public function execute($path = null); | ||
| 217 | |||
| 218 | /** | ||
| 219 | * Load data. | ||
| 220 | * | ||
| 221 | * @param string $data Either a path to a file or the content itself | ||
| 222 | * | ||
| 223 | * @return string | ||
| 224 | */ | ||
| 225 | protected function load($data) | ||
| 239 | |||
| 240 | /** | ||
| 241 | * Save to file. | ||
| 242 | * | ||
| 243 | * @param string $content The minified data | ||
| 244 | * @param string $path The path to save the minified data to | ||
| 245 | * | ||
| 246 | * @throws IOException | ||
| 247 | */ | ||
| 248 | protected function save($content, $path) | ||
| 256 | |||
| 257 | /** | ||
| 258 | * Register a pattern to execute against the source content. | ||
| 259 | * | ||
| 260 | * If $replacement is a string, it must be plain text. Placeholders like $1 or \2 don't work. | ||
| 261 | * If you need that functionality, use a callback instead. | ||
| 262 | * | ||
| 263 | * @param string $pattern PCRE pattern | ||
| 264 | * @param string|callable $replacement Replacement value for matched pattern | ||
| 265 | */ | ||
| 266 | protected function registerPattern($pattern, $replacement = '') | ||
| 273 | |||
| 274 | /** | ||
| 275 | * We can't "just" run some regular expressions against JavaScript: it's a | ||
| 276 | * complex language. E.g. having an occurrence of // xyz would be a comment, | ||
| 277 | * unless it's used within a string. Of you could have something that looks | ||
| 278 | * like a 'string', but inside a comment. | ||
| 279 | * The only way to accurately replace these pieces is to traverse the JS one | ||
| 280 | * character at a time and try to find whatever starts first. | ||
| 281 | * | ||
| 282 | * @param string $content The content to replace patterns in | ||
| 283 | * | ||
| 284 | * @return string The (manipulated) content | ||
| 285 | */ | ||
| 286 | protected function replace($content) | ||
| 355 | |||
| 356 | /** | ||
| 357 | * If $replacement is a callback, execute it, passing in the match data. | ||
| 358 | * If it's a string, just pass it through. | ||
| 359 | * | ||
| 360 | * @param string|callable $replacement Replacement value | ||
| 361 | * @param array $match Match data, in PREG_OFFSET_CAPTURE form | ||
| 362 | * | ||
| 363 | * @return string | ||
| 364 | */ | ||
| 365 | protected function executeReplacement($replacement, $match) | ||
| 376 | |||
| 377 | /** | ||
| 378 | * Strings are a pattern we need to match, in order to ignore potential | ||
| 379 | * code-like content inside them, but we just want all of the string | ||
| 380 | * content to remain untouched. | ||
| 381 | * | ||
| 382 | * This method will replace all string content with simple STRING# | ||
| 383 | * placeholder text, so we've rid all strings from characters that may be | ||
| 384 | * misinterpreted. Original string content will be saved in $this->extracted | ||
| 385 | * and after doing all other minifying, we can restore the original content | ||
| 386 | * via restoreStrings(). | ||
| 387 | * | ||
| 388 | * @param string[optional] $chars | ||
| 389 | * @param string[optional] $placeholderPrefix | ||
| 390 | */ | ||
| 391 | protected function extractStrings($chars = '\'"', $placeholderPrefix = '') | ||
| 428 | |||
| 429 | /** | ||
| 430 | * This method will restore all extracted data (strings, regexes) that were | ||
| 431 | * replaced with placeholder text in extract*(). The original content was | ||
| 432 | * saved in $this->extracted. | ||
| 433 | * | ||
| 434 | * @param string $content | ||
| 435 | * | ||
| 436 | * @return string | ||
| 437 | */ | ||
| 438 | protected function restoreExtractedData($content) | ||
| 451 | |||
| 452 | /** | ||
| 453 | * Check if the path is a regular file and can be read. | ||
| 454 | * | ||
| 455 | * @param string $path | ||
| 456 | * | ||
| 457 | * @return bool | ||
| 458 | */ | ||
| 459 | protected function canImportFile($path) | ||
| 473 | |||
| 474 | /** | ||
| 475 | * Attempts to open file specified by $path for writing. | ||
| 476 | * | ||
| 477 | * @param string $path The path to the file | ||
| 478 | * | ||
| 479 | * @return resource Specifier for the target file | ||
| 480 | * | ||
| 481 | * @throws IOException | ||
| 482 | */ | ||
| 483 | protected function openFileForWriting($path) | ||
| 491 | |||
| 492 | /** | ||
| 493 | * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. | ||
| 494 | * | ||
| 495 | * @param resource $handler The resource to write to | ||
| 496 | * @param string $content The content to write | ||
| 497 | * @param string $path The path to the file (for exception printing only) | ||
| 498 | * | ||
| 499 | * @throws IOException | ||
| 500 | */ | ||
| 501 | protected function writeToFile($handler, $content, $path = '') | ||
| 511 | } | ||
| 512 | 
If you suppress an error, we recommend checking for the error condition explicitly: