Complex classes like autoptimizeBase 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 autoptimizeBase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | abstract class autoptimizeBase |
||
11 | { |
||
12 | /** |
||
13 | * Holds content being processed (html, scripts, styles) |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $content = ''; |
||
18 | |||
19 | /** |
||
20 | * Controls debug logging. |
||
21 | * |
||
22 | * @var bool |
||
23 | */ |
||
24 | public $debug_log = false; |
||
25 | |||
26 | /** |
||
27 | * Initiated $cdn_url. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | public $cdn_url = ''; |
||
32 | |||
33 | public function __construct( $content ) |
||
37 | |||
38 | /** |
||
39 | * Reads the page and collects tags. |
||
40 | * |
||
41 | * @param array $options Options. |
||
42 | * |
||
43 | * @return bool |
||
44 | */ |
||
45 | abstract public function read( $options ); |
||
46 | |||
47 | /** |
||
48 | * Joins and optimizes collected things. |
||
49 | * |
||
50 | * @return bool |
||
51 | */ |
||
52 | abstract public function minify(); |
||
53 | |||
54 | /** |
||
55 | * Caches the things. |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | abstract public function cache(); |
||
60 | |||
61 | /** |
||
62 | * Returns the content |
||
63 | * |
||
64 | * @return string |
||
65 | */ |
||
66 | abstract public function getcontent(); |
||
67 | |||
68 | /** |
||
69 | * Tranfsorms a given URL to a full local filepath if possible. |
||
70 | * Returns local filepath or false. |
||
71 | * |
||
72 | * @param string $url URL to transform. |
||
73 | * |
||
74 | * @return bool|string |
||
75 | */ |
||
76 | public function getpath( $url ) |
||
176 | |||
177 | /** |
||
178 | * Returns the hostname part of a given $url if we're able to parse it. |
||
179 | * If not, it returns the original url (prefixed with http:// scheme in case |
||
180 | * it was missing). |
||
181 | * Used as callback for WPML multidomains filter. |
||
182 | * |
||
183 | * @param string $url URL. |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | protected function get_url_hostname( $url ) |
||
204 | |||
205 | /** |
||
206 | * Hides everything between noptimize-comment tags. |
||
207 | * |
||
208 | * @param string $markup Markup to process. |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | protected function hide_noptimize( $markup ) |
||
221 | |||
222 | /** |
||
223 | * Unhide noptimize-tags. |
||
224 | * |
||
225 | * @param string $markup Markup to process. |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | protected function restore_noptimize( $markup ) |
||
233 | |||
234 | /** |
||
235 | * Hides "iehacks" content. |
||
236 | * |
||
237 | * @param string $markup Markup to process. |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | protected function hide_iehacks( $markup ) |
||
250 | |||
251 | /** |
||
252 | * Restores "hidden" iehacks content. |
||
253 | * |
||
254 | * @param string $markup Markup to process. |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | protected function restore_iehacks( $markup ) |
||
262 | |||
263 | /** |
||
264 | * "Hides" content within HTML comments using a regex-based replacement |
||
265 | * if HTML comment markers are found. |
||
266 | * `<!--example-->` becomes `%%COMMENTS%%ZXhhbXBsZQ==%%COMMENTS%%` |
||
267 | * |
||
268 | * @param string $markup Markup to process. |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | protected function hide_comments( $markup ) |
||
281 | |||
282 | /** |
||
283 | * Restores original HTML comment markers inside a string whose HTML |
||
284 | * comments have been "hidden" by using `hide_comments()`. |
||
285 | * |
||
286 | * @param string $markup Markup to process. |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | protected function restore_comments( $markup ) |
||
294 | |||
295 | /** |
||
296 | * Replaces the given URL with the CDN-version of it when CDN replacement |
||
297 | * is supposed to be done. |
||
298 | * |
||
299 | * @param string $url URL to process. |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | public function url_replace_cdn( $url ) |
||
344 | |||
345 | /** |
||
346 | * Injects/replaces the given payload markup into `$this->content` |
||
347 | * at the specified location. |
||
348 | * If the specified tag cannot be found, the payload is appended into |
||
349 | * $this->content along with a warning wrapped inside <!--noptimize--> tags. |
||
350 | * |
||
351 | * @param string $payload Markup to inject. |
||
352 | * @param array $where Array specifying the tag name and method of injection. |
||
353 | * Index 0 is the tag name (i.e., `</body>`). |
||
354 | * Index 1 specifies ˛'before', 'after' or 'replace'. Defaults to 'before'. |
||
355 | * |
||
356 | * @return void |
||
357 | */ |
||
358 | protected function inject_in_html( $payload, $where ) |
||
390 | |||
391 | /** |
||
392 | * Returns true if given `$tag` is found in the list of `$removables`. |
||
393 | * |
||
394 | * @param string $tag Tag to search for. |
||
395 | * @param array $removables List of things considered completely removable. |
||
396 | * |
||
397 | * @return bool |
||
398 | */ |
||
399 | protected function isremovable( $tag, $removables ) |
||
409 | |||
410 | /** |
||
411 | * Callback used in `self::inject_minified()`. |
||
412 | * |
||
413 | * @param array $matches Regex matches. |
||
414 | * |
||
415 | * @return string |
||
416 | */ |
||
417 | public function inject_minified_callback( $matches ) |
||
492 | |||
493 | /** |
||
494 | * Inject already minified code in optimized JS/CSS. |
||
495 | * |
||
496 | * @param string $in Markup. |
||
497 | * |
||
498 | * @return string |
||
499 | */ |
||
500 | protected function inject_minified( $in ) |
||
513 | |||
514 | /** |
||
515 | * Specialized method to create the INJECTLATER marker. |
||
516 | * These are somewhat "special", in the sense that they're additionally wrapped |
||
517 | * within an "exclamation mark style" comment, so that they're not stripped |
||
518 | * out by minifiers. |
||
519 | * They also currently contain the hash of the file's contents too (unlike other markers). |
||
520 | * |
||
521 | * @param string $filepath Filepath. |
||
522 | * @param string $hash Hash. |
||
523 | * |
||
524 | * @return string |
||
525 | */ |
||
526 | public static function build_injectlater_marker( $filepath, $hash ) |
||
532 | |||
533 | /** |
||
534 | * Creates and returns a `%%`-style named marker which holds |
||
535 | * the base64 encoded `$data`. |
||
536 | * If `$hash` is provided, it's appended to the base64 encoded string |
||
537 | * using `|` as the separator (in order to support building the |
||
538 | * somewhat special/different INJECTLATER marker). |
||
539 | * |
||
540 | * @param string $name Marker name. |
||
541 | * @param string $data Marker data which will be base64-encoded. |
||
542 | * @param string|null $hash Optional. |
||
543 | * |
||
544 | * @return string |
||
545 | */ |
||
546 | public static function build_marker( $name, $data, $hash = null ) |
||
561 | |||
562 | /** |
||
563 | * Searches for `$search` in `$content` (using either `preg_match()` |
||
564 | * or `strpos()`, depending on whether `$search` is a valid regex pattern or not). |
||
565 | * If something is found, it replaces `$content` using `$re_replace_pattern`, |
||
566 | * effectively creating our named markers (`%%{$marker}%%`. |
||
567 | * These are then at some point replaced back to their actual/original/modified |
||
568 | * contents using `autoptimizeBase::restore_marked_content()`. |
||
569 | * |
||
570 | * @param string $marker Marker name (without percent characters). |
||
571 | * @param string $search A string or full blown regex pattern to search for in $content. Uses `strpos()` or `preg_match()`. |
||
572 | * @param string $re_replace_pattern Regex pattern to use when replacing contents. |
||
573 | * @param string $content Content to work on. |
||
574 | * |
||
575 | * @return string |
||
576 | */ |
||
577 | public static function replace_contents_with_marker_if_exists( $marker, $search, $re_replace_pattern, $content ) |
||
600 | |||
601 | /** |
||
602 | * Complements `autoptimizeBase::replace_contents_with_marker_if_exists()`. |
||
603 | * |
||
604 | * @param string $marker Marker. |
||
605 | * @param string $content Markup. |
||
606 | * |
||
607 | * @return string |
||
608 | */ |
||
609 | public static function restore_marked_content( $marker, $content ) |
||
623 | |||
624 | /** |
||
625 | * Logs given `$data` for debugging purposes (when debug logging is on). |
||
626 | * |
||
627 | * @param mixed $data Data to log. |
||
628 | * |
||
629 | * @return void |
||
630 | */ |
||
631 | protected function debug_log( $data ) |
||
643 | |||
644 | /** |
||
645 | * Checks if a single local css/js file can be minified and returns source if so. |
||
646 | * |
||
647 | * @param string $filepath Filepath. |
||
648 | * |
||
649 | * @return bool|string to be minified code or false. |
||
650 | */ |
||
651 | protected function prepare_minify_single( $filepath ) |
||
680 | |||
681 | /** |
||
682 | * Given an autoptimizeCache instance returns the (maybe cdn-ed) url of |
||
683 | * the cached file. |
||
684 | * |
||
685 | * @param autoptimizeCache $cache autoptimizeCache instance. |
||
686 | * |
||
687 | * @return string |
||
688 | */ |
||
689 | protected function build_minify_single_url( autoptimizeCache $cache ) |
||
698 | } |
||
699 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.