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 WPInv_Subscriptions 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 WPInv_Subscriptions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class WPInv_Subscriptions { |
||
| 16 | |||
| 17 | private static $instance; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Main WPInv_Subscriptions Instance |
||
| 21 | */ |
||
| 22 | public static function instance() { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Constructor -- prevent new instances |
||
| 34 | * |
||
| 35 | * @since 1.0.0 |
||
| 36 | */ |
||
| 37 | private function __construct(){ |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Get things started |
||
| 43 | * |
||
| 44 | * Sets up inits actions and filters |
||
| 45 | * |
||
| 46 | * @since 1.0.0 |
||
| 47 | */ |
||
| 48 | function init() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Setup plugin constants. |
||
| 58 | * |
||
| 59 | * @access private |
||
| 60 | * @since 1.0.0 |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | private function setup_constants() { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Add our actions |
||
| 73 | * |
||
| 74 | * @since 1.0.0 |
||
| 75 | * @return void |
||
| 76 | */ |
||
| 77 | private function actions() { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Add our filters |
||
| 90 | * |
||
| 91 | * @since 1.0 |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | private function filters() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Register our Subscriptions submenu |
||
| 100 | * |
||
| 101 | * @since 2.4 |
||
| 102 | * @return void |
||
| 103 | */ |
||
| 104 | public function wpinv_subscriptions_list() { |
||
| 114 | |||
| 115 | public function notices() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Every wpinv_action present in $_GET is called using WordPress's do_action function. |
||
| 154 | * These functions are called on init. |
||
| 155 | * |
||
| 156 | * @since 1.0.0 |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | function wpinv_get_actions() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Every wpinv_action present in $_POST is called using WordPress's do_action function. |
||
| 167 | * These functions are called on init. |
||
| 168 | * |
||
| 169 | * @since 1.0.0 |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | function wpinv_post_actions() { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get pretty subscription frequency |
||
| 180 | * |
||
| 181 | * @param $period |
||
| 182 | * @param int $frequency_count The frequency of the period. |
||
| 183 | * @return mixed|string|void |
||
| 184 | */ |
||
| 185 | public static function wpinv_get_pretty_subscription_frequency( $period, $frequency_count = 1) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Handles cancellation requests for a subscription |
||
| 212 | * |
||
| 213 | * @access public |
||
| 214 | * @since 1.0.0 |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | public function wpinv_process_cancellation( $data ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Create/Update subscription on invoice created/updated |
||
| 267 | * |
||
| 268 | * @access public |
||
| 269 | * @since 1.0.0 |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | public function wpinv_add_update_subscription( $invoice_id, $post ) { |
||
| 344 | } |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.