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 Functions 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 Functions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Functions { |
||
16 | const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_'; |
||
17 | const HTTPS_CHECK_HISTORY = 5; |
||
18 | |||
19 | /** |
||
20 | * Return array of Jetpack modules. |
||
21 | * |
||
22 | * @return array |
||
23 | */ |
||
24 | public static function get_modules() { |
||
29 | |||
30 | /** |
||
31 | * Return array of taxonomies registered on the site. |
||
32 | * |
||
33 | * @return array |
||
34 | */ |
||
35 | public static function get_taxonomies() { |
||
46 | |||
47 | /** |
||
48 | * Return array of registered shortcodes. |
||
49 | * |
||
50 | * @return array |
||
51 | */ |
||
52 | public static function get_shortcodes() { |
||
56 | |||
57 | /** |
||
58 | * Removes any callback data since we will not be able to process it on our side anyways. |
||
59 | * |
||
60 | * @param \WP_Taxonomy $taxonomy \WP_Taxonomy item. |
||
61 | * |
||
62 | * @return mixed|null |
||
63 | */ |
||
64 | public static function sanitize_taxonomy( $taxonomy ) { |
||
90 | |||
91 | /** |
||
92 | * Return array of registered post types. |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | public static function get_post_types() { |
||
108 | |||
109 | /** |
||
110 | * Sanitizes by cloning post type object. |
||
111 | * |
||
112 | * @param object $post_type \WP_Post_Type. |
||
113 | * |
||
114 | * @return object |
||
115 | */ |
||
116 | public static function sanitize_post_type( $post_type ) { |
||
126 | |||
127 | /** |
||
128 | * Return information about a synced post type. |
||
129 | * |
||
130 | * @param array $sanitized_post_type Array of args used in constructing \WP_Post_Type. |
||
131 | * @param string $post_type Post type name. |
||
132 | * |
||
133 | * @return object \WP_Post_Type |
||
134 | */ |
||
135 | public static function expand_synced_post_type( $sanitized_post_type, $post_type ) { |
||
144 | |||
145 | /** |
||
146 | * Returns site's post_type_features. |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | public static function get_post_type_features() { |
||
155 | |||
156 | /** |
||
157 | * Return hosting provider. |
||
158 | * |
||
159 | * Uses a set of known constants, classes, or functions to help determine the hosting platform. |
||
160 | * |
||
161 | * @return string Hosting provider. |
||
162 | */ |
||
163 | public static function get_hosting_provider() { |
||
180 | |||
181 | /** |
||
182 | * Return a hosting provider using a set of known constants. |
||
183 | * |
||
184 | * @return mixed A host identifier string or false. |
||
185 | */ |
||
186 | public function get_hosting_provider_by_known_constant() { |
||
208 | |||
209 | /** |
||
210 | * Return a hosting provider using a set of known classes. |
||
211 | * |
||
212 | * @return mixed A host identifier string or false. |
||
213 | */ |
||
214 | public function get_hosting_provider_by_known_class() { |
||
225 | |||
226 | /** |
||
227 | * Return a hosting provider using a set of known functions. |
||
228 | * |
||
229 | * @return mixed A host identifier string or false. |
||
230 | */ |
||
231 | public function get_hosting_provider_by_known_function() { |
||
242 | |||
243 | /** |
||
244 | * Return array of allowed REST API post types. |
||
245 | * |
||
246 | * @return array Array of allowed post types. |
||
247 | */ |
||
248 | public static function rest_api_allowed_post_types() { |
||
252 | |||
253 | /** |
||
254 | * Return array of allowed REST API public metadata. |
||
255 | * |
||
256 | * @return array Array of allowed metadata. |
||
257 | */ |
||
258 | public static function rest_api_allowed_public_metadata() { |
||
262 | |||
263 | /** |
||
264 | * Finds out if a site is using a version control system. |
||
265 | * |
||
266 | * @return bool |
||
267 | **/ |
||
268 | public static function is_version_controlled() { |
||
277 | |||
278 | /** |
||
279 | * Returns true if the site has file write access false otherwise. |
||
280 | * |
||
281 | * @return bool |
||
282 | **/ |
||
283 | public static function file_system_write_access() { |
||
309 | |||
310 | /** |
||
311 | * Helper function that is used when getting home or siteurl values. Decides |
||
312 | * whether to get the raw or filtered value. |
||
313 | * |
||
314 | * @param string $url_type URL to get, home or siteurl. |
||
315 | * @return string |
||
316 | */ |
||
317 | public static function get_raw_or_filtered_url( $url_type ) { |
||
335 | |||
336 | /** |
||
337 | * Return the escaped home_url. |
||
338 | * |
||
339 | * @return string |
||
340 | */ |
||
341 | public static function home_url() { |
||
353 | |||
354 | /** |
||
355 | * Return the escaped siteurl. |
||
356 | * |
||
357 | * @return string |
||
358 | */ |
||
359 | public static function site_url() { |
||
371 | |||
372 | /** |
||
373 | * Return main site URL with a normalized protocol. |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | public static function main_network_site_url() { |
||
380 | |||
381 | /** |
||
382 | * Return main site WordPress.com site ID. |
||
383 | * |
||
384 | * @return string |
||
385 | */ |
||
386 | public static function main_network_site_wpcom_id() { |
||
403 | |||
404 | /** |
||
405 | * Return URL with a normalized protocol. |
||
406 | * |
||
407 | * @param callable $callable Function to retrieve URL option. |
||
408 | * @param string $new_value URL Protocol to set URLs to. |
||
409 | * @return string Normalized URL. |
||
410 | */ |
||
411 | public static function get_protocol_normalized_url( $callable, $new_value ) { |
||
435 | |||
436 | /** |
||
437 | * Return URL from option or PHP constant. |
||
438 | * |
||
439 | * @param string $option_name (e.g. 'home'). |
||
440 | * |
||
441 | * @return mixed|null URL. |
||
442 | */ |
||
443 | public static function get_raw_url( $option_name ) { |
||
461 | |||
462 | /** |
||
463 | * Normalize domains by removing www unless declared in the site's option. |
||
464 | * |
||
465 | * @param string $option Option value from the site. |
||
466 | * @param callable $url_function Function retrieving the URL to normalize. |
||
467 | * @return mixed|string URL. |
||
468 | */ |
||
469 | public static function normalize_www_in_url( $option, $url_function ) { |
||
497 | |||
498 | /** |
||
499 | * Return filtered value of get_plugins. |
||
500 | * |
||
501 | * @return mixed|void |
||
502 | */ |
||
503 | public static function get_plugins() { |
||
511 | |||
512 | /** |
||
513 | * Get custom action link tags that the plugin is using |
||
514 | * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name) |
||
515 | * |
||
516 | * @param string $plugin_file_singular Particular plugin. |
||
517 | * @return array of plugin action links (key: link name value: url) |
||
518 | */ |
||
519 | public static function get_plugins_action_links( $plugin_file_singular = null ) { |
||
533 | |||
534 | /** |
||
535 | * Return the WP version as defined in the $wp_version global. |
||
536 | * |
||
537 | * @return string |
||
538 | */ |
||
539 | public static function wp_version() { |
||
543 | |||
544 | /** |
||
545 | * Return site icon url used on the site. |
||
546 | * |
||
547 | * @param int $size Size of requested icon in pixels. |
||
548 | * @return mixed|string|void |
||
549 | */ |
||
550 | public static function site_icon_url( $size = 512 ) { |
||
554 | |||
555 | /** |
||
556 | * Return roles registered on the site. |
||
557 | * |
||
558 | * @return array |
||
559 | */ |
||
560 | public static function roles() { |
||
564 | |||
565 | /** |
||
566 | * Determine time zone from WordPress' options "timezone_string" |
||
567 | * and "gmt_offset". |
||
568 | * |
||
569 | * 1. Check if `timezone_string` is set and return it. |
||
570 | * 2. Check if `gmt_offset` is set, formats UTC-offset from it and return it. |
||
571 | * 3. Default to "UTC+0" if nothing is set. |
||
572 | * |
||
573 | * Note: This function is specifically not using wp_timezone() to keep consistency with |
||
574 | * the existing formatting of the timezone string. |
||
575 | * |
||
576 | * @return string |
||
577 | */ |
||
578 | public static function get_timezone() { |
||
598 | |||
599 | /** |
||
600 | * Return list of paused themes. |
||
601 | * |
||
602 | * @return array|bool Array of paused themes or false if unsupported. |
||
603 | */ |
||
604 | public static function get_paused_themes() { |
||
608 | |||
609 | /** |
||
610 | * Return list of paused plugins. |
||
611 | * |
||
612 | * @return array|bool Array of paused plugins or false if unsupported. |
||
613 | */ |
||
614 | public static function get_paused_plugins() { |
||
618 | |||
619 | /** |
||
620 | * Return the theme's supported features. |
||
621 | * Used for syncing the supported feature that we care about. |
||
622 | * |
||
623 | * @return array List of features that the theme supports. |
||
624 | */ |
||
625 | public static function get_theme_support() { |
||
638 | |||
639 | /** |
||
640 | * Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion. |
||
641 | * |
||
642 | * @since 9.5.0 |
||
643 | * |
||
644 | * @param array|obj $any Source data to be cleaned up. |
||
645 | * @param array $seen_nodes Built array of nodes. |
||
646 | * |
||
647 | * @return array |
||
648 | */ |
||
649 | public static function json_wrap( &$any, $seen_nodes = array() ) { |
||
679 | } |
||
680 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: