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 |
||
| 13 | class WordAds { |
||
| 14 | |||
| 15 | public $params = null; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The different supported ad types. |
||
| 19 | * v0.1 - mrec only for now |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | public static $ad_tag_ids = array( |
||
| 23 | 'mrec' => array( |
||
| 24 | 'tag' => '300x250_mediumrectangle', |
||
| 25 | 'height' => '250', |
||
| 26 | 'width' => '300', |
||
| 27 | ), |
||
| 28 | 'lrec' => array( |
||
| 29 | 'tag' => '336x280_largerectangle', |
||
| 30 | 'height' => '280', |
||
| 31 | 'width' => '336', |
||
| 32 | ), |
||
| 33 | 'leaderboard' => array( |
||
| 34 | 'tag' => '728x90_leaderboard', |
||
| 35 | 'height' => '90', |
||
| 36 | 'width' => '728', |
||
| 37 | ), |
||
| 38 | 'wideskyscraper' => array( |
||
| 39 | 'tag' => '160x600_wideskyscraper', |
||
| 40 | 'height' => '600', |
||
| 41 | 'width' => '160', |
||
| 42 | ), |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Convenience function for grabbing options from params->options |
||
| 47 | * @param string $option the option to grab |
||
| 48 | * @param mixed $default (optional) |
||
| 49 | * @return option or $default if not set |
||
| 50 | * |
||
| 51 | * @since 4.5.0 |
||
| 52 | */ |
||
| 53 | function option( $option, $default = false ) { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Instantiate the plugin |
||
| 63 | * |
||
| 64 | * @since 4.5.0 |
||
| 65 | */ |
||
| 66 | function __construct() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Code to run on WordPress 'init' hook |
||
| 72 | * |
||
| 73 | * @since 4.5.0 |
||
| 74 | */ |
||
| 75 | function init() { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Check for Jetpack's The_Neverending_Home_Page and use got_infinity |
||
| 99 | * @return boolean true if load came from infinite scroll |
||
| 100 | * |
||
| 101 | * @since 4.5.0 |
||
| 102 | */ |
||
| 103 | public static function is_infinite_scroll() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Add the actions/filters to insert the ads. Checks for mobile or desktop. |
||
| 111 | * |
||
| 112 | * @since 4.5.0 |
||
| 113 | */ |
||
| 114 | private function insert_adcode() { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Add the actions/filters to insert extra-network features. |
||
| 137 | * |
||
| 138 | * @since 4.5.0 |
||
| 139 | */ |
||
| 140 | private function insert_extras() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Register desktop scripts and styles |
||
| 146 | * |
||
| 147 | * @since 4.5.0 |
||
| 148 | */ |
||
| 149 | function enqueue_scripts() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * IPONWEB metadata used by the various scripts |
||
| 160 | * @return [type] [description] |
||
|
|
|||
| 161 | */ |
||
| 162 | function insert_head_meta() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * IPONWEB scripts in <head> |
||
| 181 | * |
||
| 182 | * @since 4.5.0 |
||
| 183 | */ |
||
| 184 | function insert_head_iponweb() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Insert the ad onto the page |
||
| 191 | * |
||
| 192 | * @since 4.5.0 |
||
| 193 | */ |
||
| 194 | function insert_ad( $content ) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Inserts ad into header |
||
| 215 | * |
||
| 216 | * @since 4.5.0 |
||
| 217 | */ |
||
| 218 | function insert_header_ad() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Special cases for inserting header unit via jQuery |
||
| 238 | * |
||
| 239 | * @since 4.5.0 |
||
| 240 | */ |
||
| 241 | function insert_header_ad_special() { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get the ad for the spot and type. |
||
| 279 | * @param string $spot top, side, or belowpost |
||
| 280 | * @param string $type iponweb or adsense |
||
| 281 | */ |
||
| 282 | function get_ad( $spot, $type = 'iponweb' ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Check the reasons to bail before we attempt to insert ads. |
||
| 325 | * @return true if we should bail (don't insert ads) |
||
| 326 | * |
||
| 327 | * @since 4.5.0 |
||
| 328 | */ |
||
| 329 | public function should_bail() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Returns markup for HTML5 house ad base on unit |
||
| 335 | * @param string $unit mrec, widesky, or leaderboard |
||
| 336 | * @return string markup for HTML5 house ad |
||
| 337 | * |
||
| 338 | * @since 4.7.0 |
||
| 339 | */ |
||
| 340 | public function get_house_ad( $unit = 'mrec' ) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Activation hook actions |
||
| 370 | * |
||
| 371 | * @since 4.5.0 |
||
| 372 | */ |
||
| 373 | public static function activate() { |
||
| 376 | } |
||
| 377 | |||
| 384 |
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.