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 |
||
27 | class EventSpacesCalculator |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * @var EE_Event $event |
||
32 | */ |
||
33 | private $event; |
||
34 | |||
35 | /** |
||
36 | * @var array $datetime_query_params |
||
37 | */ |
||
38 | private $datetime_query_params; |
||
39 | |||
40 | /** |
||
41 | * @var EE_Ticket[] $active_tickets |
||
42 | */ |
||
43 | private $active_tickets = array(); |
||
44 | |||
45 | /** |
||
46 | * @var EE_Datetime[] $datetimes |
||
47 | */ |
||
48 | private $datetimes = array(); |
||
49 | |||
50 | /** |
||
51 | * Array of Ticket IDs grouped by Datetime |
||
52 | * |
||
53 | * @var array $datetimes |
||
54 | */ |
||
55 | private $datetime_tickets = array(); |
||
56 | |||
57 | /** |
||
58 | * Max spaces for each Datetime (reg limit - previous sold) |
||
59 | * |
||
60 | * @var array $datetime_spaces |
||
61 | */ |
||
62 | private $datetime_spaces = array(); |
||
63 | |||
64 | /** |
||
65 | * Array of Datetime IDs grouped by Ticket |
||
66 | * |
||
67 | * @var array $ticket_datetimes |
||
68 | */ |
||
69 | private $ticket_datetimes = array(); |
||
70 | |||
71 | /** |
||
72 | * maximum ticket quantities for each ticket (adjusted for reg limit) |
||
73 | * |
||
74 | * @var array $ticket_quantities |
||
75 | */ |
||
76 | private $ticket_quantities = array(); |
||
77 | |||
78 | /** |
||
79 | * total quantity of sold and reserved for each ticket |
||
80 | * |
||
81 | * @var array $tickets_sold |
||
82 | */ |
||
83 | private $tickets_sold = array(); |
||
84 | |||
85 | /** |
||
86 | * total spaces available across all datetimes |
||
87 | * |
||
88 | * @var array $total_spaces |
||
89 | */ |
||
90 | private $total_spaces = array(); |
||
91 | |||
92 | /** |
||
93 | * @var boolean $debug |
||
94 | */ |
||
95 | private $debug = false; |
||
96 | |||
97 | |||
98 | |||
99 | /** |
||
100 | * EventSpacesCalculator constructor. |
||
101 | * |
||
102 | * @param EE_Event $event |
||
103 | * @param array $datetime_query_params |
||
104 | * @throws EE_Error |
||
105 | */ |
||
106 | public function __construct(EE_Event $event, array $datetime_query_params = array()) |
||
111 | |||
112 | |||
113 | |||
114 | /** |
||
115 | * @return EE_Ticket[] |
||
116 | * @throws EE_Error |
||
117 | */ |
||
118 | public function getActiveTickets() |
||
133 | |||
134 | |||
135 | |||
136 | /** |
||
137 | * @param EE_Ticket[] $active_tickets |
||
138 | * @throws EE_Error |
||
139 | * @throws DomainException |
||
140 | * @throws UnexpectedEntityException |
||
141 | */ |
||
142 | public function setActiveTickets(array $active_tickets = array()) |
||
163 | |||
164 | |||
165 | |||
166 | /** |
||
167 | * @param $ticket |
||
168 | * @throws DomainException |
||
169 | * @throws EE_Error |
||
170 | * @throws UnexpectedEntityException |
||
171 | */ |
||
172 | private function validateTicket($ticket) |
||
195 | |||
196 | |||
197 | |||
198 | /** |
||
199 | * @return EE_Datetime[] |
||
200 | */ |
||
201 | public function getDatetimes() |
||
205 | |||
206 | |||
207 | |||
208 | /** |
||
209 | * @param EE_Datetime $datetime |
||
210 | * @throws EE_Error |
||
211 | * @throws DomainException |
||
212 | */ |
||
213 | public function setDatetime(EE_Datetime $datetime) |
||
229 | |||
230 | |||
231 | |||
232 | /** |
||
233 | * calculate spaces remaining based on "saleable" tickets |
||
234 | * |
||
235 | * @return float|int |
||
236 | * @throws EE_Error |
||
237 | * @throws DomainException |
||
238 | * @throws UnexpectedEntityException |
||
239 | */ |
||
240 | public function spacesRemaining() |
||
245 | |||
246 | |||
247 | |||
248 | /** |
||
249 | * calculates total available spaces for an event with no regard for sold tickets |
||
250 | * |
||
251 | * @return int|float |
||
252 | * @throws EE_Error |
||
253 | * @throws DomainException |
||
254 | * @throws UnexpectedEntityException |
||
255 | */ |
||
256 | public function totalSpacesAvailable() |
||
261 | |||
262 | |||
263 | |||
264 | /** |
||
265 | * Loops through the active tickets for the event |
||
266 | * and builds a series of data arrays that will be used for calculating |
||
267 | * the total maximum available spaces, as well as the spaces remaining. |
||
268 | * Because ticket quantities affect datetime spaces and vice versa, |
||
269 | * we need to be constantly updating these data arrays as things change, |
||
270 | * which is the entire reason for their existence. |
||
271 | * |
||
272 | * @throws EE_Error |
||
273 | * @throws DomainException |
||
274 | * @throws UnexpectedEntityException |
||
275 | */ |
||
276 | private function initialize() |
||
336 | |||
337 | |||
338 | |||
339 | /** |
||
340 | * performs calculations on initialized data |
||
341 | * |
||
342 | * @param bool $consider_sold |
||
343 | * @return int|float |
||
344 | */ |
||
345 | private function calculate($consider_sold = true) |
||
366 | |||
367 | |||
368 | |||
369 | /** |
||
370 | * @param string $datetime_identifier |
||
371 | * @param array $tickets |
||
372 | */ |
||
373 | private function trackAvailableSpacesForDatetimes($datetime_identifier, array $tickets) |
||
420 | |||
421 | |||
422 | |||
423 | /** |
||
424 | * @param string $datetime_identifier |
||
425 | * @param int $reg_limit |
||
426 | * @param string $ticket_identifier |
||
427 | * @param int $available_spaces |
||
428 | * @return int |
||
429 | */ |
||
430 | private function calculateAvailableSpacesForTicket($datetime_identifier, $reg_limit,$ticket_identifier, $available_spaces) |
||
475 | |||
476 | |||
477 | |||
478 | /** |
||
479 | * subtracts ticket amounts from all datetime reg limits |
||
480 | * that allow access to the ticket specified, |
||
481 | * because that ticket could be used |
||
482 | * to attend any of the datetimes it has access to |
||
483 | * |
||
484 | * @param string $datetime_identifier |
||
485 | * @param int $available_spaces |
||
486 | * @param int $reg_limit |
||
487 | * @param string $ticket_identifier |
||
488 | * @param int $ticket_quantity |
||
489 | */ |
||
490 | private function adjustDatetimes($datetime_identifier, $available_spaces, $reg_limit, $ticket_identifier, $ticket_quantity) |
||
525 | |||
526 | } |
||
527 | // Location: EventSpacesCalculator.php |
||
528 |