Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | * The different supported ad types. |
||
| 22 | * v0.1 - mrec only for now |
||
| 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 | 'lrec' => array( |
||
| 32 | 'tag' => '336x280_largerectangle', |
||
| 33 | 'height' => '280', |
||
| 34 | 'width' => '336', |
||
| 35 | ), |
||
| 36 | 'leaderboard' => array( |
||
| 37 | 'tag' => '728x90_leaderboard', |
||
| 38 | 'height' => '90', |
||
| 39 | 'width' => '728', |
||
| 40 | ), |
||
| 41 | 'wideskyscraper' => array( |
||
| 42 | 'tag' => '160x600_wideskyscraper', |
||
| 43 | 'height' => '600', |
||
| 44 | 'width' => '160', |
||
| 45 | ), |
||
| 46 | ); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Convenience function for grabbing options from params->options |
||
| 50 | * @param string $option the option to grab |
||
| 51 | * @param mixed $default (optional) |
||
| 52 | * @return option or $default if not set |
||
| 53 | * |
||
| 54 | * @since 4.5.0 |
||
| 55 | */ |
||
| 56 | function option( $option, $default = false ) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Instantiate the plugin |
||
| 66 | * |
||
| 67 | * @since 4.5.0 |
||
| 68 | */ |
||
| 69 | function __construct() { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Code to run on WordPress 'init' hook |
||
| 75 | * |
||
| 76 | * @since 4.5.0 |
||
| 77 | */ |
||
| 78 | function init() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Check for Jetpack's The_Neverending_Home_Page and use got_infinity |
||
| 101 | * @return boolean true if load came from infinite scroll |
||
| 102 | * |
||
| 103 | * @since 4.5.0 |
||
| 104 | */ |
||
| 105 | 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() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Register desktop scripts and styles |
||
| 165 | * |
||
| 166 | * @since 4.5.0 |
||
| 167 | */ |
||
| 168 | function enqueue_scripts() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * IPONWEB metadata used by the various scripts |
||
| 179 | * @return [type] [description] |
||
|
|
|||
| 180 | */ |
||
| 181 | function insert_head_meta() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * IPONWEB scripts in <head> |
||
| 199 | * |
||
| 200 | * @since 4.5.0 |
||
| 201 | */ |
||
| 202 | function insert_head_iponweb() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Insert the ad onto the page |
||
| 225 | * |
||
| 226 | * @since 4.5.0 |
||
| 227 | */ |
||
| 228 | View Code Duplication | function insert_ad( $content ) { |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Insert an inline ad into a post content |
||
| 253 | * Used for rendering the `wordad` shortcode. |
||
| 254 | * |
||
| 255 | * @since 6.1.0 |
||
| 256 | */ |
||
| 257 | View Code Duplication | function insert_inline_ad( $content ) { |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Inserts ad into header |
||
| 283 | * |
||
| 284 | * @since 4.5.0 |
||
| 285 | */ |
||
| 286 | function insert_header_ad() { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Special cases for inserting header unit via jQuery |
||
| 306 | * |
||
| 307 | * @since 4.5.0 |
||
| 308 | */ |
||
| 309 | function insert_header_ad_special() { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Get the ad for the spot and type. |
||
| 347 | * @param string $spot top, side, inline, or belowpost |
||
| 348 | * @param string $type iponweb or adsense |
||
| 349 | */ |
||
| 350 | function get_ad( $spot, $type = 'iponweb' ) { |
||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * Returns the snippet to be inserted into the ad unit |
||
| 398 | * @param int $height |
||
| 399 | * @param int $width |
||
| 400 | * @param int $location |
||
| 401 | * @param string $css |
||
| 402 | * @return string |
||
| 403 | * |
||
| 404 | * @since 5.7 |
||
| 405 | */ |
||
| 406 | function get_ad_snippet( $height, $width, $location = '', $css = '' ) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Check the reasons to bail before we attempt to insert ads. |
||
| 435 | * @return true if we should bail (don't insert ads) |
||
| 436 | * |
||
| 437 | * @since 4.5.0 |
||
| 438 | */ |
||
| 439 | public function should_bail() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Returns markup for HTML5 house ad base on unit |
||
| 445 | * @param string $unit mrec, widesky, or leaderboard |
||
| 446 | * @return string markup for HTML5 house ad |
||
| 447 | * |
||
| 448 | * @since 4.7.0 |
||
| 449 | */ |
||
| 450 | public function get_house_ad( $unit = 'mrec' ) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Activation hook actions |
||
| 480 | * |
||
| 481 | * @since 4.5.0 |
||
| 482 | */ |
||
| 483 | public static function activate() { |
||
| 486 | } |
||
| 487 | |||
| 494 |
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.