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:
| 1 | <?php |
||
| 7 | class Jetpack_Autoupdate { |
||
|
1 ignored issue
–
show
|
|||
| 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() { |
||
| 45 | |||
| 46 | View Code Duplication | function autoupdate_plugin( $update, $item ) { |
|
| 55 | |||
| 56 | View Code Duplication | function autoupdate_theme( $update, $item ) { |
|
| 64 | |||
| 65 | function autoupdate_core( $update, $item ) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Stores the an item identifier to the autoupdate_expected array. |
||
| 75 | * |
||
| 76 | * @param string $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme' |
||
| 77 | * @param string $type 'plugin' or 'theme' |
||
| 78 | */ |
||
| 79 | function expect( $item, $type='plugin' ) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * On completion of an automatic update, let's store the results. |
||
| 86 | * |
||
| 87 | * @param $results - Sent by WP_Automatic_Updater after it completes an autoupdate action. Results may be empty. |
||
| 88 | */ |
||
| 89 | function automatic_updates_complete( $results ) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * On shutdown, let's check to see if we've preformed an automatic update. |
||
| 95 | * If so, let's compare the expected results to the actual results, and log our findings. |
||
| 96 | * |
||
| 97 | * Results are logged locally via Jetpack::log(), and globally via Jetpack::do_stats() |
||
| 98 | */ |
||
| 99 | function log_results() { |
||
| 100 | |||
| 101 | if ( $this->is_updating ) { |
||
| 102 | |||
| 103 | $this->jetpack = Jetpack::init(); |
||
| 104 | $items_to_log = array( 'plugin', 'theme' ); |
||
| 105 | |||
| 106 | foreach( $items_to_log as $items ) { |
||
| 107 | $this->log_items( $items ); |
||
| 108 | } |
||
| 109 | |||
| 110 | $this->jetpack->do_stats( 'server_side' ); |
||
| 111 | $this->jetpack->log( 'autoupdates', $this->log ); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Iterates through expected items ( plugins or themes ) and compares them to actual results. |
||
| 117 | * |
||
| 118 | * @param $items 'plugin' or 'theme' |
||
| 119 | */ |
||
| 120 | function log_items( $items ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Parses the autoupdate results generated by WP_Automatic_Updater and returns a simple array of successful items |
||
| 159 | * |
||
| 160 | * @param string $type 'plugin' or 'theme' |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | private function get_successful_updates( $type = 'plugin' ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Cycles through results generated by WP_Automatic_Updater to find the messages for the given item and item type. |
||
| 188 | * |
||
| 189 | * @param $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme' |
||
| 190 | * @param string $type 'plugin' or 'theme' |
||
| 191 | * |
||
| 192 | * @return bool|string |
||
| 193 | */ |
||
| 194 | private function get_error_message( $item, $type = 'plugin' ) { |
||
| 195 | if ( ! isset( $this->autoupdate_results[ $type ] ) ) { |
||
| 196 | return false; |
||
| 197 | } |
||
| 198 | foreach( $this->autoupdate_results[ $type ] as $result ) { |
||
| 199 | View Code Duplication | switch( $type ) { |
|
| 200 | case 'theme': |
||
| 201 | $id = $result->item->theme; |
||
| 202 | break; |
||
| 203 | default: |
||
| 204 | $id = $result->item->plugin; |
||
| 205 | } |
||
| 206 | if ( $id == $item && isset( $result->messages ) ) { |
||
| 207 | return implode( ', ', $result->messages ); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | return false; |
||
| 211 | } |
||
| 212 | |||
| 213 | } |
||
| 214 | Jetpack_Autoupdate::init(); |
||
| 215 |