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 | private $results = array(); |
||
| 10 | |||
| 11 | private $expected = array(); |
||
| 12 | |||
| 13 | private $success = array( |
||
| 14 | 'plugin' => array(), |
||
| 15 | 'theme' => array(), |
||
| 16 | ); |
||
| 17 | |||
| 18 | private $failed = array( |
||
| 19 | 'plugin' => array(), |
||
| 20 | 'theme' => array(), |
||
| 21 | ); |
||
| 22 | |||
| 23 | private static $instance = null; |
||
| 24 | |||
| 25 | static function init() { |
||
| 26 | if ( is_null( self::$instance ) ) { |
||
| 27 | self::$instance = new Jetpack_Autoupdate; |
||
| 28 | } |
||
| 29 | return self::$instance; |
||
| 30 | } |
||
| 31 | |||
| 32 | private function __construct() { |
||
| 33 | if ( Jetpack::is_module_active( 'manage' ) ) { |
||
| 34 | add_filter( 'auto_update_plugin', array( $this, 'autoupdate_plugin' ), 10, 2 ); |
||
| 35 | add_filter( 'auto_update_theme', array( $this, 'autoupdate_theme' ), 10, 2 ); |
||
| 36 | add_filter( 'auto_update_core', array( $this, 'autoupdate_core' ), 10, 2 ); |
||
| 37 | add_action( 'automatic_updates_complete', array( $this, 'automatic_updates_complete' ), 999, 1 ); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | View Code Duplication | public function autoupdate_plugin( $update, $item ) { |
|
| 42 | $autoupdate_plugin_list = Jetpack_Options::get_option( 'autoupdate_plugins', array() ); |
||
| 43 | if ( in_array( $item->plugin, $autoupdate_plugin_list ) ) { |
||
| 44 | $this->expect( $item->plugin, 'plugin' ); |
||
| 45 | return true; |
||
| 46 | } |
||
| 47 | return $update; |
||
| 48 | } |
||
| 49 | |||
| 50 | View Code Duplication | public function autoupdate_theme( $update, $item ) { |
|
| 51 | $autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() ); |
||
| 52 | if ( in_array( $item->theme , $autoupdate_theme_list) ) { |
||
| 53 | $this->expect( $item->theme, 'theme' ); |
||
| 54 | return true; |
||
| 55 | } |
||
| 56 | return $update; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function autoupdate_core( $update, $item ) { |
||
|
|
|||
| 60 | $autoupdate_core = Jetpack_Options::get_option( 'autoupdate_core', false ); |
||
| 61 | if ( $autoupdate_core ) { |
||
| 62 | return $autoupdate_core; |
||
| 63 | } |
||
| 64 | return $update; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Stores the an item identifier to the expected array. |
||
| 69 | * |
||
| 70 | * @param string $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme' |
||
| 71 | * @param string $type 'plugin' or 'theme' |
||
| 72 | */ |
||
| 73 | private function expect( $item, $type ) { |
||
| 74 | if ( ! isset( $this->expected[ $type ] ) ) { |
||
| 75 | $this->expected[ $type ] = array(); |
||
| 76 | } |
||
| 77 | $this->expected[ $type ][] = $item; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * On completion of an automatic update, let's store the results. |
||
| 82 | * |
||
| 83 | * @param $results - Sent by WP_Automatic_Updater after it completes an autoupdate action. Results may be empty. |
||
| 84 | */ |
||
| 85 | public function automatic_updates_complete( $results ) { |
||
| 102 | |||
| 103 | public function get_log() { |
||
| 104 | return array( |
||
| 105 | 'results' => $this->results, |
||
| 106 | 'failed' => $this->failed, |
||
| 107 | 'success' => $this->success |
||
| 108 | ); |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Iterates through expected items ( plugins or themes ) and compares them to actual results. |
||
| 113 | * |
||
| 114 | * @param $items 'plugin' or 'theme' |
||
| 115 | */ |
||
| 116 | private function log_items( $items ) { |
||
| 134 | |||
| 135 | public function bump_stats() { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Parses the autoupdate results generated by WP_Automatic_Updater and returns a simple array of successful items |
||
| 174 | * |
||
| 175 | * @param string $type 'plugin' or 'theme' |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | private function get_successful_updates( $type ) { |
||
| 200 | |||
| 201 | static function get_possible_failures() { |
||
| 244 | |||
| 245 | } |
||
| 246 | Jetpack_Autoupdate::init(); |
||
| 247 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.