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_Donor_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 Give_Donor_List_Table, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Give_Donor_List_Table extends WP_List_Table { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Number of items per page. |
||
| 33 | * |
||
| 34 | * @var int |
||
| 35 | * @since 1.0 |
||
| 36 | */ |
||
| 37 | public $per_page = 30; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Number of donors found. |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | * @since 1.0 |
||
| 44 | */ |
||
| 45 | public $count = 0; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Total donors. |
||
| 49 | * |
||
| 50 | * @var int |
||
| 51 | * @since 1.0 |
||
| 52 | */ |
||
| 53 | public $total = 0; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get things started. |
||
| 57 | * |
||
| 58 | * @since 1.0 |
||
| 59 | * @see WP_List_Table::__construct() |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function __construct() { |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Show the search field. |
||
| 74 | * |
||
| 75 | * @param string $text Label for the search box. |
||
| 76 | * @param string $input_id ID of the search box. |
||
| 77 | * |
||
| 78 | * @since 1.0 |
||
| 79 | * @access public |
||
| 80 | * |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | public function search_box( $text, $input_id ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * This function renders most of the columns in the list table. |
||
| 106 | * |
||
| 107 | * @param array $donor Contains all the data of the donors. |
||
| 108 | * @param string $column_name The name of the column. |
||
| 109 | * |
||
| 110 | * @access public |
||
| 111 | * @since 1.0 |
||
| 112 | * |
||
| 113 | * @return string Column Name. |
||
| 114 | */ |
||
| 115 | public function column_default( $donor, $column_name ) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * For CheckBox Column |
||
| 145 | * |
||
| 146 | * @param array $donor Donor Data. |
||
| 147 | * |
||
| 148 | * @access public |
||
| 149 | * @since 1.8.16 |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function column_cb( $donor ){ |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Column name. |
||
| 163 | * |
||
| 164 | * @param array $donor Donor Data. |
||
| 165 | * |
||
| 166 | * @access public |
||
| 167 | * @since 1.0 |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function column_name( $donor ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Retrieve the table columns. |
||
| 182 | * |
||
| 183 | * @access public |
||
| 184 | * @since 1.0 |
||
| 185 | * |
||
| 186 | * @return array $columns Array of all the list table columns. |
||
| 187 | */ |
||
| 188 | public function get_columns() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the sortable columns. |
||
| 204 | * |
||
| 205 | * @access public |
||
| 206 | * @since 2.1 |
||
| 207 | * @return array Array of all the sortable columns. |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function get_sortable_columns() { |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Retrieve row actions. |
||
| 223 | * |
||
| 224 | * @param array $donor Donor Data. |
||
| 225 | * |
||
| 226 | * @since 1.7 |
||
| 227 | * @access public |
||
| 228 | * |
||
| 229 | * @return array An array of action links. |
||
| 230 | */ |
||
| 231 | public function get_row_actions( $donor ) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Retrieve the current page number. |
||
| 245 | * |
||
| 246 | * @access public |
||
| 247 | * @since 1.0 |
||
| 248 | * |
||
| 249 | * @return int Current page number. |
||
| 250 | */ |
||
| 251 | public function get_paged() { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Retrieves the search query string. |
||
| 257 | * |
||
| 258 | * @access public |
||
| 259 | * @since 1.0 |
||
| 260 | * |
||
| 261 | * @return mixed string If search is present, false otherwise. |
||
| 262 | */ |
||
| 263 | public function get_search() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the Bulk Actions. |
||
| 269 | * |
||
| 270 | * @access public |
||
| 271 | * @since 1.8.16 |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | public function get_bulk_actions() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Process the Bulk Actions. |
||
| 284 | * |
||
| 285 | * @access public |
||
| 286 | * @since 1.8.16 |
||
| 287 | * |
||
| 288 | * @return void |
||
| 289 | */ |
||
| 290 | public function process_bulk_action() { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Generate the table navigation above or below the table |
||
| 336 | * |
||
| 337 | * @param string $which Position to trigger i.e. Top/Bottom. |
||
| 338 | * |
||
| 339 | * @access protected |
||
| 340 | * @since 1.8.16 |
||
| 341 | */ |
||
| 342 | protected function display_tablenav( $which ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Retrieves the donor data from db. |
||
| 364 | * |
||
| 365 | * @access public |
||
| 366 | * @since 1.0 |
||
| 367 | * |
||
| 368 | * @return array $data The Donor data. |
||
| 369 | */ |
||
| 370 | View Code Duplication | public function donor_data() { |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Get donor count. |
||
| 401 | * |
||
| 402 | * @since 1.8.1 |
||
| 403 | * @access private |
||
| 404 | */ |
||
| 405 | View Code Duplication | private function get_donor_count() { |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Get donor query. |
||
| 418 | * |
||
| 419 | * @since 1.8.1 |
||
| 420 | * @access public |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | View Code Duplication | public function get_donor_query() { |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Setup the final data for the table. |
||
| 453 | * |
||
| 454 | * @access public |
||
| 455 | * @since 1.0 |
||
| 456 | * |
||
| 457 | * @return void |
||
| 458 | */ |
||
| 459 | View Code Duplication | public function prepare_items() { |
|
| 479 | } |
||
| 480 |
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.