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_Autoupdate 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_Autoupdate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Jetpack_Autoupdate { |
||
| 8 | |||
| 9 | public $updates_allowed; |
||
| 10 | public $jetpack; |
||
| 11 | public $autoupdate_results; |
||
| 12 | public $is_updating = false; |
||
| 13 | |||
| 14 | public $autoupdate_expected = array( |
||
| 15 | 'plugin'=> array(), |
||
| 16 | 'theme' => array(), |
||
| 17 | ); |
||
| 18 | |||
| 19 | public $log = array( |
||
| 20 | 'plugin' => array(), |
||
| 21 | 'theme' => array(), |
||
| 22 | ); |
||
| 23 | |||
| 24 | private static $instance = null; |
||
| 25 | |||
| 26 | static function init() { |
||
| 32 | |||
| 33 | private function __construct() { |
||
| 53 | |||
| 54 | View Code Duplication | function autoupdate_plugin( $update, $item ) { |
|
| 63 | |||
| 64 | View Code Duplication | function autoupdate_theme( $update, $item ) { |
|
| 72 | |||
| 73 | function autoupdate_core( $update, $item ) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Stores the an item identifier to the autoupdate_expected array. |
||
| 83 | * |
||
| 84 | * @param string $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme' |
||
| 85 | * @param string $type 'plugin' or 'theme' |
||
| 86 | */ |
||
| 87 | function expect( $item, $type='plugin' ) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Calculates available updates and saves them to a Jetpack Option |
||
| 94 | * Update data is saved in the following schema: |
||
| 95 | * |
||
| 96 | * array ( |
||
| 97 | * 'plugins' => (int) number of plugin updates available |
||
| 98 | * 'themes' => (int) number of theme updates available |
||
| 99 | * 'wordpress' => (int) number of wordpress core updates available |
||
| 100 | * 'translations' => (int) number of translation updates available |
||
| 101 | * 'total' => (int) total of all available updates |
||
| 102 | * 'wp_version' => (string) the current version of WordPress that is running |
||
| 103 | * 'wp_update_version' => (string) the latest available version of WordPress, only present if a WordPress update is needed |
||
| 104 | * 'site_is_version_controlled' => (bool) is the site under version control |
||
| 105 | * ) |
||
| 106 | */ |
||
| 107 | function save_update_data() { |
||
| 108 | global $wp_version; |
||
| 109 | |||
| 110 | $update_data = wp_get_update_data(); |
||
| 111 | |||
| 112 | // Stores the individual update counts as well as the total count. |
||
| 113 | if ( isset( $update_data['counts'] ) ) { |
||
| 114 | $updates = $update_data['counts']; |
||
| 115 | } |
||
| 116 | |||
| 117 | // Stores the current version of WordPress. |
||
| 118 | $updates['wp_version'] = $wp_version; |
||
| 119 | |||
| 120 | // If we need to update WordPress core, let's find the latest version number. |
||
| 121 | View Code Duplication | if ( ! empty( $updates['wordpress'] ) ) { |
|
| 122 | $cur = get_preferred_from_update_core(); |
||
| 123 | if ( isset( $cur->response ) && 'upgrade' === $cur->response ) { |
||
| 124 | $updates['wp_update_version'] = $cur->current; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | $updates['site_is_version_controlled'] = (bool) $this->is_version_controlled(); |
||
| 129 | Jetpack_Options::update_option( 'updates', $updates ); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Finds out if a site is using a version control system. |
||
| 134 | * We'll store that information as a transient with a 24 expiration. |
||
| 135 | * We only need to check once per day. |
||
| 136 | * |
||
| 137 | * @return string ( '1' | '0' ) |
||
| 138 | */ |
||
| 139 | function is_version_controlled() { |
||
| 140 | $is_version_controlled = get_transient( 'jetpack_site_is_vcs' ); |
||
| 141 | |||
| 142 | if ( false === $is_version_controlled ) { |
||
| 143 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
||
| 144 | $updater = new WP_Automatic_Updater(); |
||
| 145 | $is_version_controlled = strval( $updater->is_vcs_checkout( $context = ABSPATH ) ); |
||
| 146 | // transients should not be empty |
||
| 147 | if ( empty( $is_version_controlled ) ) { |
||
| 148 | $is_version_controlled = '0'; |
||
| 149 | } |
||
| 150 | set_transient( 'jetpack_site_is_vcs', $is_version_controlled, DAY_IN_SECONDS ); |
||
| 151 | } |
||
| 152 | |||
| 153 | return $is_version_controlled; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * On completion of an automatic update, let's store the results. |
||
| 158 | * |
||
| 159 | * @param $results - Sent by WP_Automatic_Updater after it completes an autoupdate action. Results may be empty. |
||
| 160 | */ |
||
| 161 | function automatic_updates_complete( $results ) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * On shutdown, let's check to see if we've preformed an automatic update. |
||
| 167 | * If so, let's compare the expected results to the actual results, and log our findings. |
||
| 168 | * |
||
| 169 | * Results are logged locally via Jetpack::log(), and globally via Jetpack::do_stats() |
||
| 170 | */ |
||
| 171 | function log_results() { |
||
| 172 | |||
| 173 | if ( $this->is_updating ) { |
||
| 174 | |||
| 175 | $this->jetpack = Jetpack::init(); |
||
| 176 | $items_to_log = array( 'plugin', 'theme' ); |
||
| 177 | |||
| 178 | foreach( $items_to_log as $items ) { |
||
| 179 | $this->log_items( $items ); |
||
| 180 | } |
||
| 181 | |||
| 182 | $this->jetpack->do_stats( 'server_side' ); |
||
| 183 | $this->jetpack->log( 'autoupdates', $this->log ); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Iterates through expected items ( plugins or themes ) and compares them to actual results. |
||
| 189 | * |
||
| 190 | * @param $items 'plugin' or 'theme' |
||
| 191 | */ |
||
| 192 | function log_items( $items ) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Parses the autoupdate results generated by WP_Automatic_Updater and returns a simple array of successful items |
||
| 219 | * |
||
| 220 | * @param string $type 'plugin' or 'theme' |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | private function get_successful_updates( $type = 'plugin' ) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Cycles through results generated by WP_Automatic_Updater to find the messages for the given item and item type. |
||
| 248 | * |
||
| 249 | * @param $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme' |
||
| 250 | * @param string $type 'plugin' or 'theme' |
||
| 251 | * |
||
| 252 | * @return bool|string |
||
| 253 | */ |
||
| 254 | private function get_error_message( $item, $type = 'plugin' ) { |
||
| 255 | if ( ! isset( $this->autoupdate_results[ $type ] ) ) { |
||
| 256 | return false; |
||
| 257 | } |
||
| 258 | foreach( $this->autoupdate_results[ $type ] as $result ) { |
||
| 259 | View Code Duplication | switch( $type ) { |
|
| 260 | case 'theme': |
||
| 261 | $id = $result->item->theme; |
||
| 262 | break; |
||
| 263 | default: |
||
| 264 | $id = $result->item->plugin; |
||
| 265 | } |
||
| 266 | if ( $id == $item && isset( $result->messages ) ) { |
||
| 267 | return implode( ', ', $result->messages ); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | return false; |
||
| 271 | } |
||
| 272 | |||
| 273 | } |
||
| 274 | Jetpack_Autoupdate::init(); |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.