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 | * If $replacement is a string, it must be plain text. Placeholders like $1 or \2 don't work. |
||
247 | * If you need that functionality, use a callback instead. |
||
248 | * |
||
249 | * @param string $pattern PCRE pattern |
||
250 | * @param string|callable $replacement Replacement value for matched pattern |
||
251 | */ |
||
252 | protected function registerPattern($pattern, $replacement = '') |
||
259 | |||
260 | /** |
||
261 | * We can't "just" run some regular expressions against JavaScript: it's a |
||
262 | * complex language. E.g. having an occurrence of // xyz would be a comment, |
||
263 | * unless it's used within a string. Of you could have something that looks |
||
264 | * like a 'string', but inside a comment. |
||
265 | * The only way to accurately replace these pieces is to traverse the JS one |
||
266 | * character at a time and try to find whatever starts first. |
||
267 | * |
||
268 | * @param string $content The content to replace patterns in |
||
269 | * |
||
270 | * @return string The (manipulated) content |
||
271 | */ |
||
272 | protected function replace($content) |
||
341 | |||
342 | /** |
||
343 | * If $replacement is a callback, execute it, passing in the match data. |
||
344 | * If it's a string, just pass it through. |
||
345 | * |
||
346 | * @param string|callable $replacement Replacement value |
||
347 | * @param array $match Match data, in PREG_OFFSET_CAPTURE form |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | protected function executeReplacement($replacement, $match) |
||
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 = '') |
||
497 | } |
||
498 |
If you suppress an error, we recommend checking for the error condition explicitly: