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() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Return array of allowed REST API post types. |
||
| 193 | * |
||
| 194 | * @return array Array of allowed post types. |
||
| 195 | */ |
||
| 196 | public static function rest_api_allowed_post_types() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Return array of allowed REST API public metadata. |
||
| 203 | * |
||
| 204 | * @return array Array of allowed metadata. |
||
| 205 | */ |
||
| 206 | public static function rest_api_allowed_public_metadata() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Finds out if a site is using a version control system. |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | **/ |
||
| 216 | public static function is_version_controlled() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns true if the site has file write access false otherwise. |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | **/ |
||
| 231 | public static function file_system_write_access() { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Helper function that is used when getting home or siteurl values. Decides |
||
| 260 | * whether to get the raw or filtered value. |
||
| 261 | * |
||
| 262 | * @param string $url_type URL to get, home or siteurl. |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public static function get_raw_or_filtered_url( $url_type ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Return the escaped home_url. |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public static function home_url() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Return the escaped siteurl. |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public static function site_url() { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Return main site URL with a normalized protocol. |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | public static function main_network_site_url() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Return URL with a normalized protocol. |
||
| 331 | * |
||
| 332 | * @param callable $callable Function to retrieve URL option. |
||
| 333 | * @param string $new_value URL Protocol to set URLs to. |
||
| 334 | * @return string Normalized URL. |
||
| 335 | */ |
||
| 336 | public static function get_protocol_normalized_url( $callable, $new_value ) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Return URL from option or PHP constant. |
||
| 363 | * |
||
| 364 | * @param string $option_name (e.g. 'home'). |
||
| 365 | * |
||
| 366 | * @return mixed|null URL. |
||
| 367 | */ |
||
| 368 | public static function get_raw_url( $option_name ) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Normalize domains by removing www unless declared in the site's option. |
||
| 389 | * |
||
| 390 | * @param string $option Option value from the site. |
||
| 391 | * @param callable $url_function Function retrieving the URL to normalize. |
||
| 392 | * @return mixed|string URL. |
||
| 393 | */ |
||
| 394 | public static function normalize_www_in_url( $option, $url_function ) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Return filtered value of get_plugins. |
||
| 425 | * |
||
| 426 | * @return mixed|void |
||
| 427 | */ |
||
| 428 | public static function get_plugins() { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get custom action link tags that the plugin is using |
||
| 439 | * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name) |
||
| 440 | * |
||
| 441 | * @param string $plugin_file_singular Particular plugin. |
||
| 442 | * @return array of plugin action links (key: link name value: url) |
||
| 443 | */ |
||
| 444 | public static function get_plugins_action_links( $plugin_file_singular = null ) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Return the WP version as defined in the $wp_version global. |
||
| 461 | * |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | public static function wp_version() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Return site icon url used on the site. |
||
| 471 | * |
||
| 472 | * @param int $size Size of requested icon in pixels. |
||
| 473 | * @return mixed|string|void |
||
| 474 | */ |
||
| 475 | public static function site_icon_url( $size = 512 ) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Return roles registered on the site. |
||
| 482 | * |
||
| 483 | * @return array |
||
| 484 | */ |
||
| 485 | public static function roles() { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Determine time zone from WordPress' options "timezone_string" |
||
| 492 | * and "gmt_offset". |
||
| 493 | * |
||
| 494 | * 1. Check if `timezone_string` is set and return it. |
||
| 495 | * 2. Check if `gmt_offset` is set, formats UTC-offset from it and return it. |
||
| 496 | * 3. Default to "UTC+0" if nothing is set. |
||
| 497 | * |
||
| 498 | * Note: This function is specifically not using wp_timezone() to keep consistency with |
||
| 499 | * the existing formatting of the timezone string. |
||
| 500 | * |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | public static function get_timezone() { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Return list of paused themes. |
||
| 526 | * |
||
| 527 | * @todo Remove function_exists check when WP 5.2 is the minimum. |
||
| 528 | * |
||
| 529 | * @return array|bool Array of paused themes or false if unsupported. |
||
| 530 | */ |
||
| 531 | public static function get_paused_themes() { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Return list of paused plugins. |
||
| 541 | * |
||
| 542 | * @todo Remove function_exists check when WP 5.2 is the minimum. |
||
| 543 | * |
||
| 544 | * @return array|bool Array of paused plugins or false if unsupported. |
||
| 545 | */ |
||
| 546 | public static function get_paused_plugins() { |
||
| 553 | } |
||
| 554 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.