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 |
||
27 | abstract class Minify |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * Default Minify Options list |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | public $options = array( |
||
36 | 'css-strip-comments' => true, |
||
37 | 'css-strip-whitespace' => true, |
||
38 | 'css-shorten-hex' => true, |
||
39 | 'css-shorten-zeroes' => true, |
||
40 | 'css-shorten-font-weights' => true, |
||
41 | 'css-strip-empty-tags' => true, |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * Set Minify options |
||
46 | * useful for class customizations. |
||
47 | * |
||
48 | * @param $key |
||
49 | * @param null $value |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function setOptions($key, $value = null) |
||
65 | |||
66 | /** |
||
67 | * The data to be minified. |
||
68 | * |
||
69 | * @var string[] |
||
70 | */ |
||
71 | protected $data = array(); |
||
72 | |||
73 | /** |
||
74 | * Array of patterns to match. |
||
75 | * |
||
76 | * @var string[] |
||
77 | */ |
||
78 | protected $patterns = array(); |
||
79 | |||
80 | /** |
||
81 | * This array will hold content of strings and regular expressions that have |
||
82 | * been extracted from the JS source code, so we can reliably match "code", |
||
83 | * without having to worry about potential "code-like" characters inside. |
||
84 | * |
||
85 | * @var string[] |
||
86 | */ |
||
87 | public $extracted = array(); |
||
88 | |||
89 | /** |
||
90 | * Init the minify class - optionally, code may be passed along already. |
||
91 | */ |
||
92 | public function __construct(/* $data = null, ... */) |
||
99 | |||
100 | /** |
||
101 | * Add a file or straight-up code to be minified. |
||
102 | * |
||
103 | * @param string|string[] $data |
||
104 | * |
||
105 | * @return static |
||
106 | */ |
||
107 | public function add($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 | * @throws IOException |
||
147 | */ |
||
148 | public function minify($path = null) |
||
159 | |||
160 | /** |
||
161 | * Minify & gzip the data & (optionally) saves it to a file. |
||
162 | * |
||
163 | * @param string[optional] $path Path to write the data to |
||
164 | * @param int $level |
||
165 | * @return string The minified & gzipped data |
||
166 | * @throws IOException |
||
167 | */ |
||
168 | public function gzip($path = null, $level = 9) |
||
180 | |||
181 | /** |
||
182 | * Minify the data & write it to a CacheItemInterface object. |
||
183 | * |
||
184 | * @param CacheItemInterface $item Cache item to write the data to |
||
185 | * |
||
186 | * @return CacheItemInterface Cache item with the minifier data |
||
187 | */ |
||
188 | public function cache(CacheItemInterface $item) |
||
195 | |||
196 | /** |
||
197 | * Minify the data. |
||
198 | * |
||
199 | * @param string[optional] $path Path to write the data to |
||
200 | * |
||
201 | * @return string The minified data |
||
202 | */ |
||
203 | abstract public function execute($path = null); |
||
204 | |||
205 | /** |
||
206 | * Load data. |
||
207 | * |
||
208 | * @param string $data Either a path to a file or the content itself |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | protected function load($data) |
||
226 | |||
227 | /** |
||
228 | * Save to file. |
||
229 | * |
||
230 | * @param string $content The minified data |
||
231 | * @param string $path The path to save the minified data to |
||
232 | * |
||
233 | * @throws IOException |
||
234 | */ |
||
235 | protected function save($content, $path) |
||
243 | |||
244 | /** |
||
245 | * Register a pattern to execute against the source content. |
||
246 | * |
||
247 | * @param string $pattern PCRE pattern |
||
248 | * @param string|callable $replacement Replacement value for matched pattern |
||
249 | */ |
||
250 | protected function registerPattern($pattern, $replacement = '') |
||
257 | |||
258 | /** |
||
259 | * We can't "just" run some regular expressions against JavaScript: it's a |
||
260 | * complex language. E.g. having an occurrence of // xyz would be a comment, |
||
261 | * unless it's used within a string. Of you could have something that looks |
||
262 | * like a 'string', but inside a comment. |
||
263 | * The only way to accurately replace these pieces is to traverse the JS one |
||
264 | * character at a time and try to find whatever starts first. |
||
265 | * |
||
266 | * @param string $content The content to replace patterns in |
||
267 | * |
||
268 | * @return string The (manipulated) content |
||
269 | */ |
||
270 | protected function replace($content) |
||
341 | |||
342 | /** |
||
343 | * This is where a pattern is matched against $content and the matches |
||
344 | * are replaced by their respective value. |
||
345 | * This function will be called plenty of times, where $content will always |
||
346 | * move up 1 character. |
||
347 | * |
||
348 | * @param string $pattern Pattern to match |
||
349 | * @param string|callable $replacement Replacement value |
||
350 | * @param string $content Content to match pattern against |
||
351 | * |
||
352 | * @return string |
||
353 | */ |
||
354 | protected function replacePattern($pattern, $replacement, $content) |
||
362 | |||
363 | /** |
||
364 | * Strings are a pattern we need to match, in order to ignore potential |
||
365 | * code-like content inside them, but we just want all of the string |
||
366 | * content to remain untouched. |
||
367 | * |
||
368 | * This method will replace all string content with simple STRING# |
||
369 | * placeholder text, so we've rid all strings from characters that may be |
||
370 | * misinterpreted. Original string content will be saved in $this->extracted |
||
371 | * and after doing all other minifying, we can restore the original content |
||
372 | * via restoreStrings(). |
||
373 | * |
||
374 | * @param string[optional] $chars |
||
375 | * @param string[optional] $placeholderPrefix |
||
376 | */ |
||
377 | protected function extractStrings($chars = '\'"', $placeholderPrefix = '') |
||
414 | |||
415 | /** |
||
416 | * This method will restore all extracted data (strings, regexes) that were |
||
417 | * replaced with placeholder text in extract*(). The original content was |
||
418 | * saved in $this->extracted. |
||
419 | * |
||
420 | * @param string $content |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | protected function restoreExtractedData($content) |
||
437 | |||
438 | /** |
||
439 | * Check if the path is a regular file and can be read. |
||
440 | * |
||
441 | * @param string $path |
||
442 | * |
||
443 | * @return bool |
||
444 | */ |
||
445 | protected function canImportFile($path) |
||
459 | |||
460 | /** |
||
461 | * Attempts to open file specified by $path for writing. |
||
462 | * |
||
463 | * @param string $path The path to the file |
||
464 | * |
||
465 | * @return resource Specifier for the target file |
||
466 | * |
||
467 | * @throws IOException |
||
468 | */ |
||
469 | protected function openFileForWriting($path) |
||
477 | |||
478 | /** |
||
479 | * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. |
||
480 | * |
||
481 | * @param resource $handler The resource to write to |
||
482 | * @param string $content The content to write |
||
483 | * @param string $path The path to the file (for exception printing only) |
||
484 | * |
||
485 | * @throws IOException |
||
486 | */ |
||
487 | protected function writeToFile($handler, $content, $path = '') |
||
493 | } |
||
494 |
If you suppress an error, we recommend checking for the error condition explicitly: