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 |
||
| 24 | class WordAds { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Ads parameters. |
||
| 28 | * |
||
| 29 | * @var null |
||
| 30 | */ |
||
| 31 | public $params = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Ads. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | public $ads = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Array of supported ad types. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | public static $ad_tag_ids = array( |
||
| 46 | 'mrec' => array( |
||
| 47 | 'tag' => '300x250_mediumrectangle', |
||
| 48 | 'height' => '250', |
||
| 49 | 'width' => '300', |
||
| 50 | ), |
||
| 51 | 'leaderboard' => array( |
||
| 52 | 'tag' => '728x90_leaderboard', |
||
| 53 | 'height' => '90', |
||
| 54 | 'width' => '728', |
||
| 55 | ), |
||
| 56 | 'mobile_leaderboard' => array( |
||
| 57 | 'tag' => '320x50_mobileleaderboard', |
||
| 58 | 'height' => '50', |
||
| 59 | 'width' => '320', |
||
| 60 | ), |
||
| 61 | 'wideskyscraper' => array( |
||
| 62 | 'tag' => '160x600_wideskyscraper', |
||
| 63 | 'height' => '600', |
||
| 64 | 'width' => '160', |
||
| 65 | ), |
||
| 66 | ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Mapping array of location slugs to placement ids |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | public static $ad_location_ids = array( |
||
| 74 | 'top' => 110, |
||
| 75 | 'belowpost' => 120, |
||
| 76 | 'belowpost2' => 130, |
||
| 77 | 'sidebar' => 140, |
||
| 78 | 'widget' => 150, |
||
| 79 | 'gutenberg' => 200, |
||
| 80 | 'inline' => 310, |
||
| 81 | 'inline-plugin' => 320, |
||
| 82 | ); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Mapping array of form factor slugs to form factor ids |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | public static $form_factor_ids = array( |
||
| 90 | 'square' => '001', // 250x250 |
||
| 91 | 'leaderboard' => '002', // 728x90 |
||
| 92 | 'skyscraper' => '003', // 120x600 |
||
| 93 | ); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Counter to enable unique, sequential section IDs for all amp-ad units |
||
| 97 | * |
||
| 98 | * @var int |
||
| 99 | */ |
||
| 100 | public static $amp_section_id = 1; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Solo unit CSS string. |
||
| 104 | * |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | public static $solo_unit_css = 'float:left;margin-right:5px;margin-top:0px;'; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Checks for AMP support and returns true iff active & AMP request |
||
| 111 | * |
||
| 112 | * @return boolean True if supported AMP request |
||
| 113 | * |
||
| 114 | * @since 7.5.0 |
||
| 115 | */ |
||
| 116 | public static function is_amp() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Increment the AMP section ID and return the value |
||
| 122 | * |
||
| 123 | * @return int |
||
| 124 | */ |
||
| 125 | public static function get_amp_section_id() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Convenience function for grabbing options from params->options |
||
| 131 | * |
||
| 132 | * @param string $option the option to grab. |
||
| 133 | * @param mixed $default (optional). |
||
| 134 | * @return option or $default if not set |
||
| 135 | * |
||
| 136 | * @since 4.5.0 |
||
| 137 | */ |
||
| 138 | public function option( $option, $default = false ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns the ad tag property array for supported ad types. |
||
| 148 | * |
||
| 149 | * @return array array with ad tags |
||
| 150 | * |
||
| 151 | * @since 7.1.0 |
||
| 152 | */ |
||
| 153 | public function get_ad_tags() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Returns the solo css for unit |
||
| 159 | * |
||
| 160 | * @return string the special css for solo units |
||
| 161 | * |
||
| 162 | * @since 7.1.0 |
||
| 163 | */ |
||
| 164 | public function get_solo_unit_css() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Instantiate the plugin |
||
| 170 | * |
||
| 171 | * @since 4.5.0 |
||
| 172 | */ |
||
| 173 | public function __construct() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Code to run on WordPress 'init' hook |
||
| 185 | * |
||
| 186 | * @since 4.5.0 |
||
| 187 | */ |
||
| 188 | public function init() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Check for Jetpack's The_Neverending_Home_Page and use got_infinity |
||
| 237 | * |
||
| 238 | * @return boolean true if load came from infinite scroll |
||
| 239 | * |
||
| 240 | * @since 4.5.0 |
||
| 241 | */ |
||
| 242 | public static function is_infinite_scroll() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Add the actions/filters to insert the ads. Checks for mobile or desktop. |
||
| 248 | * |
||
| 249 | * @since 4.5.0 |
||
| 250 | */ |
||
| 251 | private function insert_adcode() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Register desktop scripts and styles |
||
| 308 | * |
||
| 309 | * @since 4.5.0 |
||
| 310 | */ |
||
| 311 | public function enqueue_scripts() { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Add the IPW resource hints |
||
| 322 | * |
||
| 323 | * @since 7.9 |
||
| 324 | * |
||
| 325 | * @param array $hints Domains for hinting. |
||
| 326 | * @param string $relation_type Resource type. |
||
| 327 | * |
||
| 328 | * @return array Domains for hinting. |
||
| 329 | */ |
||
| 330 | public function resource_hints( $hints, $relation_type ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * IPONWEB metadata used by the various scripts |
||
| 356 | * |
||
| 357 | * @return [type] [description] |
||
| 358 | */ |
||
| 359 | public function insert_head_meta() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * IPONWEB scripts in <head> |
||
| 381 | * |
||
| 382 | * @since 4.5.0 |
||
| 383 | */ |
||
| 384 | public function insert_head_iponweb() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Insert the ad onto the page |
||
| 402 | * |
||
| 403 | * @since 4.5.0 |
||
| 404 | * |
||
| 405 | * @param string $content HTML content. |
||
| 406 | */ |
||
| 407 | public function insert_ad( $content ) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Insert an inline ad into a post content |
||
| 432 | * Used for rendering the `wordads` shortcode. |
||
| 433 | * |
||
| 434 | * @since 6.1.0 |
||
| 435 | * |
||
| 436 | * @param string $content HTML content. |
||
| 437 | */ |
||
| 438 | public function insert_inline_ad( $content ) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Inserts ad into header |
||
| 464 | * |
||
| 465 | * @since 4.5.0 |
||
| 466 | */ |
||
| 467 | public function insert_header_ad() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Special cases for inserting header unit via JS |
||
| 487 | * |
||
| 488 | * @since 4.5.0 |
||
| 489 | */ |
||
| 490 | public function insert_header_ad_special() { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Header unit for AMP |
||
| 524 | * |
||
| 525 | * @param string $content Content of the page. |
||
| 526 | * |
||
| 527 | * @since 7.5.0 |
||
| 528 | */ |
||
| 529 | public function insert_header_ad_amp( $content ) { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Filter the latest ads.txt to include custom user entries. Strips any tags or whitespace. |
||
| 541 | * |
||
| 542 | * @param string $adstxt The ads.txt being filtered. |
||
| 543 | * @return string Filtered ads.txt with custom entries, if applicable. |
||
| 544 | * |
||
| 545 | * @since 6.5.0 |
||
| 546 | */ |
||
| 547 | public function insert_custom_adstxt( $adstxt ) { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get the ad for the spot and type. |
||
| 563 | * |
||
| 564 | * @param string $spot top, side, inline, or belowpost. |
||
| 565 | * @param string $type iponweb or adsense. |
||
| 566 | */ |
||
| 567 | public function get_ad( $spot, $type = 'iponweb' ) { |
||
| 568 | $snippet = ''; |
||
| 569 | if ( 'iponweb' === $type ) { |
||
| 570 | $section_id = WORDADS_API_TEST_ID; |
||
| 571 | $snippet = ''; |
||
| 572 | |||
| 573 | if ( 'top' === $spot ) { |
||
| 574 | // mrec for mobile, leaderboard for desktop. |
||
| 575 | $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2'; |
||
| 576 | $form_factor = $this->params->mobile_device ? 'square' : 'leaderboard'; |
||
| 577 | $snippet = $this->get_dynamic_ad_snippet( $section_id, $form_factor, $spot ); |
||
| 578 | } elseif ( 'belowpost' === $spot ) { |
||
| 579 | $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1'; |
||
| 580 | $snippet = $this->get_dynamic_ad_snippet( $section_id, 'square', $spot ); |
||
| 581 | } elseif ( 'inline' === $spot ) { |
||
| 582 | $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '5'; |
||
| 583 | $snippet = $this->get_dynamic_ad_snippet( $section_id, 'square', $spot ); |
||
| 584 | } elseif ( 'top_amp' === $spot ) { |
||
| 585 | // Ad unit which can safely be inserted below title, above content in a variety of themes. |
||
| 586 | $width = 300; |
||
| 587 | $height = 250; |
||
| 588 | $snippet = $this->get_ad_div( $spot, $this->get_amp_snippet( $height, $width ) ); |
||
| 589 | } |
||
| 590 | } elseif ( 'house' === $type ) { |
||
| 591 | $leaderboard = 'top' === $spot && ! $this->params->mobile_device; |
||
| 592 | $snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' ); |
||
| 593 | if ( 'belowpost' === $spot && $this->option( 'wordads_second_belowpost', true ) ) { |
||
| 594 | $snippet .= $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' ); |
||
| 595 | } |
||
| 596 | } |
||
| 597 | |||
| 598 | return $snippet; |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Returns the AMP snippet to be inserted |
||
| 603 | * |
||
| 604 | * @param int $height Height. |
||
| 605 | * @param int $width Width. |
||
| 606 | * @return string |
||
| 607 | * |
||
| 608 | * @since 8.7 |
||
| 609 | */ |
||
| 610 | public function get_amp_snippet( $height, $width ) { |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Compatibility function -- main functionality replaced with get_dynamic_ad_snippet |
||
| 626 | * |
||
| 627 | * @param int $section_id Ad section. |
||
| 628 | * @param int $height Ad height. |
||
| 629 | * @param int $width Ad width. |
||
| 630 | * @param string $location Location. |
||
| 631 | * @param string $css CSS. |
||
| 632 | * |
||
| 633 | * @return string |
||
| 634 | * |
||
| 635 | * @since 5.7 |
||
| 636 | */ |
||
| 637 | public function get_ad_snippet( $section_id, $height, $width, $location = '', $css = '' ) { |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Returns the dynamic snippet to be inserted into the ad unit |
||
| 690 | * |
||
| 691 | * @param int $section_id section_id. |
||
| 692 | * @param string $form_factor form_factor. |
||
| 693 | * @param string $location location. |
||
| 694 | * @param string $relocate location to be moved after the fact for themes without required hook. |
||
| 695 | * @return string |
||
| 696 | * |
||
| 697 | * @since 8.7 |
||
| 698 | */ |
||
| 699 | public function get_dynamic_ad_snippet( $section_id, $form_factor = 'square', $location = '', $relocate = '' ) { |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Returns the complete ad div with snippet to be inserted into the page |
||
| 759 | * |
||
| 760 | * @param string $spot top, side, inline, or belowpost. |
||
| 761 | * @param string $snippet The snippet to insert into the div. |
||
| 762 | * @param array $css_classes CSS classes. |
||
| 763 | * @return string The supporting ad unit div. |
||
| 764 | * |
||
| 765 | * @since 7.1 |
||
| 766 | */ |
||
| 767 | public function get_ad_div( $spot, $snippet, array $css_classes = array() ) { |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Check the reasons to bail before we attempt to insert ads. |
||
| 798 | * |
||
| 799 | * @return true if we should bail (don't insert ads) |
||
| 800 | * |
||
| 801 | * @since 4.5.0 |
||
| 802 | */ |
||
| 803 | public function should_bail() { |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Returns markup for HTML5 house ad base on unit |
||
| 809 | * |
||
| 810 | * @param string $unit mrec, widesky, or leaderboard. |
||
| 811 | * @return string markup for HTML5 house ad |
||
| 812 | * |
||
| 813 | * @since 4.7.0 |
||
| 814 | */ |
||
| 815 | public function get_house_ad( $unit = 'mrec' ) { |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Activation hook actions |
||
| 848 | * |
||
| 849 | * @since 4.5.0 |
||
| 850 | */ |
||
| 851 | public static function activate() { |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Registers the widgets. |
||
| 857 | */ |
||
| 858 | public function widget_callback() { |
||
| 867 | } |
||
| 868 | |||
| 875 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..