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 EE_Line_Item 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 EE_Line_Item, and based on these observations, apply Extract Interface, too.
1 | <?php if ( !defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
||
28 | class EE_Line_Item extends EE_Base_Class implements EEI_Line_Item { |
||
29 | |||
30 | /** |
||
31 | * for children line items (currently not a normal relation) |
||
32 | * @type EE_Line_Item[] |
||
33 | */ |
||
34 | protected $_children; |
||
35 | |||
36 | /** |
||
37 | * for the parent line item |
||
38 | * @var EE_Line_Item |
||
39 | */ |
||
40 | protected $_parent; |
||
41 | |||
42 | |||
43 | |||
44 | /** |
||
45 | * |
||
46 | * @param array $props_n_values incoming values |
||
47 | * @param string $timezone incoming timezone (if not set the timezone set for the website will be |
||
48 | * used.) |
||
49 | * @param array $date_formats incoming date_formats in an array where the first value is the |
||
50 | * date_format and the second value is the time format |
||
51 | * @return EE_Line_Item |
||
52 | */ |
||
53 | public static function new_instance( $props_n_values = array(), $timezone = null, $date_formats = array() ) { |
||
57 | |||
58 | |||
59 | |||
60 | /** |
||
61 | * @param array $props_n_values incoming values from the database |
||
62 | * @param string $timezone incoming timezone as set by the model. If not set the timezone for |
||
63 | * the website will be used. |
||
64 | * @return EE_Line_Item |
||
65 | */ |
||
66 | public static function new_instance_from_db( $props_n_values = array(), $timezone = null ) { |
||
69 | |||
70 | |||
71 | |||
72 | /** |
||
73 | * Adds some defaults if they're not specified |
||
74 | * @param array $fieldValues |
||
75 | * @param bool $bydb |
||
76 | * @param string $timezone |
||
77 | */ |
||
78 | protected function __construct( $fieldValues = array(), $bydb = FALSE, $timezone = '' ) { |
||
84 | |||
85 | |||
86 | |||
87 | /** |
||
88 | * Gets ID |
||
89 | * @return int |
||
90 | */ |
||
91 | public function ID() { |
||
94 | |||
95 | |||
96 | |||
97 | /** |
||
98 | * Gets TXN_ID |
||
99 | * @return int |
||
100 | */ |
||
101 | public function TXN_ID() { |
||
104 | |||
105 | |||
106 | |||
107 | /** |
||
108 | * Sets TXN_ID |
||
109 | * @param int $TXN_ID |
||
110 | */ |
||
111 | public function set_TXN_ID( $TXN_ID ) { |
||
114 | |||
115 | |||
116 | |||
117 | /** |
||
118 | * Gets name |
||
119 | * @return string |
||
120 | */ |
||
121 | public function name() { |
||
128 | |||
129 | |||
130 | |||
131 | /** |
||
132 | * Sets name |
||
133 | * @param string $name |
||
134 | */ |
||
135 | public function set_name( $name ) { |
||
138 | |||
139 | |||
140 | |||
141 | /** |
||
142 | * Gets desc |
||
143 | * @return string |
||
144 | */ |
||
145 | public function desc() { |
||
148 | |||
149 | |||
150 | |||
151 | /** |
||
152 | * Sets desc |
||
153 | * @param string $desc |
||
154 | */ |
||
155 | public function set_desc( $desc ) { |
||
158 | |||
159 | |||
160 | |||
161 | /** |
||
162 | * Gets quantity |
||
163 | * @return int |
||
164 | */ |
||
165 | public function quantity() { |
||
168 | |||
169 | |||
170 | |||
171 | /** |
||
172 | * Sets quantity |
||
173 | * @param int $quantity |
||
174 | */ |
||
175 | public function set_quantity( $quantity ) { |
||
178 | |||
179 | |||
180 | |||
181 | /** |
||
182 | * Gets item_id |
||
183 | * @return string |
||
184 | */ |
||
185 | public function OBJ_ID() { |
||
188 | |||
189 | |||
190 | |||
191 | /** |
||
192 | * Sets item_id |
||
193 | * @param string $item_id |
||
194 | */ |
||
195 | public function set_OBJ_ID( $item_id ) { |
||
198 | |||
199 | |||
200 | |||
201 | /** |
||
202 | * Gets item_type |
||
203 | * @return string |
||
204 | */ |
||
205 | public function OBJ_type() { |
||
208 | |||
209 | |||
210 | |||
211 | /** |
||
212 | * Gets item_type |
||
213 | * @return string |
||
214 | */ |
||
215 | public function OBJ_type_i18n() { |
||
236 | |||
237 | |||
238 | |||
239 | /** |
||
240 | * Sets item_type |
||
241 | * @param string $OBJ_type |
||
242 | */ |
||
243 | public function set_OBJ_type( $OBJ_type ) { |
||
246 | |||
247 | |||
248 | |||
249 | /** |
||
250 | * Gets unit_price |
||
251 | * @return float |
||
252 | */ |
||
253 | public function unit_price() { |
||
256 | |||
257 | |||
258 | |||
259 | /** |
||
260 | * Sets unit_price |
||
261 | * |
||
262 | * @param float $unit_price |
||
263 | */ |
||
264 | public function set_unit_price( $unit_price ) { |
||
267 | |||
268 | |||
269 | |||
270 | /** |
||
271 | * Checks if this item is a percentage modifier or not |
||
272 | * @throws EE_Error |
||
273 | * @return boolean |
||
274 | */ |
||
275 | public function is_percent() { |
||
294 | |||
295 | |||
296 | |||
297 | /** |
||
298 | * Gets percent (between 100-.001) |
||
299 | * @return float |
||
300 | */ |
||
301 | public function percent() { |
||
304 | |||
305 | |||
306 | |||
307 | /** |
||
308 | * Sets percent (between 100-0.01) |
||
309 | * @param float $percent |
||
310 | */ |
||
311 | public function set_percent( $percent ) { |
||
314 | |||
315 | |||
316 | |||
317 | /** |
||
318 | * Gets total |
||
319 | * @return float |
||
320 | */ |
||
321 | public function total() { |
||
324 | |||
325 | |||
326 | |||
327 | /** |
||
328 | * Sets total |
||
329 | * @param float $total |
||
330 | */ |
||
331 | public function set_total( $total ) { |
||
334 | |||
335 | |||
336 | |||
337 | /** |
||
338 | * Gets order |
||
339 | * @return int |
||
340 | */ |
||
341 | public function order() { |
||
344 | |||
345 | |||
346 | |||
347 | /** |
||
348 | * Sets order |
||
349 | * @param int $order |
||
350 | */ |
||
351 | public function set_order( $order ) { |
||
354 | |||
355 | |||
356 | |||
357 | /** |
||
358 | * Gets parent |
||
359 | * @return int |
||
360 | */ |
||
361 | public function parent_ID() { |
||
364 | |||
365 | |||
366 | |||
367 | /** |
||
368 | * Sets parent |
||
369 | * @param int $parent |
||
370 | */ |
||
371 | public function set_parent_ID( $parent ) { |
||
374 | |||
375 | |||
376 | |||
377 | /** |
||
378 | * Gets type |
||
379 | * @return string |
||
380 | */ |
||
381 | public function type() { |
||
384 | |||
385 | |||
386 | |||
387 | /** |
||
388 | * Sets type |
||
389 | * @param string $type |
||
390 | */ |
||
391 | public function set_type( $type ) { |
||
394 | |||
395 | |||
396 | |||
397 | /** |
||
398 | * Gets the line item of which this item is a composite. Eg, if this is a subtotal, the parent might be a total\ |
||
399 | * If this line item is saved to the DB, fetches the parent from the DB. However, if this line item isn't in the DB |
||
400 | * it uses its cached reference to its parent line item (which would have been set by `EE_Line_Item::set_parent()` or |
||
401 | * indirectly by `EE_Line_item::add_child_line_item()`) |
||
402 | * @return EE_Line_Item |
||
403 | */ |
||
404 | public function parent() { |
||
411 | |||
412 | |||
413 | |||
414 | /** |
||
415 | * Gets ALL the children of this line item (ie, all the parts that contribute towards this total). |
||
416 | * @return EE_Line_Item[] |
||
417 | */ |
||
418 | public function children() { |
||
431 | |||
432 | |||
433 | |||
434 | /** |
||
435 | * Gets code |
||
436 | * @return string |
||
437 | */ |
||
438 | public function code() { |
||
441 | |||
442 | |||
443 | |||
444 | /** |
||
445 | * Sets code |
||
446 | * @param string $code |
||
447 | */ |
||
448 | public function set_code( $code ) { |
||
451 | |||
452 | |||
453 | |||
454 | /** |
||
455 | * Gets is_taxable |
||
456 | * @return boolean |
||
457 | */ |
||
458 | public function is_taxable() { |
||
461 | |||
462 | |||
463 | |||
464 | /** |
||
465 | * Sets is_taxable |
||
466 | * @param boolean $is_taxable |
||
467 | */ |
||
468 | public function set_is_taxable( $is_taxable ) { |
||
471 | |||
472 | |||
473 | |||
474 | /** |
||
475 | * Gets the object that this model-joins-to. |
||
476 | * returns one of the model objects that the field OBJ_ID can point to... see the 'OBJ_ID' field on EEM_Promotion_Object |
||
477 | * |
||
478 | * Eg, if this line item join model object is for a ticket, this will return the EE_Ticket object |
||
479 | * |
||
480 | * @return EE_Base_Class | NULL |
||
481 | */ |
||
482 | public function get_object() { |
||
486 | |||
487 | |||
488 | |||
489 | /** |
||
490 | * Like EE_Line_Item::get_object(), but can only ever actually return an EE_Ticket. |
||
491 | * (IE, if this line item is for a price or something else, will return NULL) |
||
492 | * @param array $query_params |
||
493 | * @return EE_Ticket |
||
494 | */ |
||
495 | public function ticket( $query_params = array() ) { |
||
501 | |||
502 | |||
503 | |||
504 | /** |
||
505 | * Gets the EE_Datetime that's related to the ticket, IF this is for a ticket |
||
506 | * @return EE_Datetime | NULL |
||
507 | */ |
||
508 | public function get_ticket_datetime() { |
||
520 | |||
521 | |||
522 | |||
523 | /** |
||
524 | * Gets the event's name that's related to the ticket, if this is for |
||
525 | * a ticket |
||
526 | * @return string |
||
527 | */ |
||
528 | public function ticket_event_name() { |
||
536 | |||
537 | |||
538 | /** |
||
539 | * Gets the event that's related to the ticket, if this line item represents a ticket. |
||
540 | * @return EE_Event|null |
||
541 | */ |
||
542 | public function ticket_event() { |
||
553 | |||
554 | |||
555 | |||
556 | /** |
||
557 | * Gets the first datetime for this lien item, assuming it's for a ticket |
||
558 | * @param string $date_format |
||
559 | * @param string $time_format |
||
560 | * @return string |
||
561 | */ |
||
562 | public function ticket_datetime_start( $date_format = '', $time_format = '' ) { |
||
570 | |||
571 | |||
572 | |||
573 | /** |
||
574 | * Adds the line item as a child to this line item. If there is another child line |
||
575 | * item with the same LIN_code, it is overwritten by this new one |
||
576 | * @param EEI_Line_Item $line_item |
||
577 | * @param bool $set_order |
||
578 | * @return bool success |
||
579 | * @throws \EE_Error |
||
580 | */ |
||
581 | public function add_child_line_item( EEI_Line_Item $line_item, $set_order = true ) { |
||
605 | |||
606 | /** |
||
607 | * Similar to EE_Base_Class::_add_relation_to, except this isn't a normal relation. |
||
608 | * If this line item is saved to the DB, this is just a wrapper for set_parent_ID() and save() |
||
609 | * However, if this line item is NOT saved to the DB, this just caches the parent on |
||
610 | * the EE_Line_Item::_parent property. |
||
611 | * @param EE_Line_Item $line_item |
||
612 | * |
||
613 | */ |
||
614 | public function set_parent( $line_item ) { |
||
626 | |||
627 | |||
628 | |||
629 | /** |
||
630 | * Gets the child line item as specified by its code. Because this returns an object (by reference) |
||
631 | * you can modify this child line item and the parent (this object) can know about them |
||
632 | * because it also has a reference to that line item |
||
633 | * @param string $code |
||
634 | * @return EE_Line_Item |
||
635 | */ |
||
636 | public function get_child_line_item( $code ) { |
||
643 | |||
644 | |||
645 | |||
646 | /** |
||
647 | * Returns how many items are deleted (or, if this item has not been saved ot the DB yet, just how many it HAD cached on it) |
||
648 | * @return int |
||
649 | */ |
||
650 | public function delete_children_line_items() { |
||
659 | |||
660 | |||
661 | |||
662 | /** |
||
663 | * If this line item has been saved to the DB, deletes its child with LIN_code == $code. If this line |
||
664 | * HAS NOT been saved to the DB, removes the child line item with index $code. |
||
665 | * Also searches through the child's children for a matching line item. However, once a line item has been found |
||
666 | * and deleted, stops searching (so if there are line items with duplicate codes, only the first one found will be deleted) |
||
667 | * @param string $code |
||
668 | * @param bool $stop_search_once_found |
||
669 | * @return int count of items deleted (or simply removed from the line item's cache, if not has not been saved to the DB yet) |
||
670 | */ |
||
671 | public function delete_child_line_item( $code, $stop_search_once_found = true ) { |
||
694 | |||
695 | /** |
||
696 | * If this line item is in the database, is of the type subtotal, and |
||
697 | * has no children, why do we have it? It should be deleted so this function |
||
698 | * does that |
||
699 | * @return boolean |
||
700 | */ |
||
701 | public function delete_if_childless_subtotal() { |
||
710 | |||
711 | |||
712 | |||
713 | /** |
||
714 | * Creates a code and returns a string. doesn't assign the code to this model object |
||
715 | * @return string |
||
716 | */ |
||
717 | public function generate_code() { |
||
721 | |||
722 | |||
723 | |||
724 | /** |
||
725 | * @return bool |
||
726 | */ |
||
727 | public function is_tax() { |
||
730 | |||
731 | |||
732 | |||
733 | /** |
||
734 | * @return bool |
||
735 | */ |
||
736 | public function is_tax_sub_total() { |
||
739 | |||
740 | |||
741 | |||
742 | /** |
||
743 | * @return bool |
||
744 | */ |
||
745 | public function is_line_item() { |
||
748 | |||
749 | |||
750 | |||
751 | /** |
||
752 | * @return bool |
||
753 | */ |
||
754 | public function is_sub_line_item() { |
||
757 | |||
758 | |||
759 | |||
760 | /** |
||
761 | * @return bool |
||
762 | */ |
||
763 | public function is_sub_total() { |
||
766 | |||
767 | |||
768 | |||
769 | /** |
||
770 | * Whether or not this line item is a cancellation line item |
||
771 | * @return boolean |
||
772 | */ |
||
773 | public function is_cancellation() { |
||
776 | |||
777 | |||
778 | |||
779 | /** |
||
780 | * @return bool |
||
781 | */ |
||
782 | public function is_total() { |
||
785 | |||
786 | |||
787 | |||
788 | /** |
||
789 | * @return bool |
||
790 | */ |
||
791 | public function is_cancelled() { |
||
794 | |||
795 | |||
796 | |||
797 | /** |
||
798 | * |
||
799 | * @return string like '2, 004.00', formatted according to the localized currency |
||
800 | */ |
||
801 | public function unit_price_no_code() { |
||
804 | |||
805 | |||
806 | |||
807 | /** |
||
808 | * |
||
809 | * @return string like '2, 004.00', formatted according to the localized currency |
||
810 | */ |
||
811 | public function total_no_code() { |
||
814 | |||
815 | |||
816 | |||
817 | /** |
||
818 | * Gets the final total on this item, taking taxes into account. |
||
819 | * Has the side-effect of setting the sub-total as it was just calculated. |
||
820 | * If this is used on a grand-total line item, also updates the transaction's |
||
821 | * TXN_total (provided this line item is allowed to persist, otherwise we don't |
||
822 | * want to change a persistable transaction with info from a non-persistent line item) |
||
823 | * |
||
824 | * @return float |
||
825 | * @throws \EE_Error |
||
826 | */ |
||
827 | public function recalculate_total_including_taxes() { |
||
849 | |||
850 | |||
851 | /** |
||
852 | * Recursively goes through all the children and recalculates sub-totals EXCEPT for |
||
853 | * tax-sub-totals (they're a an odd beast). Updates the 'total' on each line item according to either its |
||
854 | * unit price * quantity or the total of all its children EXCEPT when we're only calculating the taxable total and when this is called on the grand total |
||
855 | * @return float |
||
856 | * @throws \EE_Error |
||
857 | */ |
||
858 | public function recalculate_pre_tax_total() { |
||
907 | |||
908 | |||
909 | |||
910 | /** |
||
911 | * Calculates the pretax total when this line item is a subtotal or total line item. |
||
912 | * Basically does a sum-then-round approach (ie, any percent line item that are children |
||
913 | * will calculate their total based on the un-rounded total we're working with so far, and |
||
914 | * THEN round the result; instead of rounding as we go like with sub-line-items) |
||
915 | * |
||
916 | * @param float $calculated_total_so_far |
||
917 | * @param EE_Line_Item[] $my_children |
||
918 | * @return float |
||
919 | * @throws \EE_Error |
||
920 | */ |
||
921 | protected function _recalculate_pretax_total_for_subtotal( $calculated_total_so_far, $my_children = null ) { |
||
957 | |||
958 | |||
959 | |||
960 | /** |
||
961 | * Calculates the pretax total for a normal line item, in a round-then-sum approach |
||
962 | * (where each sub-line-item is applied to the base price for the line item |
||
963 | * and the result is immediately rounded, rather than summing all the sub-line-items |
||
964 | * then rounding, like we do when recalculating pretax totals on totals and subtotals). |
||
965 | * |
||
966 | * @param float $calculated_total_so_far |
||
967 | * @param EE_Line_Item[] $my_children |
||
968 | * @return float |
||
969 | * @throws \EE_Error |
||
970 | */ |
||
971 | protected function _recalculate_pretax_total_for_line_item( $calculated_total_so_far, $my_children = null ) { |
||
1011 | |||
1012 | |||
1013 | |||
1014 | /** |
||
1015 | * Recalculates the total on each individual tax (based on a recalculation of the pre-tax total), sets |
||
1016 | * the totals on each tax calculated, and returns the final tax total |
||
1017 | * @return float |
||
1018 | */ |
||
1019 | public function recalculate_taxes_and_tax_total() { |
||
1034 | |||
1035 | |||
1036 | |||
1037 | /** |
||
1038 | * Simply forces all the tax-sub-totals to recalculate. Assumes the taxes have been calculated |
||
1039 | * @return void |
||
1040 | */ |
||
1041 | private function _recalculate_tax_sub_total() { |
||
1062 | |||
1063 | |||
1064 | |||
1065 | /** |
||
1066 | * Gets the total tax on this line item. Assumes taxes have already been calculated using recalculate_taxes_and_total |
||
1067 | * @return float |
||
1068 | */ |
||
1069 | public function get_total_tax() { |
||
1079 | |||
1080 | |||
1081 | /** |
||
1082 | * Gets the total for all the items purchased only |
||
1083 | * @return float |
||
1084 | */ |
||
1085 | public function get_items_total() { |
||
1101 | |||
1102 | |||
1103 | |||
1104 | /** |
||
1105 | * Gets all the descendants (ie, children or children of children etc) that |
||
1106 | * are of the type 'tax' |
||
1107 | * @return EE_Line_Item[] |
||
1108 | */ |
||
1109 | public function tax_descendants() { |
||
1112 | |||
1113 | |||
1114 | |||
1115 | /** |
||
1116 | * Gets all the real items purchased which are children of this item |
||
1117 | * @return EE_Line_Item[] |
||
1118 | */ |
||
1119 | public function get_items() { |
||
1122 | |||
1123 | |||
1124 | |||
1125 | /** |
||
1126 | * Returns the amount taxable among this line item's children (or if it has no children, |
||
1127 | * how much of it is taxable). Does not recalculate totals or subtotals. |
||
1128 | * If the taxable total is negative, (eg, if none of the tickets were taxable, |
||
1129 | * but there is a "Taxable" discount), returns 0. |
||
1130 | * |
||
1131 | * @return float |
||
1132 | * @throws \EE_Error |
||
1133 | */ |
||
1134 | public function taxable_total() { |
||
1153 | |||
1154 | |||
1155 | |||
1156 | /** |
||
1157 | * Gets the transaction for this line item |
||
1158 | * @return EE_Transaction |
||
1159 | */ |
||
1160 | public function transaction() { |
||
1163 | |||
1164 | |||
1165 | |||
1166 | /** |
||
1167 | * Saves this line item to the DB, and recursively saves its descendants. |
||
1168 | * Because there currently is no proper parent-child relation on the model, |
||
1169 | * save_this_and_cached() will NOT save the descendants. |
||
1170 | * Also sets the transaction on this line item and all its descendants before saving |
||
1171 | * @param int $txn_id if none is provided, assumes $this->TXN_ID() |
||
1172 | * @return int count of items saved |
||
1173 | */ |
||
1174 | public function save_this_and_descendants_to_txn( $txn_id = NULL ) { |
||
1190 | |||
1191 | |||
1192 | |||
1193 | /** |
||
1194 | * Saves this line item to the DB, and recursively saves its descendants. |
||
1195 | * |
||
1196 | * @return int count of items saved |
||
1197 | */ |
||
1198 | public function save_this_and_descendants() { |
||
1210 | |||
1211 | |||
1212 | |||
1213 | /** |
||
1214 | * returns the cancellation line item if this item was cancelled |
||
1215 | * @return EE_Line_Item[] |
||
1216 | */ |
||
1217 | public function get_cancellations() { |
||
1221 | |||
1222 | |||
1223 | |||
1224 | /** |
||
1225 | * If this item has an ID, then this saves it again to update the db |
||
1226 | * |
||
1227 | * @return int count of items saved |
||
1228 | */ |
||
1229 | public function maybe_save() { |
||
1235 | |||
1236 | /** |
||
1237 | * clears the cached children and parent from the line item |
||
1238 | * @return void |
||
1239 | */ |
||
1240 | public function clear_related_line_item_cache() { |
||
1244 | |||
1245 | |||
1246 | |||
1247 | /** |
||
1248 | * @param bool $raw |
||
1249 | * @return int |
||
1250 | * @throws \EE_Error |
||
1251 | */ |
||
1252 | public function timestamp($raw = false) |
||
1256 | |||
1257 | |||
1258 | |||
1259 | |||
1260 | /************************* DEPRECATED *************************/ |
||
1261 | |||
1262 | |||
1263 | |||
1264 | /** |
||
1265 | * @deprecated 4.6.0 |
||
1266 | * @param string $type one of the constants on EEM_Line_Item |
||
1267 | * @return EE_Line_Item[] |
||
1268 | */ |
||
1269 | protected function _get_descendants_of_type($type) |
||
1275 | |||
1276 | |||
1277 | |||
1278 | /** |
||
1279 | * @deprecated 4.6.0 |
||
1280 | * @param string $type like one of the EEM_Line_Item::type_* |
||
1281 | * @return EE_Line_Item |
||
1282 | */ |
||
1283 | public function get_nearest_descendant_of_type($type) |
||
1289 | |||
1290 | |||
1291 | |||
1292 | } |
||
1293 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: