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() { |
||
195 | |||
196 | /** |
||
197 | * Return array of allowed REST API post types. |
||
198 | * |
||
199 | * @return array Array of allowed post types. |
||
200 | */ |
||
201 | public static function rest_api_allowed_post_types() { |
||
205 | |||
206 | /** |
||
207 | * Return array of allowed REST API public metadata. |
||
208 | * |
||
209 | * @return array Array of allowed metadata. |
||
210 | */ |
||
211 | public static function rest_api_allowed_public_metadata() { |
||
215 | |||
216 | /** |
||
217 | * Finds out if a site is using a version control system. |
||
218 | * |
||
219 | * @return bool |
||
220 | **/ |
||
221 | public static function is_version_controlled() { |
||
230 | |||
231 | /** |
||
232 | * Returns true if the site has file write access false otherwise. |
||
233 | * |
||
234 | * @return bool |
||
235 | **/ |
||
236 | public static function file_system_write_access() { |
||
262 | |||
263 | /** |
||
264 | * Helper function that is used when getting home or siteurl values. Decides |
||
265 | * whether to get the raw or filtered value. |
||
266 | * |
||
267 | * @param string $url_type URL to get, home or siteurl. |
||
268 | * @return string |
||
269 | */ |
||
270 | public static function get_raw_or_filtered_url( $url_type ) { |
||
288 | |||
289 | /** |
||
290 | * Return the escaped home_url. |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | public static function home_url() { |
||
306 | |||
307 | /** |
||
308 | * Return the escaped siteurl. |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | public static function site_url() { |
||
324 | |||
325 | /** |
||
326 | * Return main site URL with a normalized protocol. |
||
327 | * |
||
328 | * @return string |
||
329 | */ |
||
330 | public static function main_network_site_url() { |
||
333 | |||
334 | /** |
||
335 | * Return URL with a normalized protocol. |
||
336 | * |
||
337 | * @param callable $callable Function to retrieve URL option. |
||
338 | * @param string $new_value URL Protocol to set URLs to. |
||
339 | * @return string Normalized URL. |
||
340 | */ |
||
341 | public static function get_protocol_normalized_url( $callable, $new_value ) { |
||
365 | |||
366 | /** |
||
367 | * Return URL from option or PHP constant. |
||
368 | * |
||
369 | * @param string $option_name (e.g. 'home'). |
||
370 | * |
||
371 | * @return mixed|null URL. |
||
372 | */ |
||
373 | public static function get_raw_url( $option_name ) { |
||
391 | |||
392 | /** |
||
393 | * Normalize domains by removing www unless declared in the site's option. |
||
394 | * |
||
395 | * @param string $option Option value from the site. |
||
396 | * @param callable $url_function Function retrieving the URL to normalize. |
||
397 | * @return mixed|string URL. |
||
398 | */ |
||
399 | public static function normalize_www_in_url( $option, $url_function ) { |
||
427 | |||
428 | /** |
||
429 | * Return filtered value of get_plugins. |
||
430 | * |
||
431 | * @return mixed|void |
||
432 | */ |
||
433 | public static function get_plugins() { |
||
441 | |||
442 | /** |
||
443 | * Get custom action link tags that the plugin is using |
||
444 | * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name) |
||
445 | * |
||
446 | * @param string $plugin_file_singular Particular plugin. |
||
447 | * @return array of plugin action links (key: link name value: url) |
||
448 | */ |
||
449 | public static function get_plugins_action_links( $plugin_file_singular = null ) { |
||
463 | |||
464 | /** |
||
465 | * Return the WP version as defined in the $wp_version global. |
||
466 | * |
||
467 | * @return string |
||
468 | */ |
||
469 | public static function wp_version() { |
||
473 | |||
474 | /** |
||
475 | * Return site icon url used on the site. |
||
476 | * |
||
477 | * @param int $size Size of requested icon in pixels. |
||
478 | * @return mixed|string|void |
||
479 | */ |
||
480 | public static function site_icon_url( $size = 512 ) { |
||
484 | |||
485 | /** |
||
486 | * Return roles registered on the site. |
||
487 | * |
||
488 | * @return array |
||
489 | */ |
||
490 | public static function roles() { |
||
494 | |||
495 | /** |
||
496 | * Determine time zone from WordPress' options "timezone_string" |
||
497 | * and "gmt_offset". |
||
498 | * |
||
499 | * 1. Check if `timezone_string` is set and return it. |
||
500 | * 2. Check if `gmt_offset` is set, formats UTC-offset from it and return it. |
||
501 | * 3. Default to "UTC+0" if nothing is set. |
||
502 | * |
||
503 | * Note: This function is specifically not using wp_timezone() to keep consistency with |
||
504 | * the existing formatting of the timezone string. |
||
505 | * |
||
506 | * @return string |
||
507 | */ |
||
508 | public static function get_timezone() { |
||
528 | |||
529 | /** |
||
530 | * Return list of paused themes. |
||
531 | * |
||
532 | * @todo Remove function_exists check when WP 5.2 is the minimum. |
||
533 | * |
||
534 | * @return array|bool Array of paused themes or false if unsupported. |
||
535 | */ |
||
536 | public static function get_paused_themes() { |
||
543 | |||
544 | /** |
||
545 | * Return list of paused plugins. |
||
546 | * |
||
547 | * @todo Remove function_exists check when WP 5.2 is the minimum. |
||
548 | * |
||
549 | * @return array|bool Array of paused plugins or false if unsupported. |
||
550 | */ |
||
551 | public static function get_paused_plugins() { |
||
558 | } |
||
559 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.