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 URL with a normalized protocol. |
||
383 | * |
||
384 | * @param callable $callable Function to retrieve URL option. |
||
385 | * @param string $new_value URL Protocol to set URLs to. |
||
386 | * @return string Normalized URL. |
||
387 | */ |
||
388 | public static function get_protocol_normalized_url( $callable, $new_value ) { |
||
412 | |||
413 | /** |
||
414 | * Return URL from option or PHP constant. |
||
415 | * |
||
416 | * @param string $option_name (e.g. 'home'). |
||
417 | * |
||
418 | * @return mixed|null URL. |
||
419 | */ |
||
420 | public static function get_raw_url( $option_name ) { |
||
438 | |||
439 | /** |
||
440 | * Normalize domains by removing www unless declared in the site's option. |
||
441 | * |
||
442 | * @param string $option Option value from the site. |
||
443 | * @param callable $url_function Function retrieving the URL to normalize. |
||
444 | * @return mixed|string URL. |
||
445 | */ |
||
446 | public static function normalize_www_in_url( $option, $url_function ) { |
||
474 | |||
475 | /** |
||
476 | * Return filtered value of get_plugins. |
||
477 | * |
||
478 | * @return mixed|void |
||
479 | */ |
||
480 | public static function get_plugins() { |
||
488 | |||
489 | /** |
||
490 | * Get custom action link tags that the plugin is using |
||
491 | * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name) |
||
492 | * |
||
493 | * @param string $plugin_file_singular Particular plugin. |
||
494 | * @return array of plugin action links (key: link name value: url) |
||
495 | */ |
||
496 | public static function get_plugins_action_links( $plugin_file_singular = null ) { |
||
510 | |||
511 | /** |
||
512 | * Return the WP version as defined in the $wp_version global. |
||
513 | * |
||
514 | * @return string |
||
515 | */ |
||
516 | public static function wp_version() { |
||
520 | |||
521 | /** |
||
522 | * Return site icon url used on the site. |
||
523 | * |
||
524 | * @param int $size Size of requested icon in pixels. |
||
525 | * @return mixed|string|void |
||
526 | */ |
||
527 | public static function site_icon_url( $size = 512 ) { |
||
531 | |||
532 | /** |
||
533 | * Return roles registered on the site. |
||
534 | * |
||
535 | * @return array |
||
536 | */ |
||
537 | public static function roles() { |
||
541 | |||
542 | /** |
||
543 | * Determine time zone from WordPress' options "timezone_string" |
||
544 | * and "gmt_offset". |
||
545 | * |
||
546 | * 1. Check if `timezone_string` is set and return it. |
||
547 | * 2. Check if `gmt_offset` is set, formats UTC-offset from it and return it. |
||
548 | * 3. Default to "UTC+0" if nothing is set. |
||
549 | * |
||
550 | * Note: This function is specifically not using wp_timezone() to keep consistency with |
||
551 | * the existing formatting of the timezone string. |
||
552 | * |
||
553 | * @return string |
||
554 | */ |
||
555 | public static function get_timezone() { |
||
575 | |||
576 | /** |
||
577 | * Return list of paused themes. |
||
578 | * |
||
579 | * @todo Remove function_exists check when WP 5.2 is the minimum. |
||
580 | * |
||
581 | * @return array|bool Array of paused themes or false if unsupported. |
||
582 | */ |
||
583 | public static function get_paused_themes() { |
||
590 | |||
591 | /** |
||
592 | * Return list of paused plugins. |
||
593 | * |
||
594 | * @todo Remove function_exists check when WP 5.2 is the minimum. |
||
595 | * |
||
596 | * @return array|bool Array of paused plugins or false if unsupported. |
||
597 | */ |
||
598 | public static function get_paused_plugins() { |
||
605 | } |
||
606 |
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.