Complex classes like WordAds 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 WordAds, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class WordAds { |
||
| 15 | |||
| 16 | public $params = null; |
||
| 17 | |||
| 18 | public $ads = array(); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Array of supported ad types. |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | public static $ad_tag_ids = array( |
||
| 26 | 'mrec' => array( |
||
| 27 | 'tag' => '300x250_mediumrectangle', |
||
| 28 | 'height' => '250', |
||
| 29 | 'width' => '300', |
||
| 30 | ), |
||
| 31 | 'leaderboard' => array( |
||
| 32 | 'tag' => '728x90_leaderboard', |
||
| 33 | 'height' => '90', |
||
| 34 | 'width' => '728', |
||
| 35 | ), |
||
| 36 | 'mobile_leaderboard' => array( |
||
| 37 | 'tag' => '320x50_mobileleaderboard', |
||
| 38 | 'height' => '50', |
||
| 39 | 'width' => '320', |
||
| 40 | ), |
||
| 41 | 'wideskyscraper' => array( |
||
| 42 | 'tag' => '160x600_wideskyscraper', |
||
| 43 | 'height' => '600', |
||
| 44 | 'width' => '160', |
||
| 45 | ), |
||
| 46 | ); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Mapping array of location slugs to placement ids |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | public static $ad_location_ids = array( |
||
| 54 | 'top' => 110, |
||
| 55 | 'belowpost' => 120, |
||
| 56 | 'belowpost2' => 130, |
||
| 57 | 'sidebar' => 140, |
||
| 58 | 'widget' => 150, |
||
| 59 | 'gutenberg' => 200, |
||
| 60 | 'inline' => 310, |
||
| 61 | 'inline-plugin' => 320, |
||
| 62 | ); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Counter to enable unique, sequential section IDs for all amp-ad units |
||
| 66 | * |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | public static $amp_section_id = 1; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Checks for AMP support and returns true iff active & AMP request |
||
| 73 | * @return boolean True if supported AMP request |
||
| 74 | * |
||
| 75 | * @since 7.5.0 |
||
| 76 | */ |
||
| 77 | public static function is_amp() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Increment the AMP section ID and return the value |
||
| 83 | * |
||
| 84 | * @return int |
||
| 85 | */ |
||
| 86 | public static function get_amp_section_id() { |
||
| 89 | |||
| 90 | public static $SOLO_UNIT_CSS = 'float:left;margin-right:5px;margin-top:0px;'; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Convenience function for grabbing options from params->options |
||
| 94 | * |
||
| 95 | * @param string $option the option to grab |
||
| 96 | * @param mixed $default (optional) |
||
| 97 | * @return option or $default if not set |
||
| 98 | * |
||
| 99 | * @since 4.5.0 |
||
| 100 | */ |
||
| 101 | function option( $option, $default = false ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns the ad tag property array for supported ad types. |
||
| 111 | * @return array array with ad tags |
||
| 112 | * |
||
| 113 | * @since 7.1.0 |
||
| 114 | */ |
||
| 115 | function get_ad_tags() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns the solo css for unit |
||
| 121 | * @return string the special css for solo units |
||
| 122 | * |
||
| 123 | * @since 7.1.0 |
||
| 124 | */ |
||
| 125 | function get_solo_unit_css() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Instantiate the plugin |
||
| 131 | * |
||
| 132 | * @since 4.5.0 |
||
| 133 | */ |
||
| 134 | function __construct() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Code to run on WordPress 'init' hook |
||
| 141 | * |
||
| 142 | * @since 4.5.0 |
||
| 143 | */ |
||
| 144 | function init() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Check for Jetpack's The_Neverending_Home_Page and use got_infinity |
||
| 185 | * |
||
| 186 | * @return boolean true if load came from infinite scroll |
||
| 187 | * |
||
| 188 | * @since 4.5.0 |
||
| 189 | */ |
||
| 190 | public static function is_infinite_scroll() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Add the actions/filters to insert the ads. Checks for mobile or desktop. |
||
| 196 | * |
||
| 197 | * @since 4.5.0 |
||
| 198 | */ |
||
| 199 | private function insert_adcode() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Register desktop scripts and styles |
||
| 255 | * |
||
| 256 | * @since 4.5.0 |
||
| 257 | */ |
||
| 258 | function enqueue_scripts() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * IPONWEB metadata used by the various scripts |
||
| 269 | * |
||
| 270 | * @return [type] [description] |
||
|
|
|||
| 271 | */ |
||
| 272 | function insert_head_meta() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * IPONWEB scripts in <head> |
||
| 294 | * |
||
| 295 | * @since 4.5.0 |
||
| 296 | */ |
||
| 297 | function insert_head_iponweb() { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Insert the ad onto the page |
||
| 323 | * |
||
| 324 | * @since 4.5.0 |
||
| 325 | */ |
||
| 326 | function insert_ad( $content ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Insert an inline ad into a post content |
||
| 351 | * Used for rendering the `wordads` shortcode. |
||
| 352 | * |
||
| 353 | * @since 6.1.0 |
||
| 354 | */ |
||
| 355 | function insert_inline_ad( $content ) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Inserts ad into header |
||
| 381 | * |
||
| 382 | * @since 4.5.0 |
||
| 383 | */ |
||
| 384 | function insert_header_ad() { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Special cases for inserting header unit via jQuery |
||
| 404 | * |
||
| 405 | * @since 4.5.0 |
||
| 406 | */ |
||
| 407 | function insert_header_ad_special() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Header unit for AMP |
||
| 447 | * |
||
| 448 | * @param string $content Content of the page. |
||
| 449 | * |
||
| 450 | * @since 7.5.0 |
||
| 451 | */ |
||
| 452 | public function insert_header_ad_amp( $content ) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Filter the latest ads.txt to include custom user entries. Strips any tags or whitespace. |
||
| 464 | * |
||
| 465 | * @param string $adstxt The ads.txt being filtered |
||
| 466 | * @return string Filtered ads.txt with custom entries, if applicable |
||
| 467 | * |
||
| 468 | * @since 6.5.0 |
||
| 469 | */ |
||
| 470 | function insert_custom_adstxt( $adstxt ) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Get the ad for the spot and type. |
||
| 482 | * |
||
| 483 | * @param string $spot top, side, inline, or belowpost |
||
| 484 | * @param string $type iponweb or adsense |
||
| 485 | */ |
||
| 486 | function get_ad( $spot, $type = 'iponweb' ) { |
||
| 531 | |||
| 532 | |||
| 533 | /** |
||
| 534 | * Returns the snippet to be inserted into the ad unit |
||
| 535 | * |
||
| 536 | * @param int $section_id |
||
| 537 | * @param int $height |
||
| 538 | * @param int $width |
||
| 539 | * @param int $location |
||
| 540 | * @param string $css |
||
| 541 | * @return string |
||
| 542 | * |
||
| 543 | * @since 5.7 |
||
| 544 | */ |
||
| 545 | public function get_ad_snippet( $section_id, $height, $width, $location = '', $css = '' ) { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Returns the complete ad div with snippet to be inserted into the page |
||
| 596 | * |
||
| 597 | * @param string $spot top, side, inline, or belowpost |
||
| 598 | * @param string $snippet The snippet to insert into the div |
||
| 599 | * @param array $css_classes |
||
| 600 | * @return string The supporting ad unit div |
||
| 601 | * |
||
| 602 | * @since 7.1 |
||
| 603 | */ |
||
| 604 | function get_ad_div( $spot, $snippet, array $css_classes = array() ) { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Check the reasons to bail before we attempt to insert ads. |
||
| 631 | * |
||
| 632 | * @return true if we should bail (don't insert ads) |
||
| 633 | * |
||
| 634 | * @since 4.5.0 |
||
| 635 | */ |
||
| 636 | public function should_bail() { |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Returns markup for HTML5 house ad base on unit |
||
| 642 | * |
||
| 643 | * @param string $unit mrec, widesky, or leaderboard |
||
| 644 | * @return string markup for HTML5 house ad |
||
| 645 | * |
||
| 646 | * @since 4.7.0 |
||
| 647 | */ |
||
| 648 | public function get_house_ad( $unit = 'mrec' ) { |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Activation hook actions |
||
| 681 | * |
||
| 682 | * @since 4.5.0 |
||
| 683 | */ |
||
| 684 | public static function activate() { |
||
| 687 | } |
||
| 688 | |||
| 695 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.