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:
| 1 | <?php |
||
| 29 | class Give_Form_Reports_Table extends WP_List_Table { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var int Number of items per page |
||
| 33 | * @since 1.0 |
||
| 34 | */ |
||
| 35 | public $per_page = 30; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var object Query results of all the donation forms |
||
| 39 | * @since 1.0 |
||
| 40 | */ |
||
| 41 | private $donation_forms; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int Total number of Donation Forms |
||
| 45 | * @since 1.8.11 |
||
| 46 | */ |
||
| 47 | public $count; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get things started |
||
| 51 | * |
||
| 52 | * @since 1.0 |
||
| 53 | * @see WP_List_Table::__construct() |
||
| 54 | */ |
||
| 55 | View Code Duplication | public function __construct() { |
|
| 69 | |||
| 70 | /** |
||
| 71 | * This function renders most of the columns in the list table. |
||
| 72 | * |
||
| 73 | * @param array $item Contains all the data of the donation form |
||
| 74 | * @param string $column_name The name of the column |
||
| 75 | * |
||
| 76 | * @access public |
||
| 77 | * @since 1.0 |
||
| 78 | * |
||
| 79 | * @return string Column Name |
||
| 80 | */ |
||
| 81 | public function column_default( $item, $column_name ) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Retrieve the table columns |
||
| 112 | * |
||
| 113 | * @access public |
||
| 114 | * @since 1.0 |
||
| 115 | * |
||
| 116 | * @return array $columns Array of all the list table columns |
||
| 117 | */ |
||
| 118 | public function get_columns() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Retrieve the table's sortable columns |
||
| 133 | * |
||
| 134 | * @access public |
||
| 135 | * @since 1.0 |
||
| 136 | * |
||
| 137 | * @return array Array of all the sortable columns |
||
| 138 | */ |
||
| 139 | public function get_sortable_columns() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Retrieve the current page number |
||
| 149 | * |
||
| 150 | * @access public |
||
| 151 | * @since 1.0 |
||
| 152 | * |
||
| 153 | * @return int Current page number |
||
| 154 | */ |
||
| 155 | public function get_paged() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Retrieve the category being viewed |
||
| 161 | * |
||
| 162 | * @access public |
||
| 163 | * @since 1.0 |
||
| 164 | * |
||
| 165 | * @return int Category ID |
||
| 166 | */ |
||
| 167 | public function get_category() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Outputs the reporting views |
||
| 173 | * |
||
| 174 | * @access public |
||
| 175 | * @since 1.0 |
||
| 176 | * |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | public function bulk_actions( $which = '' ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Generate the table navigation above or below the table |
||
| 185 | * |
||
| 186 | * @since 1.0 |
||
| 187 | * @access protected |
||
| 188 | * |
||
| 189 | * @param string $which |
||
| 190 | */ |
||
| 191 | View Code Duplication | protected function display_tablenav( $which ) { |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Attaches the category filter to the log views |
||
| 223 | * |
||
| 224 | * @access public |
||
| 225 | * @since 1.0 |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | public function category_filter() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Performs the donation forms query |
||
| 239 | * |
||
| 240 | * @access public |
||
| 241 | * @since 1.0 |
||
| 242 | * |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | public function query() { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Build all the reports data |
||
| 297 | * |
||
| 298 | * @access public |
||
| 299 | * @since 1.0 |
||
| 300 | * |
||
| 301 | * @return array $reports_data All the data for donor reports |
||
| 302 | */ |
||
| 303 | public function reports_data() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Setup the final data for the table |
||
| 326 | * |
||
| 327 | * @access public |
||
| 328 | * @since 1.5 |
||
| 329 | * |
||
| 330 | * @uses Give_Form_Reports_Table::get_columns() |
||
| 331 | * @uses Give_Form_Reports_Table::get_sortable_columns() |
||
| 332 | * @uses Give_Form_Reports_Table::reports_data() |
||
| 333 | * @uses Give_Form_Reports_Table::get_pagenum() |
||
| 334 | * |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function prepare_items() { |
|
| 352 | } |
||
| 353 |
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.