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() { |
||
106 | |||
107 | /** |
||
108 | * Add the actions/filters to insert the ads. Checks for mobile or desktop. |
||
109 | * |
||
110 | * @since 4.5.0 |
||
111 | */ |
||
112 | private function insert_adcode() { |
||
132 | |||
133 | /** |
||
134 | * Add the actions/filters to insert extra-network features. |
||
135 | * |
||
136 | * @since 4.5.0 |
||
137 | */ |
||
138 | private function insert_extras() { |
||
141 | |||
142 | /** |
||
143 | * Register desktop scripts and styles |
||
144 | * |
||
145 | * @since 4.5.0 |
||
146 | */ |
||
147 | function enqueue_scripts() { |
||
155 | |||
156 | /** |
||
157 | * IPONWEB metadata used by the various scripts |
||
158 | * @return [type] [description] |
||
|
|||
159 | */ |
||
160 | function insert_head_meta() { |
||
176 | |||
177 | /** |
||
178 | * IPONWEB scripts in <head> |
||
179 | * |
||
180 | * @since 4.5.0 |
||
181 | */ |
||
182 | function insert_head_iponweb() { |
||
186 | |||
187 | /** |
||
188 | * Insert the ad onto the page |
||
189 | * |
||
190 | * @since 4.5.0 |
||
191 | */ |
||
192 | function insert_ad( $content ) { |
||
214 | |||
215 | /** |
||
216 | * Inserts ad into header |
||
217 | * |
||
218 | * @since 4.5.0 |
||
219 | */ |
||
220 | function insert_header_ad() { |
||
237 | |||
238 | /** |
||
239 | * Special cases for inserting header unit via jQuery |
||
240 | * |
||
241 | * @since 4.5.0 |
||
242 | */ |
||
243 | function insert_header_ad_special() { |
||
278 | |||
279 | /** |
||
280 | * Get the ad for the spot and type. |
||
281 | * @param string $spot top, side, or belowpost |
||
282 | * @param string $type iponweb or adsense |
||
283 | */ |
||
284 | function get_ad( $spot, $type = 'iponweb' ) { |
||
324 | |||
325 | /** |
||
326 | * Check the reasons to bail before we attempt to insert ads. |
||
327 | * @return true if we should bail (don't insert ads) |
||
328 | * |
||
329 | * @since 4.5.0 |
||
330 | */ |
||
331 | public function should_bail() { |
||
334 | |||
335 | /** |
||
336 | * Returns markup for HTML5 house ad base on unit |
||
337 | * @param string $unit mrec, widesky, or leaderboard |
||
338 | * @return string markup for HTML5 house ad |
||
339 | * |
||
340 | * @since 4.7.0 |
||
341 | */ |
||
342 | public function get_house_ad( $unit = 'mrec' ) { |
||
369 | |||
370 | /** |
||
371 | * Activation hook actions |
||
372 | * |
||
373 | * @since 4.5.0 |
||
374 | */ |
||
375 | public static function activate() { |
||
378 | } |
||
379 | |||
386 |
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.