Complex classes like Settings 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 Settings, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Settings { |
||
14 | /** |
||
15 | * Prefix, used for the sync settings option names. |
||
16 | * |
||
17 | * @access public |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_'; |
||
22 | |||
23 | /** |
||
24 | * A whitelist of valid settings. |
||
25 | * |
||
26 | * @access public |
||
27 | * @static |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | public static $valid_settings = array( |
||
32 | 'dequeue_max_bytes' => true, |
||
33 | 'upload_max_bytes' => true, |
||
34 | 'upload_max_rows' => true, |
||
35 | 'sync_wait_time' => true, |
||
36 | 'sync_wait_threshold' => true, |
||
37 | 'enqueue_wait_time' => true, |
||
38 | 'max_queue_size' => true, |
||
39 | 'max_queue_lag' => true, |
||
40 | 'queue_max_writes_sec' => true, |
||
41 | 'post_types_blacklist' => true, |
||
42 | 'taxonomies_blacklist' => true, |
||
43 | 'disable' => true, |
||
44 | 'network_disable' => true, |
||
45 | 'render_filtered_content' => true, |
||
46 | 'post_meta_whitelist' => true, |
||
47 | 'comment_meta_whitelist' => true, |
||
48 | 'max_enqueue_full_sync' => true, |
||
49 | 'max_queue_size_full_sync' => true, |
||
50 | 'sync_via_cron' => true, |
||
51 | 'cron_sync_time_limit' => true, |
||
52 | 'known_importers' => true, |
||
53 | ); |
||
54 | |||
55 | /** |
||
56 | * Whether WordPress is currently running an import. |
||
57 | * |
||
58 | * @access public |
||
59 | * @static |
||
60 | * |
||
61 | * @var null|boolean |
||
62 | */ |
||
63 | public static $is_importing; |
||
64 | |||
65 | /** |
||
66 | * Whether WordPress is currently running a WP cron request. |
||
67 | * |
||
68 | * @access public |
||
69 | * @static |
||
70 | * |
||
71 | * @var null|boolean |
||
72 | */ |
||
73 | public static $is_doing_cron; |
||
74 | |||
75 | /** |
||
76 | * Whether we're currently syncing. |
||
77 | * |
||
78 | * @access public |
||
79 | * @static |
||
80 | * |
||
81 | * @var null|boolean |
||
82 | */ |
||
83 | public static $is_syncing; |
||
84 | |||
85 | /** |
||
86 | * Whether we're currently sending sync items. |
||
87 | * |
||
88 | * @access public |
||
89 | * @static |
||
90 | * |
||
91 | * @var null|boolean |
||
92 | */ |
||
93 | public static $is_sending; |
||
94 | |||
95 | /** |
||
96 | * Some settings can be expensive to compute - let's cache them. |
||
97 | * |
||
98 | * @access public |
||
99 | * @static |
||
100 | * |
||
101 | * @var array |
||
102 | */ |
||
103 | public static $settings_cache = array(); |
||
104 | |||
105 | /** |
||
106 | * Retrieve all settings with their current values. |
||
107 | * |
||
108 | * @access public |
||
109 | * @static |
||
110 | * |
||
111 | * @return array All current settings. |
||
112 | */ |
||
113 | public static function get_settings() { |
||
121 | |||
122 | /** |
||
123 | * Fetches the setting. It saves it if the setting doesn't exist, so that it gets |
||
124 | * autoloaded on page load rather than re-queried every time. |
||
125 | * |
||
126 | * @access public |
||
127 | * @static |
||
128 | * |
||
129 | * @param string $setting The setting name. |
||
130 | * @return mixed The setting value. |
||
131 | */ |
||
132 | public static function get_setting( $setting ) { |
||
198 | |||
199 | /** |
||
200 | * Change multiple settings in the same time. |
||
201 | * |
||
202 | * @access public |
||
203 | * @static |
||
204 | * |
||
205 | * @param array $new_settings The new settings. |
||
206 | */ |
||
207 | public static function update_settings( $new_settings ) { |
||
229 | |||
230 | /** |
||
231 | * Whether the specified setting is a network setting. |
||
232 | * |
||
233 | * @access public |
||
234 | * @static |
||
235 | * |
||
236 | * @param string $setting Setting name. |
||
237 | * @return boolean Whether the setting is a network setting. |
||
238 | */ |
||
239 | public static function is_network_setting( $setting ) { |
||
242 | |||
243 | /** |
||
244 | * Returns escaped SQL for blacklisted post types. |
||
245 | * Can be injected directly into a WHERE clause. |
||
246 | * |
||
247 | * @access public |
||
248 | * @static |
||
249 | * |
||
250 | * @return string SQL WHERE clause. |
||
251 | */ |
||
252 | public static function get_blacklisted_post_types_sql() { |
||
255 | |||
256 | /** |
||
257 | * Returns escaped SQL for blacklisted taxonomies. |
||
258 | * Can be injected directly into a WHERE clause. |
||
259 | * |
||
260 | * @access public |
||
261 | * @static |
||
262 | * |
||
263 | * @return string SQL WHERE clause. |
||
264 | */ |
||
265 | public static function get_blacklisted_taxonomies_sql() { |
||
268 | |||
269 | /** |
||
270 | * Returns escaped SQL for blacklisted post meta. |
||
271 | * Can be injected directly into a WHERE clause. |
||
272 | * |
||
273 | * @access public |
||
274 | * @static |
||
275 | * |
||
276 | * @return string SQL WHERE clause. |
||
277 | */ |
||
278 | public static function get_whitelisted_post_meta_sql() { |
||
281 | |||
282 | /** |
||
283 | * Returns escaped SQL for blacklisted comment meta. |
||
284 | * Can be injected directly into a WHERE clause. |
||
285 | * |
||
286 | * @access public |
||
287 | * @static |
||
288 | * |
||
289 | * @return string SQL WHERE clause. |
||
290 | */ |
||
291 | public static function get_whitelisted_comment_meta_sql() { |
||
294 | |||
295 | /** |
||
296 | * Returns escaped SQL for comments, excluding any spam comments. |
||
297 | * Can be injected directly into a WHERE clause. |
||
298 | * |
||
299 | * @access public |
||
300 | * @static |
||
301 | * |
||
302 | * @return string SQL WHERE clause. |
||
303 | */ |
||
304 | public static function get_comments_filter_sql() { |
||
307 | |||
308 | /** |
||
309 | * Delete any settings options and clean up the current settings state. |
||
310 | * |
||
311 | * @access public |
||
312 | * @static |
||
313 | */ |
||
314 | public static function reset_data() { |
||
325 | |||
326 | /** |
||
327 | * Set the importing state. |
||
328 | * |
||
329 | * @access public |
||
330 | * @static |
||
331 | * |
||
332 | * @param boolean $is_importing Whether WordPress is currently importing. |
||
333 | */ |
||
334 | public static function set_importing( $is_importing ) { |
||
338 | |||
339 | /** |
||
340 | * Whether WordPress is currently importing. |
||
341 | * |
||
342 | * @access public |
||
343 | * @static |
||
344 | * |
||
345 | * @return boolean Whether WordPress is currently importing. |
||
346 | */ |
||
347 | public static function is_importing() { |
||
354 | |||
355 | /** |
||
356 | * Whether sync is enabled. |
||
357 | * |
||
358 | * @access public |
||
359 | * @static |
||
360 | * |
||
361 | * @return boolean Whether sync is enabled. |
||
362 | */ |
||
363 | public static function is_sync_enabled() { |
||
366 | |||
367 | /** |
||
368 | * Set the WP cron state. |
||
369 | * |
||
370 | * @access public |
||
371 | * @static |
||
372 | * |
||
373 | * @param boolean $is_doing_cron Whether WordPress is currently doing WP cron. |
||
374 | */ |
||
375 | public static function set_doing_cron( $is_doing_cron ) { |
||
379 | |||
380 | /** |
||
381 | * Whether WordPress is currently doing WP cron. |
||
382 | * |
||
383 | * @access public |
||
384 | * @static |
||
385 | * |
||
386 | * @return boolean Whether WordPress is currently doing WP cron. |
||
387 | */ |
||
388 | public static function is_doing_cron() { |
||
395 | |||
396 | /** |
||
397 | * Whether we are currently syncing. |
||
398 | * |
||
399 | * @access public |
||
400 | * @static |
||
401 | * |
||
402 | * @return boolean Whether we are currently syncing. |
||
403 | */ |
||
404 | public static function is_syncing() { |
||
407 | |||
408 | /** |
||
409 | * Set the syncing state. |
||
410 | * |
||
411 | * @access public |
||
412 | * @static |
||
413 | * |
||
414 | * @param boolean $is_syncing Whether we are currently syncing. |
||
415 | */ |
||
416 | public static function set_is_syncing( $is_syncing ) { |
||
419 | |||
420 | /** |
||
421 | * Whether we are currently sending sync items. |
||
422 | * |
||
423 | * @access public |
||
424 | * @static |
||
425 | * |
||
426 | * @return boolean Whether we are currently sending sync items. |
||
427 | */ |
||
428 | public static function is_sending() { |
||
431 | |||
432 | /** |
||
433 | * Set the sending state. |
||
434 | * |
||
435 | * @access public |
||
436 | * @static |
||
437 | * |
||
438 | * @param boolean $is_sending Whether we are currently sending sync items. |
||
439 | */ |
||
440 | public static function set_is_sending( $is_sending ) { |
||
443 | } |
||
444 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.