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() { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Return array of taxonomies registered on the site. |
||
| 36 | * |
||
| 37 | * @return array |
||
| 38 | */ |
||
| 39 | public static function get_taxonomies() { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Return array of registered shortcodes. |
||
| 53 | * |
||
| 54 | * @return array |
||
| 55 | */ |
||
| 56 | public static function get_shortcodes() { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Removes any callback data since we will not be able to process it on our side anyways. |
||
| 63 | * |
||
| 64 | * @param \WP_Taxonomy $taxonomy \WP_Taxonomy item. |
||
| 65 | * |
||
| 66 | * @return mixed|null |
||
| 67 | */ |
||
| 68 | public static function sanitize_taxonomy( $taxonomy ) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Return array of registered post types. |
||
| 97 | * |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public static function get_post_types() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Sanitizes by cloning post type object. |
||
| 115 | * |
||
| 116 | * @param object $post_type \WP_Post_Type. |
||
| 117 | * |
||
| 118 | * @return object |
||
| 119 | */ |
||
| 120 | public static function sanitize_post_type( $post_type ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Return information about a synced post type. |
||
| 133 | * |
||
| 134 | * @param array $sanitized_post_type Array of args used in constructing \WP_Post_Type. |
||
| 135 | * @param string $post_type Post type name. |
||
| 136 | * |
||
| 137 | * @return object \WP_Post_Type |
||
| 138 | */ |
||
| 139 | public static function expand_synced_post_type( $sanitized_post_type, $post_type ) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns site's post_type_features. |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | public static function get_post_type_features() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Return hosting provider. |
||
| 162 | * |
||
| 163 | * Uses a set of known constants, classes, or functions to help determine the hosting platform. |
||
| 164 | * |
||
| 165 | * @return string Hosting provider. |
||
| 166 | */ |
||
| 167 | public static function get_hosting_provider() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Return a hosting provider using a set of known constants. |
||
| 187 | * |
||
| 188 | * @return mixed A host identifier string or false. |
||
| 189 | */ |
||
| 190 | public function get_hosting_provider_by_known_constant() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Return a hosting provider using a set of known classes. |
||
| 215 | * |
||
| 216 | * @return mixed A host identifier string or false. |
||
| 217 | */ |
||
| 218 | public function get_hosting_provider_by_known_class() { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Return a hosting provider using a set of known functions. |
||
| 232 | * |
||
| 233 | * @return mixed A host identifier string or false. |
||
| 234 | */ |
||
| 235 | public function get_hosting_provider_by_known_function() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Return array of allowed REST API post types. |
||
| 249 | * |
||
| 250 | * @return array Array of allowed post types. |
||
| 251 | */ |
||
| 252 | public static function rest_api_allowed_post_types() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Return array of allowed REST API public metadata. |
||
| 259 | * |
||
| 260 | * @return array Array of allowed metadata. |
||
| 261 | */ |
||
| 262 | public static function rest_api_allowed_public_metadata() { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Finds out if a site is using a version control system. |
||
| 279 | * |
||
| 280 | * @return bool |
||
| 281 | **/ |
||
| 282 | public static function is_version_controlled() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Returns true if the site has file write access false otherwise. |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | **/ |
||
| 297 | public static function file_system_write_access() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Helper function that is used when getting home or siteurl values. Decides |
||
| 326 | * whether to get the raw or filtered value. |
||
| 327 | * |
||
| 328 | * @param string $url_type URL to get, home or siteurl. |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public static function get_raw_or_filtered_url( $url_type ) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Return the escaped home_url. |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public static function home_url() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Return the escaped siteurl. |
||
| 370 | * |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | public static function site_url() { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Return main site URL with a normalized protocol. |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | public static function main_network_site_url() { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Return main site WordPress.com site ID. |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | public static function main_network_site_wpcom_id() { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Return URL with a normalized protocol. |
||
| 420 | * |
||
| 421 | * @param callable $callable Function to retrieve URL option. |
||
| 422 | * @param string $new_value URL Protocol to set URLs to. |
||
| 423 | * @return string Normalized URL. |
||
| 424 | */ |
||
| 425 | public static function get_protocol_normalized_url( $callable, $new_value ) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Return URL from option or PHP constant. |
||
| 452 | * |
||
| 453 | * @param string $option_name (e.g. 'home'). |
||
| 454 | * |
||
| 455 | * @return mixed|null URL. |
||
| 456 | */ |
||
| 457 | public static function get_raw_url( $option_name ) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Normalize domains by removing www unless declared in the site's option. |
||
| 478 | * |
||
| 479 | * @param string $option Option value from the site. |
||
| 480 | * @param callable $url_function Function retrieving the URL to normalize. |
||
| 481 | * @return mixed|string URL. |
||
| 482 | */ |
||
| 483 | public static function normalize_www_in_url( $option, $url_function ) { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Return filtered value of get_plugins. |
||
| 514 | * |
||
| 515 | * @return mixed|void |
||
| 516 | */ |
||
| 517 | public static function get_plugins() { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get custom action link tags that the plugin is using |
||
| 528 | * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name) |
||
| 529 | * |
||
| 530 | * @param string $plugin_file_singular Particular plugin. |
||
| 531 | * @return array of plugin action links (key: link name value: url) |
||
| 532 | */ |
||
| 533 | public static function get_plugins_action_links( $plugin_file_singular = null ) { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Return the WP version as defined in the $wp_version global. |
||
| 550 | * |
||
| 551 | * @return string |
||
| 552 | */ |
||
| 553 | public static function wp_version() { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Return site icon url used on the site. |
||
| 560 | * |
||
| 561 | * @param int $size Size of requested icon in pixels. |
||
| 562 | * @return mixed|string|void |
||
| 563 | */ |
||
| 564 | public static function site_icon_url( $size = 512 ) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Return roles registered on the site. |
||
| 571 | * |
||
| 572 | * @return array |
||
| 573 | */ |
||
| 574 | public static function roles() { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Determine time zone from WordPress' options "timezone_string" |
||
| 581 | * and "gmt_offset". |
||
| 582 | * |
||
| 583 | * 1. Check if `timezone_string` is set and return it. |
||
| 584 | * 2. Check if `gmt_offset` is set, formats UTC-offset from it and return it. |
||
| 585 | * 3. Default to "UTC+0" if nothing is set. |
||
| 586 | * |
||
| 587 | * Note: This function is specifically not using wp_timezone() to keep consistency with |
||
| 588 | * the existing formatting of the timezone string. |
||
| 589 | * |
||
| 590 | * @return string |
||
| 591 | */ |
||
| 592 | public static function get_timezone() { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Return list of paused themes. |
||
| 615 | * |
||
| 616 | * @return array|bool Array of paused themes or false if unsupported. |
||
| 617 | */ |
||
| 618 | public static function get_paused_themes() { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Return list of paused plugins. |
||
| 625 | * |
||
| 626 | * @return array|bool Array of paused plugins or false if unsupported. |
||
| 627 | */ |
||
| 628 | public static function get_paused_plugins() { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Return the theme's supported features. |
||
| 635 | * Used for syncing the supported feature that we care about. |
||
| 636 | * |
||
| 637 | * @return array List of features that the theme supports. |
||
| 638 | */ |
||
| 639 | public static function get_theme_support() { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion. |
||
| 655 | * |
||
| 656 | * @since 9.5.0 |
||
| 657 | * |
||
| 658 | * @param array|obj $any Source data to be cleaned up. |
||
| 659 | * @param array $seen_nodes Built array of nodes. |
||
| 660 | * |
||
| 661 | * @return array |
||
| 662 | */ |
||
| 663 | public static function json_wrap( &$any, $seen_nodes = array() ) { |
||
| 693 | } |
||
| 694 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: