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_List_Table 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_List_Table, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class WPInv_Subscriptions_List_Table extends WP_List_Table { |
||
| 17 | |||
| 18 | public $per_page = 30; |
||
| 19 | public $total_count = 0; |
||
| 20 | public $active_count = 0; |
||
| 21 | public $cancelled_count = 0; |
||
| 22 | public $completed_count = 0; |
||
| 23 | public $expired_count = 0; |
||
| 24 | public $failing_count = 0; |
||
| 25 | public $pending_count = 0; |
||
| 26 | public $stopped_count = 0; |
||
| 27 | public $trialling_count = 0; |
||
| 28 | |||
| 29 | function __construct(){ |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Retrieve the view types |
||
| 44 | * |
||
| 45 | * @access public |
||
| 46 | * @since 2.4 |
||
| 47 | * @return array $views All the views available |
||
| 48 | */ |
||
| 49 | public function get_views() { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Show the search field |
||
| 77 | * |
||
| 78 | * @since 2.5 |
||
| 79 | * @access public |
||
| 80 | * |
||
| 81 | * @param string $text Label for the search box |
||
| 82 | * @param string $input_id ID of the search box |
||
| 83 | * |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | public function search_box( $text, $input_id ) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Render most columns |
||
| 113 | * |
||
| 114 | * @access private |
||
| 115 | * @since 2.4 |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | function column_default( $item, $column_name ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Customer column |
||
| 124 | * |
||
| 125 | * @access private |
||
| 126 | * @since 2.4 |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | function column_customer_id( $item ) { |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * Status column |
||
| 139 | * |
||
| 140 | * @access private |
||
| 141 | * @since 2.4 |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | function column_status( $item ) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Period column |
||
| 150 | * |
||
| 151 | * @access private |
||
| 152 | * @since 2.4 |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | function column_period( $item ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Billing Times column |
||
| 164 | * |
||
| 165 | * @access private |
||
| 166 | * @since 2.4 |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | function column_bill_times( $item ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Initial Amount column |
||
| 175 | * |
||
| 176 | * @access private |
||
| 177 | * @since 2.4 |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | function column_initial_amount( $item ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Renewal date column |
||
| 186 | * |
||
| 187 | * @access private |
||
| 188 | * @since 2.4 |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | function column_renewal_date( $item ) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Payment column |
||
| 197 | * |
||
| 198 | * @access private |
||
| 199 | * @since 2.4 |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | function column_parent_payment_id( $item ) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Product ID column |
||
| 208 | * |
||
| 209 | * @access private |
||
| 210 | * @since 2.4 |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | function column_product_id( $item ) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Render the edit column |
||
| 219 | * |
||
| 220 | * @access private |
||
| 221 | * @since 2.0 |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | function column_actions( $item ) { |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * Retrieve the table columns |
||
| 231 | * |
||
| 232 | * @access private |
||
| 233 | * @since 2.4 |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | |||
| 237 | function get_columns(){ |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Retrieve the current page number |
||
| 255 | * |
||
| 256 | * @access private |
||
| 257 | * @since 2.4 |
||
| 258 | * @return int |
||
| 259 | */ |
||
| 260 | function get_paged() { |
||
| 263 | |||
| 264 | public function get_subscription_counts() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Setup the final data for the table |
||
| 284 | * |
||
| 285 | * @access private |
||
| 286 | * @since 2.4 |
||
| 287 | * @uses $this->_column_headers |
||
| 288 | * @uses $this->items |
||
| 289 | * @uses $this->get_columns() |
||
| 290 | * @uses $this->get_sortable_columns() |
||
| 291 | * @uses $this->get_pagenum() |
||
| 292 | * @uses $this->set_pagination_args() |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | function prepare_items() { |
||
| 354 | } |
||
| 355 |
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.