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() { |
||
34 | |||
35 | /** |
||
36 | * @return boolean true if the user is browsing on a mobile device (iPad not included) |
||
37 | * |
||
38 | * @since 4.5.0 |
||
39 | */ |
||
40 | public function is_mobile() { |
||
43 | |||
44 | /** |
||
45 | * @return boolean true if site is being served via CloudFlare |
||
46 | * |
||
47 | * @since 4.5.0 |
||
48 | */ |
||
49 | public static function is_cloudflare() { |
||
65 | |||
66 | /** |
||
67 | * @return boolean true if user is browsing in iOS device |
||
68 | * |
||
69 | * @since 4.5.0 |
||
70 | */ |
||
71 | public function is_ios() { |
||
74 | |||
75 | /** |
||
76 | * Returns the user's device (see user-agent.php) or 'desktop' |
||
77 | * @return string user device |
||
78 | * |
||
79 | * @since 4.5.0 |
||
80 | */ |
||
81 | public function get_device() { |
||
94 | |||
95 | /** |
||
96 | * @return string The type of page that is being loaded |
||
97 | * |
||
98 | * @since 4.5.0 |
||
99 | */ |
||
100 | public function get_page_type() { |
||
125 | |||
126 | /** |
||
127 | * Returns true if page is static home |
||
128 | * @return boolean true if page is static home |
||
129 | * |
||
130 | * @since 4.5.0 |
||
131 | */ |
||
132 | public static function is_static_home() { |
||
137 | |||
138 | /** |
||
139 | * Logic for if we should show an ad |
||
140 | * |
||
141 | * @since 4.5.0 |
||
142 | */ |
||
143 | public static function should_show() { |
||
156 | |||
157 | /** |
||
158 | * Logic for if we should show a mobile ad |
||
159 | * |
||
160 | * @since 4.5.0 |
||
161 | */ |
||
162 | public static function should_show_mobile() { |
||
179 | } |
||
180 |
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: