Complex classes like WordAds_Params 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_Params, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class WordAds_Params { |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Setup parameters for serving the ads |
||
| 7 | * |
||
| 8 | * @since 4.5.0 |
||
| 9 | */ |
||
| 10 | public function __construct() { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return boolean true if the user is browsing on a mobile device (iPad not included) |
||
| 43 | * |
||
| 44 | * @since 4.5.0 |
||
| 45 | */ |
||
| 46 | public function is_mobile() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return boolean true if site is being served via CloudFlare |
||
| 52 | * |
||
| 53 | * @since 4.5.0 |
||
| 54 | */ |
||
| 55 | public static function is_cloudflare() { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return boolean true if user is browsing in iOS device |
||
| 74 | * |
||
| 75 | * @since 4.5.0 |
||
| 76 | */ |
||
| 77 | public function is_ios() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns the user's device (see user-agent.php) or 'desktop' |
||
| 83 | * @return string user device |
||
| 84 | * |
||
| 85 | * @since 4.5.0 |
||
| 86 | */ |
||
| 87 | public function get_device() { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return string The type of page that is being loaded |
||
| 103 | * |
||
| 104 | * @since 4.5.0 |
||
| 105 | */ |
||
| 106 | public function get_page_type() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns true if page is static home |
||
| 134 | * @return boolean true if page is static home |
||
| 135 | * |
||
| 136 | * @since 4.5.0 |
||
| 137 | */ |
||
| 138 | public static function is_static_home() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Logic for if we should show an ad |
||
| 146 | * |
||
| 147 | * @since 4.5.0 |
||
| 148 | */ |
||
| 149 | public static function should_show() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Logic for if we should show a mobile ad |
||
| 165 | * |
||
| 166 | * @since 4.5.0 |
||
| 167 | */ |
||
| 168 | public static function should_show_mobile() { |
||
| 185 | } |
||
| 186 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: