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_Logging 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_Logging, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Give_Logging { |
||
| 25 | /** |
||
| 26 | * Logs data operation handler object. |
||
| 27 | * |
||
| 28 | * @since 2.0 |
||
| 29 | * @access private |
||
| 30 | * @var Give_DB_Logs |
||
| 31 | */ |
||
| 32 | public $log_db; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Log meta data operation handler object. |
||
| 36 | * |
||
| 37 | * @since 2.0 |
||
| 38 | * @access private |
||
| 39 | * @var Give_DB_Log_Meta |
||
| 40 | */ |
||
| 41 | public $logmeta_db; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Class Constructor |
||
| 45 | * |
||
| 46 | * Set up the Give Logging Class. |
||
| 47 | * |
||
| 48 | * @since 1.0 |
||
| 49 | * @access public |
||
| 50 | */ |
||
| 51 | public function __construct() { |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * Log Post Type |
||
| 84 | * |
||
| 85 | * Registers the 'give_log' Post Type. |
||
| 86 | * |
||
| 87 | * @since 1.0 |
||
| 88 | * @access public |
||
| 89 | * |
||
| 90 | * @return void |
||
| 91 | */ |
||
| 92 | public function register_post_type() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Log Type Taxonomy |
||
| 114 | * |
||
| 115 | * Registers the "Log Type" taxonomy. Used to determine the type of log entry. |
||
| 116 | * |
||
| 117 | * @since 1.0 |
||
| 118 | * @access public |
||
| 119 | * |
||
| 120 | * @return void |
||
| 121 | */ |
||
| 122 | public function register_taxonomy() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Log Types |
||
| 130 | * |
||
| 131 | * Sets up the default log types and allows for new ones to be created. |
||
| 132 | * |
||
| 133 | * @since 1.0 |
||
| 134 | * @access public |
||
| 135 | * |
||
| 136 | * @return array $terms |
||
| 137 | */ |
||
| 138 | public function log_types() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Check if a log type is valid |
||
| 150 | * |
||
| 151 | * Checks to see if the specified type is in the registered list of types. |
||
| 152 | * |
||
| 153 | * @since 1.0 |
||
| 154 | * @access public |
||
| 155 | * |
||
| 156 | * @param string $type Log type. |
||
| 157 | * |
||
| 158 | * @return bool Whether log type is valid. |
||
| 159 | */ |
||
| 160 | public function valid_type( $type ) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Create new log entry |
||
| 166 | * |
||
| 167 | * This is just a simple and fast way to log something. Use $this->insert_log() |
||
| 168 | * if you need to store custom meta data. |
||
| 169 | * |
||
| 170 | * @since 1.0 |
||
| 171 | * @access public |
||
| 172 | * |
||
| 173 | * @param string $title Log entry title. Default is empty. |
||
| 174 | * @param string $message Log entry message. Default is empty. |
||
| 175 | * @param int $parent Log entry parent. Default is 0. |
||
| 176 | * @param string $type Log type. Default is empty string. |
||
| 177 | * |
||
| 178 | * @return int Log ID. |
||
| 179 | */ |
||
| 180 | public function add( $title = '', $message = '', $parent = 0, $type = '' ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get Logs |
||
| 193 | * |
||
| 194 | * Retrieves log items for a particular object ID. |
||
| 195 | * |
||
| 196 | * @since 1.0 |
||
| 197 | * @access public |
||
| 198 | * |
||
| 199 | * @param int $object_id Log object ID. Default is 0. |
||
| 200 | * @param string $type Log type. Default is empty string. |
||
| 201 | * @param int $paged Page number Default is null. |
||
| 202 | * |
||
| 203 | * @return array An array of the connected logs. |
||
| 204 | */ |
||
| 205 | public function get_logs( $object_id = 0, $type = '', $paged = null ) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Stores a log entry |
||
| 215 | * |
||
| 216 | * @since 1.0 |
||
| 217 | * @access public |
||
| 218 | * |
||
| 219 | * @param array $log_data Log entry data. |
||
| 220 | * @param array $log_meta Log entry meta. |
||
| 221 | * |
||
| 222 | * @return int The ID of the newly created log item. |
||
| 223 | */ |
||
| 224 | public function insert_log( $log_data = array(), $log_meta = array() ) { |
||
| 225 | $log_id = 0; |
||
|
|
|||
| 226 | |||
| 227 | $defaults = array( |
||
| 228 | 'log_parent' => 0, |
||
| 229 | 'log_content' => '', |
||
| 230 | 'log_type' => false, |
||
| 231 | |||
| 232 | // Backward compatibility. |
||
| 233 | 'post_type' => 'give_log', |
||
| 234 | 'post_status' => 'publish', |
||
| 235 | ); |
||
| 236 | |||
| 237 | $args = wp_parse_args( $log_data, $defaults ); |
||
| 238 | $this->bc_200_validate_params( $args, $log_meta ); |
||
| 239 | |||
| 240 | if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
||
| 241 | global $wpdb; |
||
| 242 | |||
| 243 | // Backward Compatibility. |
||
| 244 | if ( ! $wpdb->get_var( "SELECT ID from {$this->log_db->table_name} ORDER BY id DESC LIMIT 1" ) ) { |
||
| 245 | $latest_log_id = $wpdb->get_var( "SELECT ID from $wpdb->posts ORDER BY id DESC LIMIT 1" ); |
||
| 246 | $latest_log_id = empty( $latest_log_id ) ? 1 : ++ $latest_log_id; |
||
| 247 | |||
| 248 | $args['ID'] = $latest_log_id; |
||
| 249 | $this->log_db->insert( $args ); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | $log_id = $this->log_db->add( $args ); |
||
| 254 | |||
| 255 | // Set log meta, if any |
||
| 256 | View Code Duplication | if ( $log_id && ! empty( $log_meta ) ) { |
|
| 257 | foreach ( (array) $log_meta as $key => $meta ) { |
||
| 258 | $this->logmeta_db->update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta ); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | |||
| 263 | // Delete cache. |
||
| 264 | $this->delete_cache(); |
||
| 265 | |||
| 266 | return $log_id; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Update and existing log item |
||
| 271 | * |
||
| 272 | * @since 1.0 |
||
| 273 | * @access public |
||
| 274 | * |
||
| 275 | * @param array $log_data Log entry data. |
||
| 276 | * @param array $log_meta Log entry meta. |
||
| 277 | * |
||
| 278 | * @return bool|null True if successful, false otherwise. |
||
| 279 | */ |
||
| 280 | public function update_log( $log_data = array(), $log_meta = array() ) { |
||
| 281 | $log_id = 0; |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Fires before updating log entry. |
||
| 285 | * |
||
| 286 | * @since 1.0 |
||
| 287 | * |
||
| 288 | * @param array $log_data Log entry data. |
||
| 289 | * @param array $log_meta Log entry meta. |
||
| 290 | */ |
||
| 291 | do_action( 'give_pre_update_log', $log_data, $log_meta ); |
||
| 292 | |||
| 293 | $defaults = array( |
||
| 294 | 'log_parent' => 0, |
||
| 295 | |||
| 296 | // Backward compatibility. |
||
| 297 | 'post_type' => 'give_log', |
||
| 298 | 'post_status' => 'publish', |
||
| 299 | ); |
||
| 300 | |||
| 301 | $args = wp_parse_args( $log_data, $defaults ); |
||
| 302 | $this->bc_200_validate_params( $args, $log_meta ); |
||
| 303 | |||
| 304 | // Store the log entry |
||
| 305 | if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
||
| 306 | // Backward compatibility. |
||
| 307 | $log_id = wp_update_post( $args ); |
||
| 308 | |||
| 309 | if ( $log_id && ! empty( $log_meta ) ) { |
||
| 310 | foreach ( (array) $log_meta as $key => $meta ) { |
||
| 311 | if ( ! empty( $meta ) ) { |
||
| 312 | give_update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta ); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | } |
||
| 316 | } else { |
||
| 317 | $log_id = $this->log_db->add( $args ); |
||
| 318 | |||
| 319 | View Code Duplication | if ( $log_id && ! empty( $log_meta ) ) { |
|
| 320 | foreach ( (array) $log_meta as $key => $meta ) { |
||
| 321 | if ( ! empty( $meta ) ) { |
||
| 322 | $this->logmeta_db->update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta ); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Fires after updating log entry. |
||
| 330 | * |
||
| 331 | * @since 1.0 |
||
| 332 | * |
||
| 333 | * @param int $log_id Log entry id. |
||
| 334 | * @param array $log_data Log entry data. |
||
| 335 | * @param array $log_meta Log entry meta. |
||
| 336 | */ |
||
| 337 | do_action( 'give_post_update_log', $log_id, $log_data, $log_meta ); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Retrieve all connected logs |
||
| 342 | * |
||
| 343 | * Used for retrieving logs related to particular items, such as a specific donation. |
||
| 344 | * For new table params check: Give_DB_Logs::get_column_defaults and Give_DB_Logs::get_sql#L262 |
||
| 345 | * |
||
| 346 | * @since 1.0 |
||
| 347 | * @since 2.0 Added new table logic. |
||
| 348 | * @access public |
||
| 349 | * |
||
| 350 | * @param array $args Query arguments. |
||
| 351 | * |
||
| 352 | * @return array|false Array if logs were found, false otherwise. |
||
| 353 | */ |
||
| 354 | public function get_connected_logs( $args = array() ) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Retrieve Log Count |
||
| 382 | * |
||
| 383 | * Retrieves number of log entries connected to particular object ID. |
||
| 384 | * |
||
| 385 | * @since 1.0 |
||
| 386 | * @access public |
||
| 387 | * |
||
| 388 | * @param int $object_id Log object ID. Default is 0. |
||
| 389 | * @param string $type Log type. Default is empty string. |
||
| 390 | * @param array $meta_query Log meta query. Default is null. |
||
| 391 | * @param array $date_query Log data query. Default is null. |
||
| 392 | * |
||
| 393 | * @return int Log count. |
||
| 394 | */ |
||
| 395 | public function get_log_count( $object_id = 0, $type = '', $meta_query = null, $date_query = null ) { |
||
| 396 | $logs_count = 0; |
||
| 397 | |||
| 398 | $query_args = array( |
||
| 399 | 'number' => - 1, |
||
| 400 | |||
| 401 | // Backward comatibility. |
||
| 402 | 'post_type' => 'give_log', |
||
| 403 | 'post_status' => 'publish', |
||
| 404 | ); |
||
| 405 | |||
| 406 | if ( $object_id ) { |
||
| 407 | $query_args['log_parent'] = $object_id; |
||
| 408 | } |
||
| 409 | |||
| 410 | if ( ! empty( $type ) && $this->valid_type( $type ) ) { |
||
| 411 | $query_args['log_type'] = $type; |
||
| 412 | } |
||
| 413 | |||
| 414 | if ( ! empty( $meta_query ) ) { |
||
| 415 | $query_args['meta_query'] = $meta_query; |
||
| 416 | } |
||
| 417 | |||
| 418 | if ( ! empty( $date_query ) ) { |
||
| 419 | $query_args['date_query'] = $date_query; |
||
| 420 | } |
||
| 421 | |||
| 422 | $this->bc_200_validate_params( $query_args ); |
||
| 423 | |||
| 424 | if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
||
| 425 | // Backward compatibility. |
||
| 426 | $logs = new WP_Query( $query_args ); |
||
| 427 | $logs_count = (int) $logs->post_count; |
||
| 428 | } else { |
||
| 429 | $logs_count = $this->log_db->count( $query_args ); |
||
| 430 | } |
||
| 431 | |||
| 432 | return $logs_count; |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Delete Logs |
||
| 437 | * |
||
| 438 | * Remove log entries connected to particular object ID. |
||
| 439 | * |
||
| 440 | * @since 1.0 |
||
| 441 | * @access public |
||
| 442 | * |
||
| 443 | * @param int $object_id Log object ID. Default is 0. |
||
| 444 | * @param string $type Log type. Default is empty string. |
||
| 445 | * @param array $meta_query Log meta query. Default is null. |
||
| 446 | * |
||
| 447 | * @return void |
||
| 448 | */ |
||
| 449 | public function delete_logs( $object_id = 0, $type = '', $meta_query = null ) { |
||
| 450 | $query_args = array( |
||
| 451 | 'log_parent' => $object_id, |
||
| 452 | 'number' => - 1, |
||
| 453 | 'fields' => 'ID', |
||
| 454 | |||
| 455 | // Backward compatibility. |
||
| 456 | 'post_type' => 'give_log', |
||
| 457 | 'post_status' => 'publish', |
||
| 458 | ); |
||
| 459 | |||
| 460 | if ( ! empty( $type ) && $this->valid_type( $type ) ) { |
||
| 461 | $query_args['log_type'] = $type; |
||
| 462 | } |
||
| 463 | |||
| 464 | if ( ! empty( $meta_query ) ) { |
||
| 465 | $query_args['meta_query'] = $meta_query; |
||
| 466 | } |
||
| 467 | |||
| 468 | $this->bc_200_validate_params( $query_args ); |
||
| 469 | |||
| 470 | if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
||
| 471 | // Backward compatibility. |
||
| 472 | $logs = get_posts( $query_args ); |
||
| 473 | |||
| 474 | if ( $logs ) { |
||
| 475 | foreach ( $logs as $log ) { |
||
| 476 | wp_delete_post( $log, true ); |
||
| 477 | } |
||
| 478 | } |
||
| 479 | } else { |
||
| 480 | $logs = $this->log_db->get_logs( $query_args ); |
||
| 481 | |||
| 482 | if ( $logs ) { |
||
| 483 | foreach ( $logs as $log ) { |
||
| 484 | if ( $this->log_db->delete( $log->ID ) ) { |
||
| 485 | $this->logmeta_db->delete_row( $log->ID ); |
||
| 486 | } |
||
| 487 | } |
||
| 488 | } |
||
| 489 | } |
||
| 490 | |||
| 491 | $this->delete_cache(); |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Setup cron to delete log cache in background. |
||
| 496 | * |
||
| 497 | * @since 1.7 |
||
| 498 | * @access public |
||
| 499 | * |
||
| 500 | * @param int $post_id |
||
| 501 | */ |
||
| 502 | public function background_process_delete_cache( $post_id ) { |
||
| 503 | // Delete log cache immediately |
||
| 504 | wp_schedule_single_event( time() - 5, 'give_delete_log_cache' ); |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Delete all logging cache when form, log or payment updates |
||
| 509 | * |
||
| 510 | * @since 1.7 |
||
| 511 | * @access public |
||
| 512 | * |
||
| 513 | * @return bool |
||
| 514 | */ |
||
| 515 | public function delete_cache() { |
||
| 516 | // Add log related keys to delete. |
||
| 517 | $cache_give_logs = Give_Cache::get_options_like( 'give_logs' ); |
||
| 518 | $cache_give_log_count = Give_Cache::get_options_like( 'log_count' ); |
||
| 519 | |||
| 520 | $cache_option_names = array_merge( $cache_give_logs, $cache_give_log_count ); |
||
| 521 | |||
| 522 | // Bailout. |
||
| 523 | if ( empty( $cache_option_names ) ) { |
||
| 524 | return false; |
||
| 525 | } |
||
| 526 | |||
| 527 | Give_Cache::delete( $cache_option_names ); |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Validate query params. |
||
| 532 | * |
||
| 533 | * @since 2.0 |
||
| 534 | * @access private |
||
| 535 | * |
||
| 536 | * @param array $log_query |
||
| 537 | * @param array $log_meta |
||
| 538 | */ |
||
| 539 | private function bc_200_validate_params( &$log_query, &$log_meta = array() ) { |
||
| 540 | $query_params = array( |
||
| 541 | 'log_title' => 'post_title', |
||
| 542 | 'log_parent' => 'post_parent', |
||
| 543 | 'log_content' => 'post_content', |
||
| 544 | 'log_type' => 'tax_query', |
||
| 545 | 'log_date' => 'post_date', |
||
| 546 | 'log_date_gmt' => 'post_date_gmt', |
||
| 547 | 'number' => 'posts_per_page', |
||
| 548 | 'meta_query' => 'meta_query', |
||
| 549 | ); |
||
| 550 | |||
| 551 | if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
||
| 552 | // Set old params. |
||
| 553 | foreach ( $query_params as $new_query_param => $old_query_param ) { |
||
| 554 | |||
| 555 | View Code Duplication | if ( isset( $log_query[ $old_query_param ] ) && empty( $log_query[ $new_query_param ] ) ) { |
|
| 556 | $log_query[ $new_query_param ] = $log_query[ $old_query_param ]; |
||
| 557 | continue; |
||
| 558 | } elseif ( ! isset( $log_query[ $new_query_param ] ) ) { |
||
| 559 | continue; |
||
| 560 | } |
||
| 561 | |||
| 562 | switch ( $new_query_param ) { |
||
| 563 | case 'log_type': |
||
| 564 | $log_query['tax_query'] = array( |
||
| 565 | array( |
||
| 566 | 'taxonomy' => 'give_log_type', |
||
| 567 | 'field' => 'slug', |
||
| 568 | 'terms' => $log_query[ $new_query_param ], |
||
| 569 | ), |
||
| 570 | ); |
||
| 571 | break; |
||
| 572 | |||
| 573 | case 'meta_query': |
||
| 574 | if( ! empty( $log_query['meta_query'] ) && empty( $log_query['post_parent'] ) ) { |
||
| 575 | foreach ( $log_query['meta_query'] as $index => $meta_query ){ |
||
| 576 | if( ! is_array( $meta_query ) || empty( $meta_query['key'] ) ) { |
||
| 577 | continue; |
||
| 578 | } |
||
| 579 | |||
| 580 | switch ( $meta_query['key'] ) { |
||
| 581 | case '_give_log_form_id': |
||
| 582 | $log_query['post_parent'] = $meta_query['value']; |
||
| 583 | unset( $log_query['meta_query'][$index] ); |
||
| 584 | break; |
||
| 585 | } |
||
| 586 | } |
||
| 587 | } |
||
| 588 | break; |
||
| 589 | |||
| 590 | default: |
||
| 591 | $log_query[ $old_query_param ] = $log_query[ $new_query_param ]; |
||
| 592 | } |
||
| 593 | } |
||
| 594 | } else { |
||
| 595 | // Set only old params. |
||
| 596 | $query_params = array_flip( $query_params ); |
||
| 597 | foreach ( $query_params as $old_query_param => $new_query_param ) { |
||
| 598 | View Code Duplication | if ( isset( $log_query[ $new_query_param ] ) && empty( $log_query[ $old_query_param ] ) ) { |
|
| 599 | $log_query[ $old_query_param ] = $log_query[ $new_query_param ]; |
||
| 600 | continue; |
||
| 601 | } elseif ( ! isset( $log_query[ $old_query_param ] ) ) { |
||
| 602 | continue; |
||
| 603 | } |
||
| 604 | |||
| 605 | switch ( $old_query_param ) { |
||
| 606 | case 'tax_query': |
||
| 607 | if ( isset( $log_query[ $old_query_param ][0]['terms'] ) ) { |
||
| 608 | $log_query[ $new_query_param ] = $log_query[ $old_query_param ][0]['terms']; |
||
| 609 | } |
||
| 610 | break; |
||
| 611 | |||
| 612 | default: |
||
| 613 | $log_query[ $new_query_param ] = $log_query[ $old_query_param ]; |
||
| 614 | } |
||
| 615 | } |
||
| 616 | } |
||
| 617 | } |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Set new log properties. |
||
| 621 | * |
||
| 622 | * @since 2.0 |
||
| 623 | * @access private |
||
| 624 | * |
||
| 625 | * @param array $logs |
||
| 626 | */ |
||
| 627 | private function bc_200_add_new_properties( &$logs ) { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Change log parent to payment if set to form. |
||
| 673 | * |
||
| 674 | * @since 2.0 |
||
| 675 | * @access public |
||
| 676 | * |
||
| 677 | * @param mixed $check |
||
| 678 | * @param int $log_id |
||
| 679 | * @param array $meta_key |
||
| 680 | * @param array $meta_value |
||
| 681 | * |
||
| 682 | * @return mixed |
||
| 683 | */ |
||
| 684 | public function bc_200_set_payment_as_log_parent( $check, $log_id, $meta_key, $meta_value ) { |
||
| 730 | } |
||
| 731 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.