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_Tools_Recount_All_Stats 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_Tools_Recount_All_Stats, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Give_Tools_Recount_All_Stats extends Give_Batch_Export { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Our export type. Used for export-type specific filters/actions |
||
| 27 | * |
||
| 28 | * @since 1.5 |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | public $export_type = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Allows for a non-form batch processing to be run. |
||
| 35 | * |
||
| 36 | * @since 1.5 |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | public $is_void = true; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Sets the number of items to pull on each step |
||
| 43 | * |
||
| 44 | * @since 1.5 |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | public $per_step = 30; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Display message on completing recount process |
||
| 51 | * |
||
| 52 | * @since 1.8.9 |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | public $message = ''; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Sets donation form id for recalculation |
||
| 59 | * |
||
| 60 | * @since 1.8.9 |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | protected $form_id = 0; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Is Recount process completed |
||
| 67 | * |
||
| 68 | * @since 1.8.9 |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | public $done = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get the recount all stats data |
||
| 75 | * |
||
| 76 | * @access public |
||
| 77 | * @since 1.5 |
||
| 78 | * |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | public function get_data() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Return the calculated completion percentage |
||
| 189 | * |
||
| 190 | * @since 1.5 |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | public function get_percentage_complete() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Set the properties specific to the payments export |
||
| 217 | * |
||
| 218 | * @since 1.5 |
||
| 219 | * |
||
| 220 | * @param array $request The Form Data passed into the batch processing |
||
| 221 | */ |
||
| 222 | public function set_properties( $request ) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Process a step |
||
| 228 | * |
||
| 229 | * @since 1.5 |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | public function process_step() { |
||
| 257 | |||
| 258 | View Code Duplication | public function headers() { |
|
| 259 | ignore_user_abort( true ); |
||
| 260 | |||
| 261 | if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { |
||
| 262 | set_time_limit( 0 ); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Perform the export |
||
| 268 | * |
||
| 269 | * @access public |
||
| 270 | * @since 1.5 |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | public function export() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Pre Fetch Data |
||
| 283 | * |
||
| 284 | * @access public |
||
| 285 | * @since 1.5 |
||
| 286 | */ |
||
| 287 | public function pre_fetch() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Given a key, get the information from the Database Directly |
||
| 370 | * |
||
| 371 | * @since 1.5 |
||
| 372 | * |
||
| 373 | * @param string $key The option_name |
||
| 374 | * |
||
| 375 | * @return mixed Returns the data from the database |
||
| 376 | */ |
||
| 377 | private function get_stored_data( $key ) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Give a key, store the value |
||
| 386 | * |
||
| 387 | * @since 1.5 |
||
| 388 | * |
||
| 389 | * @param string $key The option_name |
||
| 390 | * @param mixed $value The value to store |
||
| 391 | * |
||
| 392 | * @return void |
||
| 393 | */ |
||
| 394 | View Code Duplication | private function store_data( $key, $value ) { |
|
| 395 | global $wpdb; |
||
| 396 | |||
| 397 | $value = maybe_serialize( $value ); |
||
| 398 | |||
| 399 | $data = array( |
||
| 400 | 'option_name' => $key, |
||
| 401 | 'option_value' => $value, |
||
| 402 | 'autoload' => 'no', |
||
| 403 | ); |
||
| 404 | |||
| 405 | $formats = array( |
||
| 406 | '%s', |
||
| 407 | '%s', |
||
| 408 | '%s', |
||
| 409 | ); |
||
| 410 | |||
| 411 | $wpdb->replace( $wpdb->options, $data, $formats ); |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Delete an option |
||
| 416 | * |
||
| 417 | * @since 1.5 |
||
| 418 | * |
||
| 419 | * @param string $key The option_name to delete |
||
| 420 | * |
||
| 421 | * @return void |
||
| 422 | */ |
||
| 423 | private function delete_data( $key ) { |
||
| 427 | |||
| 428 | } |
||
| 429 |