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 | public function __construct( $content ) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Reads the page and collects tags. |
||
| 33 | * |
||
| 34 | * @param array $options Options. |
||
| 35 | * |
||
| 36 | * @return bool |
||
| 37 | */ |
||
| 38 | abstract public function read( $options ); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Joins and optimizes collected things. |
||
| 42 | * |
||
| 43 | * @return bool |
||
| 44 | */ |
||
| 45 | abstract public function minify(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Caches the things. |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | abstract public function cache(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Returns the content |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | abstract public function getcontent(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Tranfsorms a given URL to a full local filepath if possible. |
||
| 63 | * Returns local filepath or false. |
||
| 64 | * |
||
| 65 | * @param string $url URL to transform. |
||
| 66 | * |
||
| 67 | * @return bool|string |
||
| 68 | */ |
||
| 69 | public function getpath( $url ) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns the hostname part of a given $url if we're able to parse it. |
||
| 164 | * If not, it returns the original url (prefixed with http:// scheme in case |
||
| 165 | * it was missing). |
||
| 166 | * Used as callback for WPML multidomains filter. |
||
| 167 | * |
||
| 168 | * @param string $url URL. |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | protected function get_url_hostname( $url ) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Hides everything between noptimize-comment tags. |
||
| 192 | * |
||
| 193 | * @param string $markup Markup to process. |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | protected function hide_noptimize( $markup ) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Unhide noptimize-tags. |
||
| 209 | * |
||
| 210 | * @param string $markup Markup to process. |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | protected function restore_noptimize( $markup ) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Hides "iehacks" content. |
||
| 221 | * |
||
| 222 | * @param string $markup Markup to process. |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | protected function hide_iehacks( $markup ) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Restores "hidden" iehacks content. |
||
| 238 | * |
||
| 239 | * @param string $markup Markup to process. |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | protected function restore_iehacks( $markup ) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * "Hides" content within HTML comments using a regex-based replacement |
||
| 250 | * if HTML comment markers are found. |
||
| 251 | * `<!--example-->` becomes `%%COMMENTS%%ZXhhbXBsZQ==%%COMMENTS%%` |
||
| 252 | * |
||
| 253 | * @param string $markup Markup to process. |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | protected function hide_comments( $markup ) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Restores original HTML comment markers inside a string whose HTML |
||
| 269 | * comments have been "hidden" by using `hide_comments()`. |
||
| 270 | * |
||
| 271 | * @param string $markup Markup to process. |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | protected function restore_comments( $markup ) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Replaces the given URL with the CDN-version of it when CDN replacement |
||
| 282 | * is supposed to be done. |
||
| 283 | * |
||
| 284 | * @param string $url URL to process. |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function url_replace_cdn( $url ) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Injects/replaces the given payload markup into `$this->content` |
||
| 332 | * at the specified location. |
||
| 333 | * If the specified tag cannot be found, the payload is appended into |
||
| 334 | * $this->content along with a warning wrapped inside <!--noptimize--> tags. |
||
| 335 | * |
||
| 336 | * @param string $payload Markup to inject. |
||
| 337 | * @param array $where Array specifying the tag name and method of injection. |
||
| 338 | * Index 0 is the tag name (i.e., `</body>`). |
||
| 339 | * Index 1 specifies ˛'before', 'after' or 'replace'. Defaults to 'before'. |
||
| 340 | * |
||
| 341 | * @return void |
||
| 342 | */ |
||
| 343 | protected function inject_in_html( $payload, $where ) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Returns true if given `$tag` is found in the list of `$removables`. |
||
| 378 | * |
||
| 379 | * @param string $tag Tag to search for. |
||
| 380 | * @param array $removables List of things considered completely removable. |
||
| 381 | * |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | protected function isremovable( $tag, $removables ) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Callback used in `self::inject_minified()`. |
||
| 397 | * |
||
| 398 | * @param array $matches Regex matches. |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function inject_minified_callback( $matches ) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Inject already minified code in optimized JS/CSS. |
||
| 480 | * |
||
| 481 | * @param string $in Markup. |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | protected function inject_minified( $in ) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Specialized method to create the INJECTLATER marker. |
||
| 502 | * These are somewhat "special", in the sense that they're additionally wrapped |
||
| 503 | * within an "exclamation mark style" comment, so that they're not stripped |
||
| 504 | * out by minifiers. |
||
| 505 | * They also currently contain the hash of the file's contents too (unlike other markers). |
||
| 506 | * |
||
| 507 | * @param string $filepath Filepath. |
||
| 508 | * @param string $hash Hash. |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | public static function build_injectlater_marker( $filepath, $hash ) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Creates and returns a `%%`-style named marker which holds |
||
| 521 | * the base64 encoded `$data`. |
||
| 522 | * If `$hash` is provided, it's appended to the base64 encoded string |
||
| 523 | * using `|` as the separator (in order to support building the |
||
| 524 | * somewhat special/different INJECTLATER marker). |
||
| 525 | * |
||
| 526 | * @param string $name Marker name. |
||
| 527 | * @param string $data Marker data which will be base64-encoded. |
||
| 528 | * @param string|null $hash Optional. |
||
| 529 | * |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | public static function build_marker( $name, $data, $hash = null ) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Searches for `$search` in `$content` (using either `preg_match()` |
||
| 550 | * or `strpos()`, depending on whether `$search` is a valid regex pattern or not). |
||
| 551 | * If something is found, it replaces `$content` using `$re_replace_pattern`, |
||
| 552 | * effectively creating our named markers (`%%{$marker}%%`. |
||
| 553 | * These are then at some point replaced back to their actual/original/modified |
||
| 554 | * contents using `autoptimizeBase::restore_marked_content()`. |
||
| 555 | * |
||
| 556 | * @param string $marker Marker name (without percent characters). |
||
| 557 | * @param string $search A string or full blown regex pattern to search for in $content. Uses `strpos()` or `preg_match()`. |
||
| 558 | * @param string $re_replace_pattern Regex pattern to use when replacing contents. |
||
| 559 | * @param string $content Content to work on. |
||
| 560 | * |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | protected function replace_contents_with_marker_if_exists( $marker, $search, $re_replace_pattern, $content ) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Complements `autoptimizeBase::replace_contents_with_marker_if_exists()`. |
||
| 589 | * |
||
| 590 | * @param string $marker Marker. |
||
| 591 | * @param string $content Markup. |
||
| 592 | * |
||
| 593 | * @return string |
||
| 594 | */ |
||
| 595 | protected function restore_marked_content( $marker, $content ) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Logs given `$data` for debugging purposes (when debug logging is on). |
||
| 612 | * |
||
| 613 | * @param mixed $data Data to log. |
||
| 614 | * |
||
| 615 | * @return void |
||
| 616 | */ |
||
| 617 | protected function debug_log( $data ) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Checks if a single local css/js file can be minified and returns source if so. |
||
| 632 | * |
||
| 633 | * @param string $filepath Filepath. |
||
| 634 | * |
||
| 635 | * @return bool|string to be minified code or false. |
||
| 636 | */ |
||
| 637 | protected function prepare_minify_single( $filepath ) |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Given an autoptimizeCache instance returns the (maybe cdn-ed) url of |
||
| 669 | * the cached file. |
||
| 670 | * |
||
| 671 | * @param autoptimizeCache $cache autoptimizeCache instance. |
||
| 672 | * |
||
| 673 | * @return string |
||
| 674 | */ |
||
| 675 | protected function build_minify_single_url( autoptimizeCache $cache ) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Returns true if given $str ends with given $test. |
||
| 687 | * |
||
| 688 | * @param string $str String to check. |
||
| 689 | * @param string $test Ending to match. |
||
| 690 | * |
||
| 691 | * @return bool |
||
| 692 | */ |
||
| 693 | protected function str_ends_in( $str, $test ) |
||
| 704 | } |
||
| 705 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: