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 { |
||
1 ignored issue
–
show
|
|||
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() { |
||
31 | |||
32 | private function __construct() { |
||
40 | |||
41 | View Code Duplication | public function autoupdate_plugin( $update, $item ) { |
|
49 | |||
50 | View Code Duplication | public function autoupdate_theme( $update, $item ) { |
|
58 | |||
59 | public function autoupdate_core( $update, $item ) { |
||
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 ) { |
||
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() { |
||
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 |