Complex classes like WPSC_Purchase_Log 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 WPSC_Purchase_Log, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class WPSC_Purchase_Log { |
||
| 10 | const INCOMPLETE_SALE = 1; |
||
| 11 | const ORDER_RECEIVED = 2; |
||
| 12 | const ACCEPTED_PAYMENT = 3; |
||
| 13 | const JOB_DISPATCHED = 4; |
||
| 14 | const CLOSED_ORDER = 5; |
||
| 15 | const PAYMENT_DECLINED = 6; |
||
| 16 | const REFUNDED = 7; |
||
| 17 | const REFUND_PENDING = 8; |
||
| 18 | const PARTIALLY_REFUNDED = 9; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Names of column that requires escaping values as strings before being inserted |
||
| 22 | * into the database |
||
| 23 | * |
||
| 24 | * @access private |
||
| 25 | * @static |
||
| 26 | * @since 3.8.9 |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private static $string_cols = array( |
||
| 31 | 'sessionid', |
||
| 32 | 'transactid', |
||
| 33 | 'authcode', |
||
| 34 | 'date', |
||
| 35 | 'gateway', |
||
| 36 | 'billing_country', |
||
| 37 | 'shipping_country', |
||
| 38 | 'email_sent', |
||
| 39 | 'stock_adjusted', |
||
| 40 | 'discount_data', |
||
| 41 | 'track_id', |
||
| 42 | 'billing_region', |
||
| 43 | 'shipping_region', |
||
| 44 | 'find_us', |
||
| 45 | 'engrave_text', |
||
| 46 | 'shipping_method', |
||
| 47 | 'shipping_option', |
||
| 48 | 'affiliate_id', |
||
| 49 | 'plugin_version', |
||
| 50 | 'notes', |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Names of column that requires escaping values as integers before being inserted |
||
| 55 | * into the database |
||
| 56 | * |
||
| 57 | * @static |
||
| 58 | * @since 3.8.9 |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private static $int_cols = array( |
||
| 62 | 'id', |
||
| 63 | 'statusno', |
||
| 64 | 'processed', |
||
| 65 | 'user_ID', |
||
| 66 | ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Names of column that requires escaping values as float before being inserted |
||
| 70 | * into the database |
||
| 71 | * |
||
| 72 | * @static |
||
| 73 | * @since 4.0 |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private static $float_cols = array( |
||
| 77 | 'totalprice', |
||
| 78 | 'base_shipping', |
||
| 79 | 'discount_value', |
||
| 80 | 'wpec_taxes_total', |
||
| 81 | 'wpec_taxes_rate', |
||
| 82 | ); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Array of metadata |
||
| 86 | * |
||
| 87 | * @static |
||
| 88 | * @since 4.0 |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | private static $metadata = array( |
||
|
|
|||
| 92 | 'totalprice', |
||
| 93 | 'base_shipping', |
||
| 94 | 'discount_value', |
||
| 95 | 'wpec_taxes_total', |
||
| 96 | 'wpec_taxes_rate', |
||
| 97 | ); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get the SQL query format for a column |
||
| 101 | * |
||
| 102 | * @since 3.8.9 |
||
| 103 | * @param string $col Name of the column |
||
| 104 | * @return string Placeholder |
||
| 105 | */ |
||
| 106 | private static function get_column_format( $col ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Query the purchase log table to get sales and earning stats |
||
| 120 | * |
||
| 121 | * Accepts an array of arguments: |
||
| 122 | * - 'ids': IDs of products for which you want to get stats |
||
| 123 | * - 'products': array of WPSC_Product objects for which you want to get stats |
||
| 124 | * - 'start' and 'end': the timestamp range (integers) during which you want |
||
| 125 | * to collect the stats. |
||
| 126 | * You can use none, either, or both of them. |
||
| 127 | * Note that the [start, end) interval is a left-closed, |
||
| 128 | * right-open. |
||
| 129 | * E.g.: to get stats from Jan 1st, 2013 to |
||
| 130 | * Dec 31st, 2013 (23:59:59), |
||
| 131 | * set "start" to the timestamp for Jan 1st, 2013, set |
||
| 132 | * "end" to the timestamp for Jan 1st, 2014 |
||
| 133 | * - 'order': what to sort the results by, defaults to 'id'. |
||
| 134 | * Can be 'ids', 'sales', 'earnings' or empty string to disable sorting |
||
| 135 | * - 'orderby': how to sort the results, defaults to 'ASC'. |
||
| 136 | * Can be 'ASC', 'DESC' (lowercase is fine too) |
||
| 137 | * - 'per_page': How many items to fetch, defaults to 0, which fetches all results |
||
| 138 | * - 'page': Which page of the results to fetch, defaults to 1. |
||
| 139 | * Has no effect if per_page is set to 0. |
||
| 140 | * |
||
| 141 | * @since 3.8.14 |
||
| 142 | * @param array|string $args Arguments |
||
| 143 | * @return array Array containing 'sales' and 'earnings' stats |
||
| 144 | */ |
||
| 145 | public static function fetch_stats( $args ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Convert advanced date/time arguments like year, month, day, 'ago' etc. |
||
| 282 | * into basic arguments which are "start" and "end". |
||
| 283 | * |
||
| 284 | * @since 3.8.14 |
||
| 285 | * @param array $args Arguments |
||
| 286 | * @return array Arguments after converting |
||
| 287 | */ |
||
| 288 | private static function convert_date_args( $args ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get overall sales and earning stats for just one product |
||
| 295 | * |
||
| 296 | * @since 3.8.14 |
||
| 297 | * @param int $id ID of the product |
||
| 298 | * @return array Array containing 'sales' and 'earnings' stats |
||
| 299 | */ |
||
| 300 | public static function get_stats_for_product( $id, $args = '' ) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Check whether the status code indicates a completed status |
||
| 318 | * |
||
| 319 | * @since 3.8.13 |
||
| 320 | * @param int $status Status code |
||
| 321 | * @return boolean |
||
| 322 | */ |
||
| 323 | public static function is_order_status_completed( $status ) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Contains the values fetched from the DB |
||
| 335 | * |
||
| 336 | * @access private |
||
| 337 | * @since 3.8.9 |
||
| 338 | * |
||
| 339 | * @var array |
||
| 340 | */ |
||
| 341 | private $data = array(); |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Data that is not directly stored inside the DB but is inferred |
||
| 345 | * |
||
| 346 | * @since 3.9 |
||
| 347 | * @var array |
||
| 348 | */ |
||
| 349 | private $meta_data = array(); |
||
| 350 | |||
| 351 | private $gateway_data = array(); |
||
| 352 | |||
| 353 | /** |
||
| 354 | * True if the DB row is fetched into $this->data. |
||
| 355 | * |
||
| 356 | * @access private |
||
| 357 | * @since 3.8.9 |
||
| 358 | * |
||
| 359 | * @var string |
||
| 360 | */ |
||
| 361 | private $fetched = false; |
||
| 362 | private $is_status_changed = false; |
||
| 363 | private $previous_status = false; |
||
| 364 | |||
| 365 | private $cart_contents = array(); |
||
| 366 | private $cart_ids = array(); |
||
| 367 | private $can_edit = null; |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Contains the constructor arguments. This array is necessary because we will |
||
| 371 | * lazy load the DB row into $this->data whenever necessary. Lazy loading is, |
||
| 372 | * in turn, necessary because sometimes right after saving a new record, we need |
||
| 373 | * to fetch a property with the same object. |
||
| 374 | * |
||
| 375 | * @access private |
||
| 376 | * @since 3.8.9 |
||
| 377 | * |
||
| 378 | * @var array |
||
| 379 | */ |
||
| 380 | private $args = array( |
||
| 381 | 'col' => '', |
||
| 382 | 'value' => '', |
||
| 383 | ); |
||
| 384 | |||
| 385 | /** |
||
| 386 | * True if the row exists in DB |
||
| 387 | * |
||
| 388 | * @access private |
||
| 389 | * @since 3.8.9 |
||
| 390 | * |
||
| 391 | * @var string |
||
| 392 | */ |
||
| 393 | private $exists = false; |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Update cache of the passed log object |
||
| 397 | * |
||
| 398 | * @access public |
||
| 399 | * @static |
||
| 400 | * @since 3.8.9 |
||
| 401 | * |
||
| 402 | * @param WPSC_Purchase_Log $log The log object that you want to store into cache |
||
| 403 | * @return void |
||
| 404 | */ |
||
| 405 | public static function update_cache( &$log ) { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Deletes cache of a log (either by using the log ID or sessionid) |
||
| 419 | * |
||
| 420 | * @access public |
||
| 421 | * @static |
||
| 422 | * @since 3.8.9 |
||
| 423 | * |
||
| 424 | * @param string $value The value to query |
||
| 425 | * @param string $col Optional. Defaults to 'id'. Whether to delete cache by using |
||
| 426 | * a purchase log ID or sessionid |
||
| 427 | * @return void |
||
| 428 | */ |
||
| 429 | public static function delete_cache( $value, $col = 'id' ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Deletes a log from the database. |
||
| 444 | * |
||
| 445 | * @access public |
||
| 446 | * @since 3.8.9 |
||
| 447 | * |
||
| 448 | * @uses $wpdb Global database instance. |
||
| 449 | * @uses wpsc_is_store_admin() Check user has admin capabilities. |
||
| 450 | * @uses WPSC_Purchase_Log::delete_cache() Delete purchaselog cache. |
||
| 451 | * @uses WPSC_Claimed_Stock Claimed Stock class. |
||
| 452 | * |
||
| 453 | * @param string $log_id ID of the log. |
||
| 454 | * @return boolean Deleted successfully. |
||
| 455 | */ |
||
| 456 | public function delete( $log_id = false ) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Constructor of the purchase log object. If no argument is passed, this simply |
||
| 509 | * create a new empty object. Otherwise, this will get the purchase log from the |
||
| 510 | * DB either by using purchase log id or sessionid (specified by the 2nd argument). |
||
| 511 | * |
||
| 512 | * Eg: |
||
| 513 | * |
||
| 514 | * // get purchase log with ID number 23 |
||
| 515 | * $log = new WPSC_Purchase_Log( 23 ); |
||
| 516 | * |
||
| 517 | * // get purchase log with sessionid "asdf" |
||
| 518 | * $log = new WPSC_Purchase_Log( 'asdf', 'sessionid' ) |
||
| 519 | * |
||
| 520 | * @access public |
||
| 521 | * @since 3.8.9 |
||
| 522 | * |
||
| 523 | * @param string $value Optional. Defaults to false. |
||
| 524 | * @param string $col Optional. Defaults to 'id'. |
||
| 525 | */ |
||
| 526 | public function __construct( $value = false, $col = 'id' ) { |
||
| 566 | |||
| 567 | private function set_total_shipping() { |
||
| 576 | |||
| 577 | private function set_gateway_name() { |
||
| 590 | |||
| 591 | private function set_shipping_method_names() { |
||
| 607 | |||
| 608 | private function set_meta_props() { |
||
| 618 | |||
| 619 | public function get_meta() { |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Fetches the actual record from the database |
||
| 630 | * |
||
| 631 | * @access private |
||
| 632 | * @since 3.8.9 |
||
| 633 | * |
||
| 634 | * @return void |
||
| 635 | */ |
||
| 636 | private function fetch() { |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Whether the DB row for this purchase log exists |
||
| 672 | * |
||
| 673 | * @access public |
||
| 674 | * @since 3.8.9 |
||
| 675 | * |
||
| 676 | * @return bool True if it exists. Otherwise false. |
||
| 677 | */ |
||
| 678 | public function exists() { |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Returns the value of the specified property of the purchase log |
||
| 685 | * |
||
| 686 | * @access public |
||
| 687 | * @since 3.8.9 |
||
| 688 | * |
||
| 689 | * @param string $key Name of the property (column) |
||
| 690 | * @return mixed |
||
| 691 | */ |
||
| 692 | public function get( $key ) { |
||
| 708 | |||
| 709 | public function get_cart_contents() { |
||
| 734 | |||
| 735 | public function get_cart_item( $item_id ) { |
||
| 745 | |||
| 746 | public function update_cart_item( $item_id, $data ) { |
||
| 771 | |||
| 772 | public function remove_cart_item( $item_id ) { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Init the purchase log items for this purchase log. |
||
| 799 | * |
||
| 800 | * @since 4.0 |
||
| 801 | * |
||
| 802 | * @return wpsc_purchaselogs_items|false The purhchase log item object or false. |
||
| 803 | */ |
||
| 804 | public function init_items() { |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Returns the whole database row in the form of an associative array |
||
| 818 | * |
||
| 819 | * @access public |
||
| 820 | * @since 3.8.9 |
||
| 821 | * |
||
| 822 | * @return array |
||
| 823 | */ |
||
| 824 | public function get_data() { |
||
| 831 | |||
| 832 | public function get_gateway_data( $from_currency = false, $to_currency = false ) { |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Sets a property to a certain value. This function accepts a key and a value |
||
| 879 | * as arguments, or an associative array containing key value pairs. |
||
| 880 | * |
||
| 881 | * Loops through data, comparing against database, and saves as meta if not found in purchase log table. |
||
| 882 | * |
||
| 883 | * @access public |
||
| 884 | * @since 3.8.9 |
||
| 885 | * |
||
| 886 | * @param mixed $key Name of the property (column), or an array containing key |
||
| 887 | * value pairs |
||
| 888 | * @param string|int $value Optional. Defaults to false. In case $key is a string, |
||
| 889 | * this should be specified. |
||
| 890 | * @return WPSC_Purchase_Log The current object (for method chaining) |
||
| 891 | */ |
||
| 892 | public function set( $key, $value = null ) { |
||
| 928 | |||
| 929 | /** |
||
| 930 | * Returns an array containing the parameter format so that this can be used in |
||
| 931 | * $wpdb methods (update, insert etc.) |
||
| 932 | * |
||
| 933 | * @access private |
||
| 934 | * @since 3.8.9 |
||
| 935 | * |
||
| 936 | * @param array $data |
||
| 937 | * @return array |
||
| 938 | */ |
||
| 939 | private function get_data_format( $data ) { |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Saves the purchase log back to the database |
||
| 951 | * |
||
| 952 | * @access public |
||
| 953 | * @since 3.8.9 |
||
| 954 | * |
||
| 955 | * @return void |
||
| 956 | */ |
||
| 957 | public function save() { |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Save meta data for purchase log, if any was set via set(). |
||
| 1022 | * |
||
| 1023 | * @since 4.0 |
||
| 1024 | * @return void |
||
| 1025 | */ |
||
| 1026 | private function save_meta_data() { |
||
| 1037 | |||
| 1038 | private function update_downloadable_status() { |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Adds ability to retrieve a purchase log by a meta key or value. |
||
| 1057 | * |
||
| 1058 | * @since 4.0 |
||
| 1059 | * |
||
| 1060 | * @param string $key Meta key. Optional. |
||
| 1061 | * @param string $value Meta value. Optional. |
||
| 1062 | * |
||
| 1063 | * @return false|WPSC_Purchase_Log False if no log is found or meta key and value are both not provided. WPSC_Purchase_Log object if found. |
||
| 1064 | */ |
||
| 1065 | public static function get_log_by_meta( $key = '', $value = '' ) { |
||
| 1089 | |||
| 1090 | public function get_next_log_id() { |
||
| 1104 | |||
| 1105 | public function get_previous_log_id() { |
||
| 1119 | |||
| 1120 | public function is_transaction_completed() { |
||
| 1123 | |||
| 1124 | public function can_edit() { |
||
| 1132 | |||
| 1133 | public function is_order_received() { |
||
| 1136 | |||
| 1137 | public function is_incomplete_sale() { |
||
| 1140 | |||
| 1141 | public function is_accepted_payment() { |
||
| 1144 | |||
| 1145 | public function is_job_dispatched() { |
||
| 1148 | |||
| 1149 | public function is_closed_order() { |
||
| 1152 | |||
| 1153 | public function is_payment_declined() { |
||
| 1156 | |||
| 1157 | public function is_refunded() { |
||
| 1160 | |||
| 1161 | public function is_refund_pending() { |
||
| 1164 | } |
||
| 1165 |
This check marks private properties in classes that are never used. Those properties can be removed.