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() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return boolean true if user is browsing in iOS device |
||
| 92 | * |
||
| 93 | * @since 4.5.0 |
||
| 94 | */ |
||
| 95 | public function is_ios() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Returns the user's device (see user-agent.php) or 'desktop' |
||
| 101 | * @return string user device |
||
| 102 | * |
||
| 103 | * @since 4.5.0 |
||
| 104 | */ |
||
| 105 | public function get_device() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return string The type of page that is being loaded |
||
| 121 | * |
||
| 122 | * @since 4.5.0 |
||
| 123 | */ |
||
| 124 | public function get_page_type() { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns true if page is static home |
||
| 152 | * @return boolean true if page is static home |
||
| 153 | * |
||
| 154 | * @since 4.5.0 |
||
| 155 | */ |
||
| 156 | public static function is_static_home() { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Logic for if we should show an ad |
||
| 164 | * |
||
| 165 | * @since 4.5.0 |
||
| 166 | */ |
||
| 167 | public function should_show() { |
||
| 196 | } |
||
| 197 |
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: