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 extends WPSC_Query_Base { |
||
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 | private $gateway_data = array(); |
||
100 | private $form_data_obj = null; |
||
101 | |||
102 | private $is_status_changed = false; |
||
103 | private $previous_status = false; |
||
104 | |||
105 | private $log_items = array(); |
||
106 | private $log_item_ids = array(); |
||
107 | private $can_edit = null; |
||
108 | |||
109 | /** |
||
110 | * Contains the constructor arguments. This array is necessary because we will |
||
111 | * lazy load the DB row into $this->data whenever necessary. Lazy loading is, |
||
112 | * in turn, necessary because sometimes right after saving a new record, we need |
||
113 | * to fetch a property with the same object. |
||
114 | * |
||
115 | * @access private |
||
116 | * @since 3.8.9 |
||
117 | * |
||
118 | * @var array |
||
119 | */ |
||
120 | private $args = array( |
||
121 | 'col' => '', |
||
122 | 'value' => '', |
||
123 | ); |
||
124 | |||
125 | protected $buyers_name = null; |
||
126 | protected $buyers_city = null; |
||
127 | protected $buyers_email = null; |
||
128 | protected $buyers_address = null; |
||
129 | protected $buyers_state_and_postcode = null; |
||
130 | protected $buyers_country = null; |
||
131 | protected $buyers_phone = null; |
||
132 | protected $shipping_name = null; |
||
133 | protected $shipping_address = null; |
||
134 | protected $shipping_city = null; |
||
135 | protected $shipping_state_and_postcode = null; |
||
136 | protected $shipping_country = null; |
||
137 | protected $payment_method = null; |
||
138 | protected $shipping_method = null; |
||
139 | |||
140 | /** |
||
141 | * Get the SQL query format for a column |
||
142 | * |
||
143 | * @since 3.8.9 |
||
144 | * @param string $col Name of the column |
||
145 | * @return string Placeholder |
||
146 | */ |
||
147 | private static function get_column_format( $col ) { |
||
158 | |||
159 | /** |
||
160 | * Query the purchase log table to get sales and earning stats |
||
161 | * |
||
162 | * Accepts an array of arguments: |
||
163 | * - 'ids': IDs of products for which you want to get stats |
||
164 | * - 'products': array of WPSC_Product objects for which you want to get stats |
||
165 | * - 'start' and 'end': the timestamp range (integers) during which you want |
||
166 | * to collect the stats. |
||
167 | * You can use none, either, or both of them. |
||
168 | * Note that the [start, end) interval is a left-closed, |
||
169 | * right-open. |
||
170 | * E.g.: to get stats from Jan 1st, 2013 to |
||
171 | * Dec 31st, 2013 (23:59:59), |
||
172 | * set "start" to the timestamp for Jan 1st, 2013, set |
||
173 | * "end" to the timestamp for Jan 1st, 2014 |
||
174 | * - 'order': what to sort the results by, defaults to 'id'. |
||
175 | * Can be 'ids', 'sales', 'earnings' or empty string to disable sorting |
||
176 | * - 'orderby': how to sort the results, defaults to 'ASC'. |
||
177 | * Can be 'ASC', 'DESC' (lowercase is fine too) |
||
178 | * - 'per_page': How many items to fetch, defaults to 0, which fetches all results |
||
179 | * - 'page': Which page of the results to fetch, defaults to 1. |
||
180 | * Has no effect if per_page is set to 0. |
||
181 | * |
||
182 | * @since 3.8.14 |
||
183 | * @param array|string $args Arguments |
||
184 | * @return array Array containing 'sales' and 'earnings' stats |
||
185 | */ |
||
186 | public static function fetch_stats( $args ) { |
||
320 | |||
321 | /** |
||
322 | * Convert advanced date/time arguments like year, month, day, 'ago' etc. |
||
323 | * into basic arguments which are "start" and "end". |
||
324 | * |
||
325 | * @since 3.8.14 |
||
326 | * @param array $args Arguments |
||
327 | * @return array Arguments after converting |
||
328 | */ |
||
329 | private static function convert_date_args( $args ) { |
||
333 | |||
334 | /** |
||
335 | * Get overall sales and earning stats for just one product |
||
336 | * |
||
337 | * @since 3.8.14 |
||
338 | * @param int $id ID of the product |
||
339 | * @return array Array containing 'sales' and 'earnings' stats |
||
340 | */ |
||
341 | public static function get_stats_for_product( $id, $args = '' ) { |
||
356 | |||
357 | /** |
||
358 | * Check whether the status code indicates a completed status |
||
359 | * |
||
360 | * @since 3.8.13 |
||
361 | * @param int $status Status code |
||
362 | * @return boolean |
||
363 | */ |
||
364 | public static function is_order_status_completed( $status ) { |
||
373 | |||
374 | /** |
||
375 | * Update cache of the passed log object |
||
376 | * |
||
377 | * @access public |
||
378 | * @static |
||
379 | * @since 3.8.9 |
||
380 | * |
||
381 | * @param WPSC_Purchase_Log $log The log object that you want to store into cache |
||
382 | * @return void |
||
383 | */ |
||
384 | public static function update_cache( &$log ) { |
||
387 | |||
388 | /** |
||
389 | * Update caches. |
||
390 | * |
||
391 | * @access public |
||
392 | * @static |
||
393 | * @since 4.0 |
||
394 | * |
||
395 | * @return void |
||
396 | */ |
||
397 | public function update_caches() { |
||
411 | |||
412 | /** |
||
413 | * Deletes cache of a log (either by using the log ID or sessionid) |
||
414 | * |
||
415 | * @access public |
||
416 | * @static |
||
417 | * @since 3.8.9 |
||
418 | * |
||
419 | * @param string $value The value to query |
||
420 | * @param string $col Optional. Defaults to 'id'. Whether to delete cache by using |
||
421 | * a purchase log ID or sessionid |
||
422 | * @return void |
||
423 | */ |
||
424 | public static function delete_cache( $value, $col = 'id' ) { |
||
429 | |||
430 | /** |
||
431 | * Deletes caches. |
||
432 | * |
||
433 | * @access public |
||
434 | * @static |
||
435 | * @since 4.0 |
||
436 | * |
||
437 | * @param string|null $value Optional (left for back-compatibility). The value which was queried. |
||
438 | * @param string|null $col Optional (left for back-compatibility). The column used as the identifier. |
||
439 | * |
||
440 | * @return void |
||
441 | */ |
||
442 | public function delete_caches( $value = null, $col = null ) { |
||
458 | |||
459 | /** |
||
460 | * Deletes a log from the database. |
||
461 | * |
||
462 | * @access public |
||
463 | * @since 3.8.9 |
||
464 | * |
||
465 | * @uses $wpdb Global database instance. |
||
466 | * @uses wpsc_is_store_admin() Check user has admin capabilities. |
||
467 | * @uses WPSC_Purchase_Log::delete_cache() Delete purchaselog cache. |
||
468 | * @uses WPSC_Claimed_Stock Claimed Stock class. |
||
469 | * |
||
470 | * @param string $log_id ID of the log. |
||
471 | * @return boolean Deleted successfully. |
||
472 | */ |
||
473 | public function delete( $log_id = false ) { |
||
523 | |||
524 | /** |
||
525 | * Constructor of the purchase log object. If no argument is passed, this simply |
||
526 | * create a new empty object. Otherwise, this will get the purchase log from the |
||
527 | * DB either by using purchase log id or sessionid (specified by the 2nd argument). |
||
528 | * |
||
529 | * Eg: |
||
530 | * |
||
531 | * // get purchase log with ID number 23 |
||
532 | * $log = new WPSC_Purchase_Log( 23 ); |
||
533 | * |
||
534 | * // get purchase log with sessionid "asdf" |
||
535 | * $log = new WPSC_Purchase_Log( 'asdf', 'sessionid' ) |
||
536 | * |
||
537 | * @access public |
||
538 | * @since 3.8.9 |
||
539 | * |
||
540 | * @param string $value Optional. Defaults to false. |
||
541 | * @param string $col Optional. Defaults to 'id'. |
||
542 | */ |
||
543 | public function __construct( $value = false, $col = 'id' ) { |
||
585 | |||
586 | private function set_total_shipping() { |
||
595 | |||
596 | private function set_gateway_name() { |
||
609 | |||
610 | private function set_shipping_method_names() { |
||
626 | |||
627 | private function set_meta_props() { |
||
637 | |||
638 | public function get_meta() { |
||
646 | |||
647 | /** |
||
648 | * Fetches the actual record from the database |
||
649 | * |
||
650 | * @access protected |
||
651 | * @since 3.8.9 |
||
652 | * |
||
653 | * @return WPSC_Query_Base |
||
654 | */ |
||
655 | protected function fetch() { |
||
690 | |||
691 | /** |
||
692 | * Prepares the return value for get() (apply_filters, etc). |
||
693 | * |
||
694 | * @access protected |
||
695 | * @since 4.0 |
||
696 | * |
||
697 | * @param mixed $value Value fetched |
||
698 | * @param string $key Key for $data. |
||
699 | * |
||
700 | * @return mixed |
||
701 | */ |
||
702 | protected function prepare_get( $value, $key ) { |
||
705 | |||
706 | /** |
||
707 | * Prepares the return value for get_data() (apply_filters, etc). |
||
708 | * |
||
709 | * @access protected |
||
710 | * @since 4.0 |
||
711 | * |
||
712 | * @return mixed |
||
713 | */ |
||
714 | protected function prepare_get_data() { |
||
717 | |||
718 | /** |
||
719 | * Prepares the return value for get_meta() (apply_filters, etc). |
||
720 | * |
||
721 | * @access protected |
||
722 | * @since 4.0 |
||
723 | * |
||
724 | * @return mixed |
||
725 | */ |
||
726 | protected function prepare_get_meta() { |
||
729 | |||
730 | public function get_cart_contents() { |
||
734 | |||
735 | public function get_items() { |
||
760 | |||
761 | public function get_item( $item_id ) { |
||
762 | $item_id = absint( $item_id ); |
||
763 | $items = $this->get_items(); |
||
764 | |||
765 | if ( isset( $this->log_item_ids[ $item_id ] ) ) { |
||
766 | return $items[ $this->log_item_ids[ $item_id ] ]; |
||
767 | } |
||
768 | |||
769 | return false; |
||
770 | } |
||
771 | |||
772 | public function get_item_from_product_id( $product_id ) { |
||
773 | $product_id = absint( $product_id ); |
||
774 | $items = $this->get_items(); |
||
775 | |||
776 | foreach ( $items as $item ) { |
||
777 | if ( $product_id === absint( $item->prodid ) ) { |
||
778 | return $item; |
||
779 | } |
||
780 | } |
||
781 | |||
782 | return false; |
||
783 | } |
||
784 | |||
785 | public function update_item( $item_id, $data ) { |
||
786 | global $wpdb; |
||
787 | |||
788 | $item_id = absint( $item_id ); |
||
789 | $item = $this->get_item( $item_id ); |
||
790 | |||
791 | if ( $item ) { |
||
792 | do_action( 'wpsc_purchase_log_before_update_item', $item_id ); |
||
793 | |||
794 | $data = wp_unslash( $data ); |
||
795 | $result = $wpdb->update( WPSC_TABLE_CART_CONTENTS, $data, array( 'id' => $item_id ) ); |
||
796 | |||
797 | if ( $result ) { |
||
798 | |||
799 | $this->log_items = array(); |
||
800 | $this->get_item( $item_id ); |
||
801 | |||
802 | do_action( 'wpsc_purchase_log_update_item', $item_id ); |
||
803 | } |
||
804 | |||
805 | return $result; |
||
806 | } |
||
807 | |||
808 | return false; |
||
809 | } |
||
810 | |||
811 | public function remove_item( $item_id ) { |
||
812 | global $wpdb; |
||
813 | |||
814 | $item_id = absint( $item_id ); |
||
815 | $item = $this->get_item( $item_id ); |
||
816 | |||
817 | if ( $item ) { |
||
818 | do_action( 'wpsc_purchase_log_before_remove_item', $item_id ); |
||
819 | |||
820 | $result = $wpdb->delete( WPSC_TABLE_CART_CONTENTS, array( 'id' => $item_id ) ); |
||
821 | |||
822 | if ( $result ) { |
||
823 | |||
824 | unset( $this->log_items[ $this->log_item_ids[ $item_id ] ] ); |
||
825 | unset( $this->log_item_ids[ $item_id ] ); |
||
826 | |||
827 | do_action( 'wpsc_purchase_log_remove_item', $item_id ); |
||
828 | } |
||
829 | |||
830 | return $result; |
||
831 | } |
||
832 | |||
833 | return false; |
||
834 | } |
||
835 | |||
836 | public function form_data() { |
||
843 | |||
844 | public function get_gateway_data( $from_currency = false, $to_currency = false ) { |
||
888 | |||
889 | /** |
||
890 | * Sets a property to a certain value. This function accepts a key and a value |
||
891 | * as arguments, or an associative array containing key value pairs. |
||
892 | * |
||
893 | * Loops through data, comparing against database, and saves as meta if not found in purchase log table. |
||
894 | * |
||
895 | * @access public |
||
896 | * @since 3.8.9 |
||
897 | * |
||
898 | * @param mixed $key Name of the property (column), or an array containing key |
||
899 | * value pairs |
||
900 | * @param string|int $value Optional. Defaults to false. In case $key is a string, |
||
901 | * this should be specified. |
||
902 | * @return WPSC_Purchase_Log The current object (for method chaining) |
||
903 | */ |
||
904 | public function set( $key, $value = null ) { |
||
939 | |||
940 | /** |
||
941 | * Returns an array containing the parameter format so that this can be used in |
||
942 | * $wpdb methods (update, insert etc.) |
||
943 | * |
||
944 | * @access private |
||
945 | * @since 3.8.9 |
||
946 | * |
||
947 | * @param array $data |
||
948 | * @return array |
||
949 | */ |
||
950 | private function get_data_format( $data ) { |
||
959 | |||
960 | /** |
||
961 | * Saves the purchase log back to the database |
||
962 | * |
||
963 | * @access public |
||
964 | * @since 3.8.9 |
||
965 | * |
||
966 | * @return void |
||
967 | */ |
||
968 | public function save() { |
||
1030 | |||
1031 | /** |
||
1032 | * Save meta data for purchase log, if any was set via set(). |
||
1033 | * |
||
1034 | * @access public |
||
1035 | * @since 4.0 |
||
1036 | * |
||
1037 | * @return WPSC_Purchase_Log The current object (for method chaining) |
||
1038 | */ |
||
1039 | public function save_meta() { |
||
1052 | |||
1053 | private function update_downloadable_status() { |
||
1069 | |||
1070 | public function have_downloads_locked() { |
||
1078 | |||
1079 | /** |
||
1080 | * Adds ability to retrieve a purchase log by a meta key or value. |
||
1081 | * |
||
1082 | * @since 4.0 |
||
1083 | * |
||
1084 | * @param string $key Meta key. Optional. |
||
1085 | * @param string $value Meta value. Optional. |
||
1086 | * |
||
1087 | * @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. |
||
1088 | */ |
||
1089 | public static function get_log_by_meta( $key = '', $value = '' ) { |
||
1113 | |||
1114 | public function get_next_log_id() { |
||
1128 | |||
1129 | public function get_previous_log_id() { |
||
1143 | |||
1144 | public function is_transaction_completed() { |
||
1147 | |||
1148 | public function can_edit() { |
||
1156 | |||
1157 | public function is_order_received() { |
||
1160 | |||
1161 | public function is_incomplete_sale() { |
||
1164 | |||
1165 | public function is_accepted_payment() { |
||
1168 | |||
1169 | public function is_job_dispatched() { |
||
1172 | |||
1173 | public function is_closed_order() { |
||
1176 | |||
1177 | public function is_payment_declined() { |
||
1180 | |||
1181 | public function is_refunded() { |
||
1184 | |||
1185 | public function is_refund_pending() { |
||
1188 | |||
1189 | /* |
||
1190 | * Utility methods using the $purchlogitem global.. Global usage to be replaced in the future. |
||
1191 | * |
||
1192 | * TODO: seriously get rid of all these badly coded purchaselogs.functions.php functions |
||
1193 | * and wpsc_purchaselogs/wpsc_purchaselogs_items classes. |
||
1194 | */ |
||
1195 | |||
1196 | /** |
||
1197 | * Init the purchase log items for this purchase log. |
||
1198 | * |
||
1199 | * @since 4.0 |
||
1200 | * |
||
1201 | * @return wpsc_purchaselogs_items|false The purhchase log item object or false. |
||
1202 | */ |
||
1203 | public function init_items() { |
||
1211 | |||
1212 | public function buyers_name() { |
||
1231 | |||
1232 | public function buyers_city() { |
||
1241 | |||
1242 | public function buyers_email() { |
||
1251 | |||
1252 | public function buyers_address() { |
||
1261 | |||
1262 | public function buyers_state_and_postcode() { |
||
1288 | |||
1289 | public function buyers_country() { |
||
1298 | |||
1299 | public function buyers_phone() { |
||
1308 | |||
1309 | public function shipping_name() { |
||
1322 | |||
1323 | public function shipping_city() { |
||
1332 | |||
1333 | public function shipping_address() { |
||
1342 | |||
1343 | public function shipping_state_and_postcode() { |
||
1367 | |||
1368 | public function shipping_country() { |
||
1379 | |||
1380 | public function payment_method() { |
||
1401 | |||
1402 | public function shipping_method() { |
||
1416 | |||
1417 | /** |
||
1418 | * Returns base shipping should make a function to calculate items shipping as well |
||
1419 | * |
||
1420 | * @since 4.0 |
||
1421 | * |
||
1422 | * @param boolean $numeric Return numeric value. |
||
1423 | * |
||
1424 | * @return mixed |
||
1425 | */ |
||
1426 | public function discount( $numeric = false ) { |
||
1434 | |||
1435 | /** |
||
1436 | * Returns base shipping should make a function to calculate items shipping as well |
||
1437 | * |
||
1438 | * @since 4.0 |
||
1439 | * |
||
1440 | * @param boolean $numeric Return numeric value. |
||
1441 | * @param boolean $include_items Whether to calculate per-item-shipping. |
||
1442 | * |
||
1443 | * @return mixed |
||
1444 | */ |
||
1445 | public function shipping( $numeric = false, $include_items = false ) { |
||
1458 | |||
1459 | /** |
||
1460 | * Returns taxes total. |
||
1461 | * |
||
1462 | * @since 4.0 |
||
1463 | * |
||
1464 | * @param boolean $numeric Return numeric value. |
||
1465 | * |
||
1466 | * @return mixed |
||
1467 | */ |
||
1468 | public function taxes( $numeric = false ) { |
||
1477 | |||
1478 | public function total_price() { |
||
1484 | |||
1485 | } |
||
1486 |
This check marks private properties in classes that are never used. Those properties can be removed.