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 Give_Sales_Log_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 Give_Sales_Log_Table, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Give_Sales_Log_Table extends WP_List_Table { |
||
29 | /** |
||
30 | * Number of results to show per page |
||
31 | * |
||
32 | * @since 1.0 |
||
33 | * @var int |
||
34 | */ |
||
35 | public $per_page = 30; |
||
36 | |||
37 | /** |
||
38 | * Get things started |
||
39 | * |
||
40 | * @since 1.0 |
||
41 | * @see WP_List_Table::__construct() |
||
42 | */ |
||
43 | View Code Duplication | public function __construct() { |
|
55 | |||
56 | /** |
||
57 | * This function renders most of the columns in the list table. |
||
58 | * |
||
59 | * @access public |
||
60 | * @since 1.0 |
||
61 | * |
||
62 | * @param array $item Contains all the data of the discount code |
||
63 | * @param string $column_name The name of the column |
||
64 | * |
||
65 | * @return string Column Name |
||
66 | */ |
||
67 | public function column_default( $item, $column_name ) { |
||
104 | |||
105 | /** |
||
106 | * Retrieve the table columns |
||
107 | * |
||
108 | * @access public |
||
109 | * @since 1.0 |
||
110 | * @return array $columns Array of all the list table columns |
||
111 | */ |
||
112 | public function get_columns() { |
||
125 | |||
126 | /** |
||
127 | * Retrieve the current page number |
||
128 | * |
||
129 | * @access public |
||
130 | * @since 1.0 |
||
131 | * @return int Current page number |
||
132 | */ |
||
133 | public function get_paged() { |
||
136 | |||
137 | /** |
||
138 | * Retrieves the user we are filtering logs by, if any |
||
139 | * |
||
140 | * @access public |
||
141 | * @since 1.0 |
||
142 | * @return mixed int If User ID, string If Email/Login |
||
143 | */ |
||
144 | public function get_filtered_user() { |
||
147 | |||
148 | /** |
||
149 | * Retrieves the ID of the give_form we're filtering logs by |
||
150 | * |
||
151 | * @access public |
||
152 | * @since 1.0 |
||
153 | * @return int Download ID |
||
154 | */ |
||
155 | public function get_filtered_give_form() { |
||
158 | |||
159 | /** |
||
160 | * Retrieves the search query string |
||
161 | * |
||
162 | * @access public |
||
163 | * @since 1.0 |
||
164 | * @return string|bool string If search is present, false otherwise |
||
165 | */ |
||
166 | public function get_search() { |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Display Tablenav (extended) |
||
173 | * |
||
174 | * Display the table navigation above or below the table even when no items in the logs, so nav doesn't disappear |
||
175 | * |
||
176 | * @see : https://github.com/WordImpress/Give/issues/564 |
||
177 | * |
||
178 | * @since 1.4.1 |
||
179 | * @access protected |
||
180 | * |
||
181 | * @param string $which |
||
182 | */ |
||
183 | View Code Duplication | protected function display_tablenav( $which ) { |
|
203 | |||
204 | |||
205 | /** |
||
206 | * Gets the meta query for the log query |
||
207 | * |
||
208 | * This is used to return log entries that match our search query, user query, or form query |
||
209 | * |
||
210 | * @since 1.0 |
||
211 | * @access public |
||
212 | * |
||
213 | * @return array $meta_query |
||
214 | */ |
||
215 | public function get_meta_query() { |
||
277 | |||
278 | /** |
||
279 | * Outputs the log views |
||
280 | * |
||
281 | * @access public |
||
282 | * @since 1.0 |
||
283 | * @return void |
||
284 | */ |
||
285 | function bulk_actions( $which = '' ) { |
||
288 | |||
289 | /** |
||
290 | * Sets up the forms filter |
||
291 | * |
||
292 | * @access public |
||
293 | * @since 1.0 |
||
294 | * @return void |
||
295 | */ |
||
296 | public function give_forms_filter() { |
||
319 | |||
320 | /** |
||
321 | * Gets the log entries for the current view |
||
322 | * |
||
323 | * @access public |
||
324 | * @since 1.0 |
||
325 | * @global object $give_logs Give Logs Object |
||
326 | * @return array $logs_data Array of all the Log entires |
||
327 | */ |
||
328 | public function get_logs() { |
||
382 | |||
383 | /** |
||
384 | * Setup the final data for the table |
||
385 | * |
||
386 | * @access public |
||
387 | * @since 1.0 |
||
388 | * @global object $give_logs Give Logs Object |
||
389 | * @uses Give_Sales_Log_Table::get_columns() |
||
390 | * @uses WP_List_Table::get_sortable_columns() |
||
391 | * @uses Give_Sales_Log_Table::get_pagenum() |
||
392 | * @uses Give_Sales_Log_Table::get_logs() |
||
393 | * @uses Give_Sales_Log_Table::get_log_count() |
||
394 | * @return void |
||
395 | */ |
||
396 | public function prepare_items() { |
||
415 | } |
||
416 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.