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() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @return boolean true if the user is browsing on a mobile device (iPad not included) |
||
| 61 | * |
||
| 62 | * @since 4.5.0 |
||
| 63 | */ |
||
| 64 | public function is_mobile() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return boolean true if site is being served via CloudFlare |
||
| 70 | * |
||
| 71 | * @since 4.5.0 |
||
| 72 | */ |
||
| 73 | public static function is_cloudflare() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return boolean true if user is browsing in iOS device |
||
| 88 | * |
||
| 89 | * @since 4.5.0 |
||
| 90 | */ |
||
| 91 | public function is_ios() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns the user's device (see user-agent.php) or 'desktop' |
||
| 97 | * @return string user device |
||
| 98 | * |
||
| 99 | * @since 4.5.0 |
||
| 100 | */ |
||
| 101 | public function get_device() { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string The type of page that is being loaded |
||
| 117 | * |
||
| 118 | * @since 4.5.0 |
||
| 119 | */ |
||
| 120 | public function get_page_type() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return int The page type code for ipw config |
||
| 148 | * |
||
| 149 | * @since 5.6.0 |
||
| 150 | */ |
||
| 151 | public function get_page_type_ipw() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns true if page is static home |
||
| 177 | * @return boolean true if page is static home |
||
| 178 | * |
||
| 179 | * @since 4.5.0 |
||
| 180 | */ |
||
| 181 | public static function is_static_home() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Logic for if we should show an ad |
||
| 189 | * |
||
| 190 | * @since 4.5.0 |
||
| 191 | */ |
||
| 192 | public function should_show() { |
||
| 221 | } |
||
| 222 |
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: