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_Subscription_Reports_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_Subscription_Reports_Table, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class WPInv_Subscription_Reports_Table extends WP_List_Table { |
||
25 | |||
26 | /** |
||
27 | * Number of results to show per page |
||
28 | * |
||
29 | * @since 1.0.0 |
||
30 | */ |
||
31 | |||
32 | public $per_page = 20; |
||
33 | public $total_count = 0; |
||
34 | public $active_count = 0; |
||
35 | public $pending_count = 0; |
||
36 | public $expired_count = 0; |
||
37 | public $completed_count = 0; |
||
38 | public $trialling_count = 0; |
||
39 | public $cancelled_count = 0; |
||
40 | public $failing_count = 0; |
||
41 | |||
42 | /** |
||
43 | * Get things started |
||
44 | * |
||
45 | * @access private |
||
46 | * @since 1.0.0 |
||
47 | * @return void |
||
|
|||
48 | */ |
||
49 | function __construct(){ |
||
62 | |||
63 | /** |
||
64 | * Retrieve the view types |
||
65 | * |
||
66 | * @access public |
||
67 | * @since 1.0.0 |
||
68 | * @return array $views All the views available |
||
69 | */ |
||
70 | public function get_views() { |
||
95 | |||
96 | /** |
||
97 | * Show the search field |
||
98 | * |
||
99 | * @since 2.5 |
||
100 | * @access public |
||
101 | * |
||
102 | * @param string $text Label for the search box |
||
103 | * @param string $input_id ID of the search box |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | public function search_box( $text, $input_id ) { |
||
131 | |||
132 | /** |
||
133 | * Render most columns |
||
134 | * |
||
135 | * @access private |
||
136 | * @since 1.0.0 |
||
137 | * @return string |
||
138 | */ |
||
139 | function column_default( $item, $column_name ) { |
||
142 | |||
143 | /** |
||
144 | * Subscription id column |
||
145 | * |
||
146 | * @access private |
||
147 | * @since 1.0.0 |
||
148 | * @return string |
||
149 | */ |
||
150 | function column_sub_id( $item ) { |
||
153 | |||
154 | /** |
||
155 | * Customer column |
||
156 | * |
||
157 | * @access private |
||
158 | * @since 1.0.0 |
||
159 | * @return string |
||
160 | */ |
||
161 | function column_customer_id( $item ) { |
||
167 | |||
168 | /** |
||
169 | * Status column |
||
170 | * |
||
171 | * @access private |
||
172 | * @since 1.0.0 |
||
173 | * @return string |
||
174 | */ |
||
175 | function column_status( $item ) { |
||
178 | |||
179 | /** |
||
180 | * Period column |
||
181 | * |
||
182 | * @access private |
||
183 | * @since 1.0.0 |
||
184 | * @return string |
||
185 | */ |
||
186 | function column_period( $item ) { |
||
192 | |||
193 | /** |
||
194 | * Billing Times column |
||
195 | * |
||
196 | * @access private |
||
197 | * @since 1.0.0 |
||
198 | * @return string |
||
199 | */ |
||
200 | function column_bill_times( $item ) { |
||
203 | |||
204 | /** |
||
205 | * Initial Amount column |
||
206 | * |
||
207 | * @access private |
||
208 | * @since 1.0.0 |
||
209 | * @return string |
||
210 | */ |
||
211 | function column_initial_amount( $item ) { |
||
214 | |||
215 | /** |
||
216 | * Renewal date column |
||
217 | * |
||
218 | * @access private |
||
219 | * @since 1.0.0 |
||
220 | * @return string |
||
221 | */ |
||
222 | function column_renewal_date( $item ) { |
||
225 | |||
226 | /** |
||
227 | * Payment column |
||
228 | * |
||
229 | * @access private |
||
230 | * @since 1.0.0 |
||
231 | * @return string |
||
232 | */ |
||
233 | function column_parent_payment_id( $item ) { |
||
236 | |||
237 | /** |
||
238 | * Product ID column |
||
239 | * |
||
240 | * @access private |
||
241 | * @since 1.0.0 |
||
242 | * @return string |
||
243 | */ |
||
244 | function column_product_id( $item ) { |
||
247 | |||
248 | /** |
||
249 | * Render the edit column |
||
250 | * |
||
251 | * @access private |
||
252 | * @since 2.0 |
||
253 | * @return string |
||
254 | */ |
||
255 | function column_actions( $item ) { |
||
258 | |||
259 | |||
260 | /** |
||
261 | * Retrieve the table columns |
||
262 | * |
||
263 | * @access private |
||
264 | * @since 1.0.0 |
||
265 | * @return array |
||
266 | */ |
||
267 | |||
268 | function get_columns(){ |
||
284 | |||
285 | /** |
||
286 | * Retrieve the current page number |
||
287 | * |
||
288 | * @access private |
||
289 | * @since 1.0.0 |
||
290 | * @return int |
||
291 | */ |
||
292 | function get_paged() { |
||
295 | |||
296 | /** |
||
297 | * Retrieve the subscription counts |
||
298 | * |
||
299 | * @access public |
||
300 | * @since 1.4 |
||
301 | * @return void |
||
302 | */ |
||
303 | public function get_subscription_counts() { |
||
321 | |||
322 | /** |
||
323 | * Setup the final data for the table |
||
324 | * |
||
325 | * @access private |
||
326 | * @since 1.0.0 |
||
327 | * @uses $this->_column_headers |
||
328 | * @uses $this->items |
||
329 | * @uses $this->get_columns() |
||
330 | * @uses $this->get_sortable_columns() |
||
331 | * @uses $this->get_pagenum() |
||
332 | * @uses $this->set_pagination_args() |
||
333 | * @return array |
||
334 | */ |
||
335 | function prepare_items() { |
||
394 | } |
||
395 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.