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:
1 | <?php |
||
21 | class EEM_Transaction extends EEM_Base |
||
22 | { |
||
23 | |||
24 | // private instance of the Transaction object |
||
25 | protected static $_instance; |
||
26 | |||
27 | /** |
||
28 | * Status ID(STS_ID on esp_status table) to indicate the transaction is complete, |
||
29 | * but payment is pending. This is the state for transactions where payment is promised |
||
30 | * from an offline gateway. |
||
31 | */ |
||
32 | // const open_status_code = 'TPN'; |
||
33 | |||
34 | /** |
||
35 | * Status ID(STS_ID on esp_status table) to indicate the transaction failed, |
||
36 | * either due to a technical reason (server or computer crash during registration), |
||
37 | * or some other reason that prevent the collection of any useful contact information from any of the registrants |
||
38 | */ |
||
39 | const failed_status_code = 'TFL'; |
||
40 | |||
41 | /** |
||
42 | * Status ID(STS_ID on esp_status table) to indicate the transaction was abandoned, |
||
43 | * either due to a technical reason (server or computer crash during registration), |
||
44 | * or due to an abandoned cart after registrant chose not to complete the registration process |
||
45 | * HOWEVER... |
||
46 | * an abandoned TXN differs from a failed TXN in that it was able to capture contact information for at least one |
||
47 | * registrant |
||
48 | */ |
||
49 | const abandoned_status_code = 'TAB'; |
||
50 | |||
51 | /** |
||
52 | * Status ID(STS_ID on esp_status table) to indicate an incomplete transaction, |
||
53 | * meaning that monies are still owing: TXN_paid < TXN_total |
||
54 | */ |
||
55 | const incomplete_status_code = 'TIN'; |
||
56 | |||
57 | /** |
||
58 | * Status ID (STS_ID on esp_status table) to indicate a complete transaction. |
||
59 | * meaning that NO monies are owing: TXN_paid == TXN_total |
||
60 | */ |
||
61 | const complete_status_code = 'TCM'; |
||
62 | |||
63 | /** |
||
64 | * Status ID(STS_ID on esp_status table) to indicate the transaction is overpaid. |
||
65 | * This is the same as complete, but site admins actually owe clients the moneys! TXN_paid > TXN_total |
||
66 | */ |
||
67 | const overpaid_status_code = 'TOP'; |
||
68 | |||
69 | |||
70 | /** |
||
71 | * private constructor to prevent direct creation |
||
72 | * |
||
73 | * @Constructor |
||
74 | * @access protected |
||
75 | * |
||
76 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any |
||
77 | * incoming timezone data that gets saved). Note this just sends the timezone info to the |
||
78 | * date time model field objects. Default is NULL (and will be assumed using the set |
||
79 | * timezone in the 'timezone_string' wp option) |
||
80 | * |
||
81 | * @return EEM_Transaction |
||
|
|||
82 | * @throws \EE_Error |
||
83 | */ |
||
84 | protected function __construct($timezone) |
||
127 | |||
128 | |||
129 | /** |
||
130 | * txn_status_array |
||
131 | * get list of transaction statuses |
||
132 | * |
||
133 | * @access public |
||
134 | * @return array |
||
135 | */ |
||
136 | public static function txn_status_array() |
||
149 | |||
150 | /** |
||
151 | * get the revenue per day for the Transaction Admin page Reports Tab |
||
152 | * |
||
153 | * @access public |
||
154 | * |
||
155 | * @param string $period |
||
156 | * |
||
157 | * @return \stdClass[] |
||
158 | */ |
||
159 | public function get_revenue_per_day_report($period = '-1 month') |
||
181 | |||
182 | |||
183 | /** |
||
184 | * get the revenue per event for the Transaction Admin page Reports Tab |
||
185 | * |
||
186 | * @access public |
||
187 | * |
||
188 | * @param string $period |
||
189 | * |
||
190 | * @throws \EE_Error |
||
191 | * @return mixed |
||
192 | */ |
||
193 | public function get_revenue_per_event_report($period = '-1 month') |
||
241 | |||
242 | |||
243 | /** |
||
244 | * Gets the current transaction given the reg_url_link, or assumes the reg_url_link is in the |
||
245 | * $_REQUEST global variable. Either way, tries to find the current transaction (through |
||
246 | * the registration pointed to by reg_url_link), if not returns null |
||
247 | * |
||
248 | * @param string $reg_url_link |
||
249 | * |
||
250 | * @return EE_Transaction |
||
251 | */ |
||
252 | public function get_transaction_from_reg_url_link($reg_url_link = '') |
||
261 | |||
262 | |||
263 | /** |
||
264 | * Updates the provided EE_Transaction with all the applicable payments |
||
265 | * (or fetch the EE_Transaction from its ID) |
||
266 | * |
||
267 | * @deprecated |
||
268 | * |
||
269 | * @param EE_Transaction|int $transaction_obj_or_id |
||
270 | * @param boolean $save_txn whether or not to save the transaction during this function call |
||
271 | * |
||
272 | * @return boolean |
||
273 | * @throws \EE_Error |
||
274 | */ |
||
275 | View Code Duplication | public function update_based_on_payments($transaction_obj_or_id, $save_txn = true) |
|
290 | |||
291 | /** |
||
292 | * Deletes "junk" transactions that were probably added by bots. There might be TONS |
||
293 | * of these, so we are very careful to NOT select (which the models do even when deleting), |
||
294 | * and so we only use wpdb directly and only do minimal joins. |
||
295 | * Transactions are considered "junk" if they're failed for longer than a week. |
||
296 | * Also, there is an extra check for payments related to the transaction, because if a transaction has a payment on |
||
297 | * it, it's probably not junk (regardless of what status it has). |
||
298 | * The downside to this approach is that is addons are listening for object deletions |
||
299 | * on EEM_Base::delete() they won't be notified of this. However, there is an action that plugins can hook into |
||
300 | * to catch these types of deletions. |
||
301 | * |
||
302 | * @global WPDB $wpdb |
||
303 | * @return mixed |
||
304 | */ |
||
305 | public function delete_junk_transactions() |
||
367 | |||
368 | |||
369 | /** |
||
370 | * @param array $transaction_IDs |
||
371 | * |
||
372 | * @return bool |
||
373 | */ |
||
374 | public static function unset_locked_transactions(array $transaction_IDs) |
||
390 | |||
391 | |||
392 | |||
393 | /** |
||
394 | * returns an array of EE_Transaction objects whose timestamp is greater than |
||
395 | * the current time minus the session lifespan, which defaults to 60 minutes |
||
396 | * |
||
397 | * @return EE_Base_Class[]|EE_Transaction[] |
||
398 | * @throws EE_Error |
||
399 | * @throws InvalidArgumentException |
||
400 | * @throws InvalidDataTypeException |
||
401 | * @throws InvalidInterfaceException |
||
402 | */ |
||
403 | public function get_transactions_in_progress() |
||
407 | |||
408 | |||
409 | |||
410 | /** |
||
411 | * returns an array of EE_Transaction objects whose timestamp is less than |
||
412 | * the current time minus the session lifespan, which defaults to 60 minutes |
||
413 | * |
||
414 | * @return EE_Base_Class[]|EE_Transaction[] |
||
415 | * @throws EE_Error |
||
416 | * @throws InvalidArgumentException |
||
417 | * @throws InvalidDataTypeException |
||
418 | * @throws InvalidInterfaceException |
||
419 | */ |
||
420 | public function get_transactions_not_in_progress() |
||
424 | |||
425 | |||
426 | |||
427 | /** |
||
428 | * @param string $comparison |
||
429 | * @return EE_Base_Class[]|EE_Transaction[] |
||
430 | * @throws EE_Error |
||
431 | * @throws InvalidArgumentException |
||
432 | * @throws InvalidDataTypeException |
||
433 | * @throws InvalidInterfaceException |
||
434 | */ |
||
435 | private function _get_transactions_in_progress($comparison = '>=') |
||
459 | |||
460 | |||
461 | } |
||
462 | // End of file EEM_Transaction.model.php |
||
464 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.