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() ) { |
||
| 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() ) { |
||
| 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 ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Delete Logs |
||
| 444 | * |
||
| 445 | * Remove log entries connected to particular object ID. |
||
| 446 | * |
||
| 447 | * @since 1.0 |
||
| 448 | * @access public |
||
| 449 | * |
||
| 450 | * @param int $object_id Log object ID. Default is 0. |
||
| 451 | * @param string $type Log type. Default is empty string. |
||
| 452 | * @param array $meta_query Log meta query. Default is null. |
||
| 453 | * |
||
| 454 | * @return void |
||
| 455 | */ |
||
| 456 | public function delete_logs( $object_id = 0, $type = '', $meta_query = null ) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Setup cron to delete log cache in background. |
||
| 509 | * |
||
| 510 | * @since 1.7 |
||
| 511 | * @access public |
||
| 512 | * |
||
| 513 | * @param int $post_id |
||
| 514 | */ |
||
| 515 | public function background_process_delete_cache( $post_id ) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Delete all logging cache when form, log or payment updates |
||
| 522 | * |
||
| 523 | * @since 1.7 |
||
| 524 | * @access public |
||
| 525 | * |
||
| 526 | * @return bool |
||
| 527 | */ |
||
| 528 | public function delete_cache() { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Validate query params. |
||
| 545 | * |
||
| 546 | * @since 2.0 |
||
| 547 | * @access private |
||
| 548 | * |
||
| 549 | * @param array $log_query |
||
| 550 | * @param array $log_meta |
||
| 551 | */ |
||
| 552 | private function bc_200_validate_params( &$log_query, &$log_meta = array() ) { |
||
| 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_20_set_payment_as_log_parent( $check, $log_id, $meta_key, $meta_value ) { |
||
| 730 | } |
||
| 731 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.