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 EventSpacesCalculator 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 EventSpacesCalculator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class EventSpacesCalculator |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var EE_Event $event |
||
28 | */ |
||
29 | private $event; |
||
30 | |||
31 | /** |
||
32 | * @var array $datetime_query_params |
||
33 | */ |
||
34 | private $datetime_query_params; |
||
35 | |||
36 | /** |
||
37 | * @var EE_Ticket[] $active_tickets |
||
38 | */ |
||
39 | private $active_tickets = array(); |
||
40 | |||
41 | /** |
||
42 | * @var EE_Datetime[] $datetimes |
||
43 | */ |
||
44 | private $datetimes = array(); |
||
45 | |||
46 | /** |
||
47 | * Array of Ticket IDs grouped by Datetime |
||
48 | * |
||
49 | * @var array $datetimes |
||
50 | */ |
||
51 | private $datetime_tickets = array(); |
||
52 | |||
53 | /** |
||
54 | * Max spaces for each Datetime (reg limit - previous sold) |
||
55 | * |
||
56 | * @var array $datetime_spaces |
||
57 | */ |
||
58 | private $datetime_spaces = array(); |
||
59 | |||
60 | /** |
||
61 | * Array of Datetime IDs grouped by Ticket |
||
62 | * |
||
63 | * @var array $ticket_datetimes |
||
64 | */ |
||
65 | private $ticket_datetimes = array(); |
||
66 | |||
67 | /** |
||
68 | * maximum ticket quantities for each ticket (adjusted for reg limit) |
||
69 | * |
||
70 | * @var array $ticket_quantities |
||
71 | */ |
||
72 | private $ticket_quantities = array(); |
||
73 | |||
74 | /** |
||
75 | * total quantity of sold and reserved for each ticket |
||
76 | * |
||
77 | * @var array $tickets_sold |
||
78 | */ |
||
79 | private $tickets_sold = array(); |
||
80 | |||
81 | /** |
||
82 | * total spaces available across all datetimes |
||
83 | * |
||
84 | * @var array $total_spaces |
||
85 | */ |
||
86 | private $total_spaces = array(); |
||
87 | |||
88 | /** |
||
89 | * @var boolean $debug |
||
90 | */ |
||
91 | private $debug = false; |
||
92 | |||
93 | |||
94 | |||
95 | /** |
||
96 | * EventSpacesCalculator constructor. |
||
97 | * |
||
98 | * @param EE_Event $event |
||
99 | * @param array $datetime_query_params |
||
100 | * @throws EE_Error |
||
101 | */ |
||
102 | public function __construct(EE_Event $event, array $datetime_query_params = array()) |
||
107 | |||
108 | |||
109 | |||
110 | /** |
||
111 | * @return EE_Ticket[] |
||
112 | * @throws EE_Error |
||
113 | */ |
||
114 | public function getActiveTickets() |
||
129 | |||
130 | |||
131 | |||
132 | /** |
||
133 | * @param EE_Ticket[] $active_tickets |
||
134 | * @throws EE_Error |
||
135 | */ |
||
136 | public function setActiveTickets(array $active_tickets) |
||
150 | |||
151 | |||
152 | |||
153 | /** |
||
154 | * @return EE_Datetime[] |
||
155 | */ |
||
156 | public function getDatetimes() |
||
160 | |||
161 | |||
162 | |||
163 | /** |
||
164 | * @param EE_Datetime $datetime |
||
165 | * @throws EE_Error |
||
166 | * |
||
167 | */ |
||
168 | public function setDatetime(EE_Datetime $datetime) |
||
172 | |||
173 | |||
174 | |||
175 | /** |
||
176 | * calculate spaces remaining based on "saleable" tickets |
||
177 | * |
||
178 | * @return int|float |
||
179 | * @throws EE_Error |
||
180 | */ |
||
181 | public function spacesRemaining() |
||
186 | |||
187 | |||
188 | |||
189 | /** |
||
190 | * calculates total available spaces for an event with no regard for sold tickets |
||
191 | * |
||
192 | * @return int|float |
||
193 | * @throws EE_Error |
||
194 | */ |
||
195 | public function totalSpacesAvailable() |
||
200 | |||
201 | |||
202 | |||
203 | /** |
||
204 | * Loops through the active tickets for the event |
||
205 | * and builds a series of data arrays that will be used for calculating |
||
206 | * the total maximum available spaces, as well as the spaces remaining. |
||
207 | * Because ticket quantities affect datetime spaces and vice versa, |
||
208 | * we need to be constantly updating these data arrays as things change, |
||
209 | * which is the entire reason for their existence. |
||
210 | * |
||
211 | * @throws EE_Error |
||
212 | */ |
||
213 | private function initialize() |
||
276 | |||
277 | |||
278 | |||
279 | /** |
||
280 | * performs calculations on initialized data |
||
281 | * |
||
282 | * @param bool $consider_sold |
||
283 | * @return int|float |
||
284 | */ |
||
285 | private function calculate($consider_sold = true) |
||
306 | |||
307 | |||
308 | |||
309 | /** |
||
310 | * @param string $DTT_ID |
||
311 | * @param array $tickets |
||
312 | */ |
||
313 | private function trackAvailableSpacesForDatetimes($DTT_ID, array $tickets) |
||
360 | |||
361 | |||
362 | |||
363 | /** |
||
364 | * @param string $DTT_ID |
||
365 | * @param int $reg_limit |
||
366 | * @param string $TKT_ID |
||
367 | * @param int $spaces |
||
368 | * @return int |
||
369 | */ |
||
370 | private function calculateAvailableSpacesForTicket($DTT_ID, $reg_limit,$TKT_ID, $spaces) |
||
415 | |||
416 | |||
417 | |||
418 | /** |
||
419 | * subtracts ticket amounts from all datetime reg limits |
||
420 | * that allow access to the ticket specified, |
||
421 | * because that ticket could be used |
||
422 | * to attend any of the datetimes it has access to |
||
423 | * |
||
424 | * @param string $DTT_ID |
||
425 | * @param int $spaces |
||
426 | * @param int $reg_limit |
||
427 | * @param string $TKT_ID |
||
428 | * @param int $ticket_quantity |
||
429 | */ |
||
430 | private function adjustDatetimes($DTT_ID, $spaces, $reg_limit, $TKT_ID, $ticket_quantity) |
||
465 | |||
466 | } |
||
467 | // Location: EventSpacesCalculator.php |
||
468 |