Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | 'term_relationships_full_sync_item_size' => true, |
||
54 | 'sync_sender_enabled' => true, |
||
55 | 'full_sync_sender_enabled' => true, |
||
56 | 'full_sync_send_duration' => true, |
||
57 | 'full_sync_limits' => true, |
||
58 | ); |
||
59 | |||
60 | /** |
||
61 | * Whether WordPress is currently running an import. |
||
62 | * |
||
63 | * @access public |
||
64 | * @static |
||
65 | * |
||
66 | * @var null|boolean |
||
67 | */ |
||
68 | public static $is_importing; |
||
69 | |||
70 | /** |
||
71 | * Whether WordPress is currently running a WP cron request. |
||
72 | * |
||
73 | * @access public |
||
74 | * @static |
||
75 | * |
||
76 | * @var null|boolean |
||
77 | */ |
||
78 | public static $is_doing_cron; |
||
79 | |||
80 | /** |
||
81 | * Whether we're currently syncing. |
||
82 | * |
||
83 | * @access public |
||
84 | * @static |
||
85 | * |
||
86 | * @var null|boolean |
||
87 | */ |
||
88 | public static $is_syncing; |
||
89 | |||
90 | /** |
||
91 | * Whether we're currently sending sync items. |
||
92 | * |
||
93 | * @access public |
||
94 | * @static |
||
95 | * |
||
96 | * @var null|boolean |
||
97 | */ |
||
98 | public static $is_sending; |
||
99 | |||
100 | /** |
||
101 | * Retrieve all settings with their current values. |
||
102 | * |
||
103 | * @access public |
||
104 | * @static |
||
105 | * |
||
106 | * @return array All current settings. |
||
107 | */ |
||
108 | public static function get_settings() { |
||
116 | |||
117 | /** |
||
118 | * Fetches the setting. It saves it if the setting doesn't exist, so that it gets |
||
119 | * autoloaded on page load rather than re-queried every time. |
||
120 | * |
||
121 | * @access public |
||
122 | * @static |
||
123 | * |
||
124 | * @param string $setting The setting name. |
||
125 | * @return mixed The setting value. |
||
126 | */ |
||
127 | public static function get_setting( $setting ) { |
||
185 | |||
186 | /** |
||
187 | * Change multiple settings in the same time. |
||
188 | * |
||
189 | * @access public |
||
190 | * @static |
||
191 | * |
||
192 | * @param array $new_settings The new settings. |
||
193 | */ |
||
194 | public static function update_settings( $new_settings ) { |
||
214 | |||
215 | /** |
||
216 | * Whether the specified setting is a network setting. |
||
217 | * |
||
218 | * @access public |
||
219 | * @static |
||
220 | * |
||
221 | * @param string $setting Setting name. |
||
222 | * @return boolean Whether the setting is a network setting. |
||
223 | */ |
||
224 | public static function is_network_setting( $setting ) { |
||
227 | |||
228 | /** |
||
229 | * Returns escaped SQL for blacklisted post types. |
||
230 | * Can be injected directly into a WHERE clause. |
||
231 | * |
||
232 | * @access public |
||
233 | * @static |
||
234 | * |
||
235 | * @return string SQL WHERE clause. |
||
236 | */ |
||
237 | public static function get_blacklisted_post_types_sql() { |
||
240 | |||
241 | /** |
||
242 | * Returns escaped values for disallowed post types. |
||
243 | * |
||
244 | * @access public |
||
245 | * @static |
||
246 | * |
||
247 | * @return array Post type filter values |
||
248 | */ |
||
249 | public static function get_disallowed_post_types_structured() { |
||
257 | |||
258 | /** |
||
259 | * Returns escaped SQL for blacklisted taxonomies. |
||
260 | * Can be injected directly into a WHERE clause. |
||
261 | * |
||
262 | * @access public |
||
263 | * @static |
||
264 | * |
||
265 | * @return string SQL WHERE clause. |
||
266 | */ |
||
267 | public static function get_blacklisted_taxonomies_sql() { |
||
270 | |||
271 | /** |
||
272 | * Returns escaped SQL for blacklisted post meta. |
||
273 | * Can be injected directly into a WHERE clause. |
||
274 | * |
||
275 | * @access public |
||
276 | * @static |
||
277 | * |
||
278 | * @return string SQL WHERE clause. |
||
279 | */ |
||
280 | public static function get_whitelisted_post_meta_sql() { |
||
283 | |||
284 | /** |
||
285 | * Returns escaped SQL for allowed post meta keys. |
||
286 | * |
||
287 | * @access public |
||
288 | * @static |
||
289 | * |
||
290 | * @return array Meta keys filter values |
||
291 | */ |
||
292 | View Code Duplication | public static function get_allowed_post_meta_structured() { |
|
300 | |||
301 | /** |
||
302 | * Returns escaped SQL for blacklisted comment meta. |
||
303 | * Can be injected directly into a WHERE clause. |
||
304 | * |
||
305 | * @access public |
||
306 | * @static |
||
307 | * |
||
308 | * @return string SQL WHERE clause. |
||
309 | */ |
||
310 | public static function get_whitelisted_comment_meta_sql() { |
||
313 | |||
314 | /** |
||
315 | * Returns SQL-escaped values for allowed post meta keys. |
||
316 | * |
||
317 | * @access public |
||
318 | * @static |
||
319 | * |
||
320 | * @return array Meta keys filter values |
||
321 | */ |
||
322 | View Code Duplication | public static function get_allowed_comment_meta_structured() { |
|
330 | |||
331 | /** |
||
332 | * Returns escaped SQL for comments, excluding any spam comments. |
||
333 | * Can be injected directly into a WHERE clause. |
||
334 | * |
||
335 | * @access public |
||
336 | * @static |
||
337 | * |
||
338 | * @return string SQL WHERE clause. |
||
339 | */ |
||
340 | public static function get_comments_filter_sql() { |
||
343 | |||
344 | /** |
||
345 | * Delete any settings options and clean up the current settings state. |
||
346 | * |
||
347 | * @access public |
||
348 | * @static |
||
349 | */ |
||
350 | public static function reset_data() { |
||
360 | |||
361 | /** |
||
362 | * Set the importing state. |
||
363 | * |
||
364 | * @access public |
||
365 | * @static |
||
366 | * |
||
367 | * @param boolean $is_importing Whether WordPress is currently importing. |
||
368 | */ |
||
369 | public static function set_importing( $is_importing ) { |
||
373 | |||
374 | /** |
||
375 | * Whether WordPress is currently importing. |
||
376 | * |
||
377 | * @access public |
||
378 | * @static |
||
379 | * |
||
380 | * @return boolean Whether WordPress is currently importing. |
||
381 | */ |
||
382 | public static function is_importing() { |
||
389 | |||
390 | /** |
||
391 | * Whether sync is enabled. |
||
392 | * |
||
393 | * @access public |
||
394 | * @static |
||
395 | * |
||
396 | * @return boolean Whether sync is enabled. |
||
397 | */ |
||
398 | public static function is_sync_enabled() { |
||
401 | |||
402 | /** |
||
403 | * Set the WP cron state. |
||
404 | * |
||
405 | * @access public |
||
406 | * @static |
||
407 | * |
||
408 | * @param boolean $is_doing_cron Whether WordPress is currently doing WP cron. |
||
409 | */ |
||
410 | public static function set_doing_cron( $is_doing_cron ) { |
||
414 | |||
415 | /** |
||
416 | * Whether WordPress is currently doing WP cron. |
||
417 | * |
||
418 | * @access public |
||
419 | * @static |
||
420 | * |
||
421 | * @return boolean Whether WordPress is currently doing WP cron. |
||
422 | */ |
||
423 | public static function is_doing_cron() { |
||
430 | |||
431 | /** |
||
432 | * Whether we are currently syncing. |
||
433 | * |
||
434 | * @access public |
||
435 | * @static |
||
436 | * |
||
437 | * @return boolean Whether we are currently syncing. |
||
438 | */ |
||
439 | public static function is_syncing() { |
||
442 | |||
443 | /** |
||
444 | * Set the syncing state. |
||
445 | * |
||
446 | * @access public |
||
447 | * @static |
||
448 | * |
||
449 | * @param boolean $is_syncing Whether we are currently syncing. |
||
450 | */ |
||
451 | public static function set_is_syncing( $is_syncing ) { |
||
454 | |||
455 | /** |
||
456 | * Whether we are currently sending sync items. |
||
457 | * |
||
458 | * @access public |
||
459 | * @static |
||
460 | * |
||
461 | * @return boolean Whether we are currently sending sync items. |
||
462 | */ |
||
463 | public static function is_sending() { |
||
466 | |||
467 | /** |
||
468 | * Set the sending state. |
||
469 | * |
||
470 | * @access public |
||
471 | * @static |
||
472 | * |
||
473 | * @param boolean $is_sending Whether we are currently sending sync items. |
||
474 | */ |
||
475 | public static function set_is_sending( $is_sending ) { |
||
478 | |||
479 | /** |
||
480 | * Whether should send from the queue |
||
481 | * |
||
482 | * @access public |
||
483 | * @static |
||
484 | * |
||
485 | * @param string $queue_id The queue identifier. |
||
486 | * |
||
487 | * @return boolean Whether sync is enabled. |
||
488 | */ |
||
489 | public static function is_sender_enabled( $queue_id ) { |
||
492 | |||
493 | } |
||
494 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: