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() { |
||
182 | |||
183 | /** |
||
184 | * Return a hosting provider using a set of known constants. |
||
185 | * |
||
186 | * @return mixed A host identifier string or false. |
||
187 | */ |
||
188 | public static function get_hosting_provider_by_known_constant() { |
||
217 | |||
218 | /** |
||
219 | * Return a hosting provider using a set of known classes. |
||
220 | * |
||
221 | * @return mixed A host identifier string or false. |
||
222 | */ |
||
223 | public static function get_hosting_provider_by_known_class() { |
||
234 | |||
235 | /** |
||
236 | * Return a hosting provider using a set of known functions. |
||
237 | * |
||
238 | * @return mixed A host identifier string or false. |
||
239 | */ |
||
240 | public static function get_hosting_provider_by_known_function() { |
||
251 | |||
252 | /** |
||
253 | * Return array of allowed REST API post types. |
||
254 | * |
||
255 | * @return array Array of allowed post types. |
||
256 | */ |
||
257 | public static function rest_api_allowed_post_types() { |
||
261 | |||
262 | /** |
||
263 | * Return array of allowed REST API public metadata. |
||
264 | * |
||
265 | * @return array Array of allowed metadata. |
||
266 | */ |
||
267 | public static function rest_api_allowed_public_metadata() { |
||
271 | |||
272 | /** |
||
273 | * Finds out if a site is using a version control system. |
||
274 | * |
||
275 | * @return bool |
||
276 | **/ |
||
277 | public static function is_version_controlled() { |
||
286 | |||
287 | /** |
||
288 | * Returns true if the site has file write access false otherwise. |
||
289 | * |
||
290 | * @return bool |
||
291 | **/ |
||
292 | public static function file_system_write_access() { |
||
318 | |||
319 | /** |
||
320 | * Helper function that is used when getting home or siteurl values. Decides |
||
321 | * whether to get the raw or filtered value. |
||
322 | * |
||
323 | * @param string $url_type URL to get, home or siteurl. |
||
324 | * @return string |
||
325 | */ |
||
326 | public static function get_raw_or_filtered_url( $url_type ) { |
||
344 | |||
345 | /** |
||
346 | * Return the escaped home_url. |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | public static function home_url() { |
||
362 | |||
363 | /** |
||
364 | * Return the escaped siteurl. |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | public static function site_url() { |
||
380 | |||
381 | /** |
||
382 | * Return main site URL with a normalized protocol. |
||
383 | * |
||
384 | * @return string |
||
385 | */ |
||
386 | public static function main_network_site_url() { |
||
389 | |||
390 | /** |
||
391 | * Return URL with a normalized protocol. |
||
392 | * |
||
393 | * @param callable $callable Function to retrieve URL option. |
||
394 | * @param string $new_value URL Protocol to set URLs to. |
||
395 | * @return string Normalized URL. |
||
396 | */ |
||
397 | public static function get_protocol_normalized_url( $callable, $new_value ) { |
||
421 | |||
422 | /** |
||
423 | * Return URL from option or PHP constant. |
||
424 | * |
||
425 | * @param string $option_name (e.g. 'home'). |
||
426 | * |
||
427 | * @return mixed|null URL. |
||
428 | */ |
||
429 | public static function get_raw_url( $option_name ) { |
||
447 | |||
448 | /** |
||
449 | * Normalize domains by removing www unless declared in the site's option. |
||
450 | * |
||
451 | * @param string $option Option value from the site. |
||
452 | * @param callable $url_function Function retrieving the URL to normalize. |
||
453 | * @return mixed|string URL. |
||
454 | */ |
||
455 | public static function normalize_www_in_url( $option, $url_function ) { |
||
483 | |||
484 | /** |
||
485 | * Return filtered value of get_plugins. |
||
486 | * |
||
487 | * @return mixed|void |
||
488 | */ |
||
489 | public static function get_plugins() { |
||
497 | |||
498 | /** |
||
499 | * Get custom action link tags that the plugin is using |
||
500 | * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name) |
||
501 | * |
||
502 | * @param string $plugin_file_singular Particular plugin. |
||
503 | * @return array of plugin action links (key: link name value: url) |
||
504 | */ |
||
505 | public static function get_plugins_action_links( $plugin_file_singular = null ) { |
||
519 | |||
520 | /** |
||
521 | * Return the WP version as defined in the $wp_version global. |
||
522 | * |
||
523 | * @return string |
||
524 | */ |
||
525 | public static function wp_version() { |
||
529 | |||
530 | /** |
||
531 | * Return site icon url used on the site. |
||
532 | * |
||
533 | * @param int $size Size of requested icon in pixels. |
||
534 | * @return mixed|string|void |
||
535 | */ |
||
536 | public static function site_icon_url( $size = 512 ) { |
||
540 | |||
541 | /** |
||
542 | * Return roles registered on the site. |
||
543 | * |
||
544 | * @return array |
||
545 | */ |
||
546 | public static function roles() { |
||
550 | |||
551 | /** |
||
552 | * Determine time zone from WordPress' options "timezone_string" |
||
553 | * and "gmt_offset". |
||
554 | * |
||
555 | * 1. Check if `timezone_string` is set and return it. |
||
556 | * 2. Check if `gmt_offset` is set, formats UTC-offset from it and return it. |
||
557 | * 3. Default to "UTC+0" if nothing is set. |
||
558 | * |
||
559 | * Note: This function is specifically not using wp_timezone() to keep consistency with |
||
560 | * the existing formatting of the timezone string. |
||
561 | * |
||
562 | * @return string |
||
563 | */ |
||
564 | public static function get_timezone() { |
||
584 | |||
585 | /** |
||
586 | * Return list of paused themes. |
||
587 | * |
||
588 | * @todo Remove function_exists check when WP 5.2 is the minimum. |
||
589 | * |
||
590 | * @return array|bool Array of paused themes or false if unsupported. |
||
591 | */ |
||
592 | public static function get_paused_themes() { |
||
599 | |||
600 | /** |
||
601 | * Return list of paused plugins. |
||
602 | * |
||
603 | * @todo Remove function_exists check when WP 5.2 is the minimum. |
||
604 | * |
||
605 | * @return array|bool Array of paused plugins or false if unsupported. |
||
606 | */ |
||
607 | public static function get_paused_plugins() { |
||
614 | } |
||
615 |
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.