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 |
||
15 | class WordAds_Params { |
||
16 | |||
17 | /** |
||
18 | * Setup parameters for serving the ads |
||
19 | * |
||
20 | * @since 4.5.0 |
||
21 | */ |
||
22 | public function __construct() { |
||
82 | |||
83 | /** |
||
84 | * Is this a mobile device? |
||
85 | * |
||
86 | * @return boolean true if the user is browsing on a mobile device (iPad not included) |
||
87 | * |
||
88 | * @since 4.5.0 |
||
89 | */ |
||
90 | public function is_mobile() { |
||
93 | |||
94 | /** |
||
95 | * Is this site served by CloudFlare? |
||
96 | * |
||
97 | * @return boolean true if site is being served via CloudFlare |
||
98 | * |
||
99 | * @since 4.5.0 |
||
100 | */ |
||
101 | public static function is_cloudflare() { |
||
113 | |||
114 | /** |
||
115 | * Is this an iOS device? |
||
116 | * |
||
117 | * @return boolean true if user is browsing in iOS device |
||
118 | * |
||
119 | * @since 4.5.0 |
||
120 | */ |
||
121 | public function is_ios() { |
||
124 | |||
125 | /** |
||
126 | * Returns the user's device (see user-agent.php) or 'desktop' |
||
127 | * |
||
128 | * @return string user device |
||
129 | * |
||
130 | * @since 4.5.0 |
||
131 | */ |
||
132 | public function get_device() { |
||
145 | |||
146 | /** |
||
147 | * Get page type. |
||
148 | * |
||
149 | * @return string The type of page that is being loaded |
||
150 | * |
||
151 | * @since 4.5.0 |
||
152 | */ |
||
153 | public function get_page_type() { |
||
178 | |||
179 | /** |
||
180 | * Get IPW code. |
||
181 | * |
||
182 | * @return int The page type code for ipw config |
||
183 | * |
||
184 | * @since 5.6.0 |
||
185 | */ |
||
186 | public function get_page_type_ipw() { |
||
209 | |||
210 | /** |
||
211 | * Returns true if page is static home |
||
212 | * |
||
213 | * @return boolean true if page is static home |
||
214 | * |
||
215 | * @since 4.5.0 |
||
216 | */ |
||
217 | public static function is_static_home() { |
||
222 | |||
223 | /** |
||
224 | * Logic for if we should show an ad |
||
225 | * |
||
226 | * @since 4.5.0 |
||
227 | */ |
||
228 | public function should_show() { |
||
257 | } |
||
258 |
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: