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 Jetpack_Sync_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 Jetpack_Sync_Functions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Jetpack_Sync_Functions { |
||
| 8 | const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_'; |
||
| 9 | const HTTPS_CHECK_HISTORY = 5; |
||
| 10 | |||
| 11 | public static function get_modules() { |
||
| 16 | |||
| 17 | public static function get_taxonomies() { |
||
| 30 | |||
| 31 | public static function get_shortcodes() { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Removes any callback data since we will not be able to process it on our side anyways. |
||
| 38 | */ |
||
| 39 | public static function sanitize_taxonomy( $taxonomy ) { |
||
| 65 | |||
| 66 | public static function get_post_types() { |
||
| 71 | |||
| 72 | public static function get_post_type_features() { |
||
| 77 | |||
| 78 | public static function get_hosting_provider() { |
||
| 79 | if ( defined( 'GD_SYSTEM_PLUGIN_DIR' ) || class_exists( '\\WPaaS\\Plugin' ) ) { |
||
| 80 | return 'gd-managed-wp'; |
||
| 81 | } |
||
| 82 | if ( defined( 'MM_BASE_DIR' ) ) { |
||
| 83 | return 'bh'; |
||
| 84 | } |
||
| 85 | if ( defined( 'IS_PRESSABLE' ) ) { |
||
| 86 | return 'pressable'; |
||
| 87 | } |
||
| 88 | if ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ) { |
||
| 89 | return 'wpe'; |
||
| 90 | } |
||
| 91 | if ( defined( 'VIP_GO_ENV' ) && false !== VIP_GO_ENV ) { |
||
| 92 | return 'vip-go'; |
||
| 93 | } |
||
| 94 | return 'unknown'; |
||
| 95 | } |
||
| 96 | |||
| 97 | public static function rest_api_allowed_post_types() { |
||
| 98 | /** This filter is already documented in class.json-api-endpoints.php */ |
||
| 99 | return apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) ); |
||
| 100 | } |
||
| 101 | |||
| 102 | public static function rest_api_allowed_public_metadata() { |
||
| 103 | /** This filter is documented in json-endpoints/class.wpcom-json-api-post-endpoint.php */ |
||
| 104 | return apply_filters( 'rest_api_allowed_public_metadata', array() ); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Finds out if a site is using a version control system. |
||
| 109 | * @return bool |
||
| 110 | **/ |
||
| 111 | public static function is_version_controlled() { |
||
| 112 | |||
| 113 | if ( ! class_exists( 'WP_Automatic_Updater' ) ) { |
||
| 114 | require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
||
| 115 | } |
||
| 116 | $updater = new WP_Automatic_Updater(); |
||
| 117 | |||
| 118 | return (bool) strval( $updater->is_vcs_checkout( $context = ABSPATH ) ); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns true if the site has file write access false otherwise. |
||
| 123 | * @return bool |
||
| 124 | **/ |
||
| 125 | public static function file_system_write_access() { |
||
| 126 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
| 127 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
||
| 128 | } |
||
| 129 | |||
| 130 | require_once( ABSPATH . 'wp-admin/includes/template.php' ); |
||
| 131 | |||
| 132 | $filesystem_method = get_filesystem_method(); |
||
| 133 | if ( 'direct' === $filesystem_method ) { |
||
| 134 | return true; |
||
| 135 | } |
||
| 136 | |||
| 137 | ob_start(); |
||
| 138 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
| 139 | ob_end_clean(); |
||
| 140 | if ( $filesystem_credentials_are_stored ) { |
||
| 141 | return true; |
||
| 142 | } |
||
| 143 | |||
| 144 | return false; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Helper function that is used when getting home or siteurl values. Decides |
||
| 149 | * whether to get the raw or filtered value. |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public static function get_raw_or_filtered_url( $url_type ) { |
||
| 154 | if ( |
||
| 155 | ! Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) || |
||
| 156 | Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) |
||
| 157 | ) { |
||
| 158 | $url = self::get_raw_url( $url_type ); |
||
| 159 | } else { |
||
| 160 | $url_function = ( 'home' == $url_type ) |
||
| 161 | ? 'home_url' |
||
| 162 | : 'site_url'; |
||
| 163 | $url = self::normalize_www_in_url( $url_type, $url_function ); |
||
| 164 | $url = self::get_protocol_normalized_url( $url_function, $url ); |
||
| 165 | } |
||
| 166 | |||
| 167 | return $url; |
||
| 168 | } |
||
| 169 | |||
| 170 | public static function home_url() { |
||
| 171 | $url = self::get_raw_or_filtered_url( 'home' ); |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Allows overriding of the home_url value that is synced back to WordPress.com. |
||
| 175 | * |
||
| 176 | * @since 5.2 |
||
| 177 | * |
||
| 178 | * @param string $home_url |
||
| 179 | */ |
||
| 180 | return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) ); |
||
| 181 | } |
||
| 182 | |||
| 183 | public static function site_url() { |
||
| 184 | $url = self::get_raw_or_filtered_url( 'siteurl' ); |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Allows overriding of the site_url value that is synced back to WordPress.com. |
||
| 188 | * |
||
| 189 | * @since 5.2 |
||
| 190 | * |
||
| 191 | * @param string $site_url |
||
| 192 | */ |
||
| 193 | return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) ); |
||
| 194 | } |
||
| 195 | |||
| 196 | public static function main_network_site_url() { |
||
| 197 | return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() ); |
||
| 198 | } |
||
| 199 | |||
| 200 | public static function get_protocol_normalized_url( $callable, $new_value ) { |
||
| 201 | $option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable; |
||
| 202 | |||
| 203 | $parsed_url = wp_parse_url( $new_value ); |
||
| 204 | if ( ! $parsed_url ) { |
||
| 205 | return $new_value; |
||
| 206 | } |
||
| 207 | |||
| 208 | $scheme = $parsed_url['scheme']; |
||
| 209 | $scheme_history = get_option( $option_key, array() ); |
||
| 210 | $scheme_history[] = $scheme; |
||
| 211 | |||
| 212 | // Limit length to self::HTTPS_CHECK_HISTORY |
||
| 213 | $scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) ); |
||
| 214 | |||
| 215 | update_option( $option_key, $scheme_history ); |
||
| 216 | |||
| 217 | $forced_scheme = in_array( 'https', $scheme_history ) ? 'https' : 'http'; |
||
| 218 | |||
| 219 | return set_url_scheme( $new_value, $forced_scheme ); |
||
| 220 | } |
||
| 221 | |||
| 222 | public static function get_raw_url( $option_name ) { |
||
| 223 | $value = null; |
||
|
|
|||
| 224 | $constant = ( 'home' == $option_name ) |
||
| 225 | ? 'WP_HOME' |
||
| 226 | : 'WP_SITEURL'; |
||
| 227 | |||
| 228 | if ( Jetpack_Constants::is_defined( $constant ) ) { |
||
| 229 | $value = Jetpack_Constants::get_constant( $constant ); |
||
| 230 | } else { |
||
| 231 | // Let's get the option from the database so that we can bypass filters. This will help |
||
| 232 | // ensure that we get more uniform values. |
||
| 233 | $value = Jetpack_Options::get_raw_option( $option_name ); |
||
| 234 | } |
||
| 235 | |||
| 236 | return $value; |
||
| 237 | } |
||
| 238 | |||
| 239 | public static function normalize_www_in_url( $option, $url_function ) { |
||
| 267 | |||
| 268 | public static function get_plugins() { |
||
| 276 | |||
| 277 | public static function wp_version() { |
||
| 282 | |||
| 283 | public static function site_icon_url() { |
||
| 290 | } |
||
| 291 |
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.