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 | 6 | */ |
|
| 32 | public $log_db; |
||
| 33 | 6 | ||
| 34 | /** |
||
| 35 | * Log meta data operation handler object. |
||
| 36 | 6 | * |
|
| 37 | * @since 2.0 |
||
| 38 | 6 | * @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 | 6 | * |
|
| 48 | * @since 1.0 |
||
| 49 | * @access public |
||
| 50 | 6 | */ |
|
| 51 | 6 | public function __construct() { |
|
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Log Post Type |
||
| 86 | * |
||
| 87 | 55 | * Registers the 'give_log' Post Type. |
|
| 88 | * |
||
| 89 | 55 | * @since 1.0 |
|
| 90 | 55 | * @access public |
|
| 91 | * |
||
| 92 | 55 | * @return void |
|
| 93 | */ |
||
| 94 | 55 | public function register_post_type() { |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Log Type Taxonomy |
||
| 116 | * |
||
| 117 | * Registers the "Log Type" taxonomy. Used to determine the type of log entry. |
||
| 118 | * |
||
| 119 | * @since 1.0 |
||
| 120 | * @access public |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | public function register_taxonomy() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | 1 | * Log Types |
|
| 132 | * |
||
| 133 | 1 | * Sets up the default log types and allows for new ones to be created. |
|
| 134 | 1 | * |
|
| 135 | 1 | * @since 1.0 |
|
| 136 | * @access public |
||
| 137 | 1 | * |
|
| 138 | * @return array $terms |
||
| 139 | 1 | */ |
|
| 140 | public function log_types() { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Check if a log type is valid |
||
| 153 | * |
||
| 154 | * Checks to see if the specified type is in the registered list of types. |
||
| 155 | 1 | * |
|
| 156 | 1 | * @since 1.0 |
|
| 157 | 1 | * @access public |
|
| 158 | 1 | * |
|
| 159 | * @param string $type Log type. |
||
| 160 | 1 | * |
|
| 161 | * @return bool Whether log type is valid. |
||
| 162 | */ |
||
| 163 | public function valid_type( $type ) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Create new log entry |
||
| 169 | * |
||
| 170 | * This is just a simple and fast way to log something. Use $this->insert_log() |
||
| 171 | * if you need to store custom meta data. |
||
| 172 | * |
||
| 173 | * @since 1.0 |
||
| 174 | * @access public |
||
| 175 | 43 | * |
|
| 176 | * @param string $title Log entry title. Default is empty. |
||
| 177 | 43 | * @param string $message Log entry message. Default is empty. |
|
| 178 | 43 | * @param int $parent Log entry parent. Default is 0. |
|
| 179 | 43 | * @param string $type Log type. Default is empty string. |
|
| 180 | 43 | * |
|
| 181 | * @return int Log ID. |
||
| 182 | 43 | */ |
|
| 183 | public function add( $title = '', $message = '', $parent = 0, $type = '' ) { |
||
| 193 | 43 | ||
| 194 | 43 | /** |
|
| 195 | * Get Logs |
||
| 196 | * |
||
| 197 | 43 | * Retrieves log items for a particular object ID. |
|
| 198 | 42 | * |
|
| 199 | 42 | * @since 1.0 |
|
| 200 | 42 | * @access public |
|
| 201 | 42 | * |
|
| 202 | * @param int $object_id Log object ID. Default is 0. |
||
| 203 | 43 | * @param string $type Log type. Default is empty string. |
|
| 204 | * @param int $paged Page number Default is null. |
||
| 205 | 43 | * |
|
| 206 | * @return array An array of the connected logs. |
||
| 207 | */ |
||
| 208 | public function get_logs( $object_id = 0, $type = '', $paged = null ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Stores a log entry |
||
| 218 | * |
||
| 219 | * @since 1.0 |
||
| 220 | * @access public |
||
| 221 | * |
||
| 222 | * @param array $log_data Log entry data. |
||
| 223 | * @param array $log_meta Log entry meta. |
||
| 224 | * |
||
| 225 | * @return int The ID of the newly created log item. |
||
| 226 | */ |
||
| 227 | public function insert_log( $log_data = array(), $log_meta = array() ) { |
||
| 271 | |||
| 272 | 1 | /** |
|
| 273 | 1 | * Update and existing log item |
|
| 274 | 1 | * |
|
| 275 | 1 | * @since 1.0 |
|
| 276 | 1 | * @access public |
|
| 277 | 1 | * |
|
| 278 | * @param array $log_data Log entry data. |
||
| 279 | 1 | * @param array $log_meta Log entry meta. |
|
| 280 | * |
||
| 281 | 1 | * @return bool|null True if successful, false otherwise. |
|
| 282 | 1 | */ |
|
| 283 | public function update_log( $log_data = array(), $log_meta = array() ) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Retrieve all connected logs |
||
| 345 | * |
||
| 346 | * Used for retrieving logs related to particular items, such as a specific donation. |
||
| 347 | * For new table params check: Give_DB_Logs::get_column_defaults and Give_DB_Logs::get_sql#L262 |
||
| 348 | 32 | * |
|
| 349 | * @since 1.0 |
||
| 350 | 32 | * @since 2.0 Added new table logic. |
|
| 351 | 32 | * @access public |
|
| 352 | 32 | * |
|
| 353 | 32 | * @param array $args Query arguments. |
|
| 354 | * |
||
| 355 | 32 | * @return array|false Array if logs were found, false otherwise. |
|
| 356 | */ |
||
| 357 | 32 | public function get_connected_logs( $args = array() ) { |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Retrieve Log Count |
||
| 385 | * |
||
| 386 | * Retrieves number of log entries connected to particular object ID. |
||
| 387 | * |
||
| 388 | * @since 1.0 |
||
| 389 | * @access public |
||
| 390 | * |
||
| 391 | * @param int $object_id Log object ID. Default is 0. |
||
| 392 | * @param string $type Log type. Default is empty string. |
||
| 393 | * @param array $meta_query Log meta query. Default is null. |
||
| 394 | * @param array $date_query Log data query. Default is null. |
||
| 395 | * |
||
| 396 | * @return int Log count. |
||
| 397 | */ |
||
| 398 | public function get_log_count( $object_id = 0, $type = '', $meta_query = null, $date_query = null ) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Delete Logs |
||
| 440 | * |
||
| 441 | * Remove log entries connected to particular object ID. |
||
| 442 | * |
||
| 443 | * @since 1.0 |
||
| 444 | * @access public |
||
| 445 | * |
||
| 446 | * @param int $object_id Log object ID. Default is 0. |
||
| 447 | * @param string $type Log type. Default is empty string. |
||
| 448 | * @param array $meta_query Log meta query. Default is null. |
||
| 449 | * |
||
| 450 | * @return void |
||
| 451 | */ |
||
| 452 | public function delete_logs( $object_id = 0, $type = '', $meta_query = null ) { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Setup cron to delete log cache in background. |
||
| 499 | * |
||
| 500 | * @since 1.7 |
||
| 501 | * @access public |
||
| 502 | * |
||
| 503 | * @param int $post_id |
||
| 504 | */ |
||
| 505 | public function background_process_delete_cache( $post_id ) { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Delete all logging cache when form, log or payment updates |
||
| 512 | * |
||
| 513 | * @since 1.7 |
||
| 514 | * @access public |
||
| 515 | * |
||
| 516 | * @return bool |
||
| 517 | */ |
||
| 518 | public function delete_cache() { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Validate query params. |
||
| 535 | * |
||
| 536 | * @since 2.0 |
||
| 537 | * @access private |
||
| 538 | * |
||
| 539 | * @param array $log_query |
||
| 540 | * @param array $log_meta |
||
| 541 | */ |
||
| 542 | private function bc_200_validate_params( &$log_query, &$log_meta = array() ) { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Set new log properties. |
||
| 637 | * |
||
| 638 | * @since 2.0 |
||
| 639 | * @access private |
||
| 640 | * |
||
| 641 | * @param array $logs |
||
| 642 | */ |
||
| 643 | private function bc_200_add_new_properties( &$logs ) { |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Change log parent to payment if set to form. |
||
| 689 | * |
||
| 690 | * @since 2.0 |
||
| 691 | * @access public |
||
| 692 | * |
||
| 693 | * @param mixed $check |
||
| 694 | * @param int $log_id |
||
| 695 | * @param array $meta_key |
||
| 696 | * @param array $meta_value |
||
| 697 | * |
||
| 698 | * @return mixed |
||
| 699 | */ |
||
| 700 | public function bc_200_set_payment_as_log_parent( $check, $log_id, $meta_key, $meta_value ) { |
||
| 746 | } |
||
| 747 |
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.