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 |
||
27 | class Event extends EventCalculationBase |
||
28 | { |
||
29 | /** |
||
30 | * @var EEM_Event |
||
31 | */ |
||
32 | protected $event_model; |
||
33 | |||
34 | /** |
||
35 | * @var EEM_Registration |
||
36 | */ |
||
37 | protected $registration_model; |
||
38 | public function __construct(EEM_Event $event_model, EEM_Registration $registration_model) |
||
43 | |||
44 | /** |
||
45 | * Calculates the total spaces on the event (not subtracting sales, but taking |
||
46 | * sales into account; so this is the optimum sales that CAN still be achieved) |
||
47 | * See EE_Event::total_available_spaces( true ); |
||
48 | * |
||
49 | * @param array $wpdb_row |
||
50 | * @param WP_REST_Request $request |
||
51 | * @param EventControllerBase $controller |
||
52 | * @return int |
||
53 | * @throws EE_Error |
||
54 | * @throws DomainException |
||
55 | * @throws InvalidDataTypeException |
||
56 | * @throws InvalidInterfaceException |
||
57 | * @throws UnexpectedEntityException |
||
58 | * @throws InvalidArgumentException |
||
59 | */ |
||
60 | View Code Duplication | public function optimumSalesAtStart($wpdb_row, $request, $controller) |
|
82 | |||
83 | |||
84 | /** |
||
85 | * Calculates the total spaces on the event (ignoring all sales; so this is the optimum |
||
86 | * sales that COULD have been achieved) |
||
87 | * See EE_Event::total_available_spaces( true ); |
||
88 | * |
||
89 | * @param array $wpdb_row |
||
90 | * @param WP_REST_Request $request |
||
91 | * @param EventControllerBase $controller |
||
92 | * @return int |
||
93 | * @throws DomainException |
||
94 | * @throws EE_Error |
||
95 | * @throws InvalidArgumentException |
||
96 | * @throws InvalidDataTypeException |
||
97 | * @throws InvalidInterfaceException |
||
98 | * @throws UnexpectedEntityException |
||
99 | */ |
||
100 | View Code Duplication | public function optimumSalesNow($wpdb_row, $request, $controller) |
|
122 | |||
123 | |||
124 | /** |
||
125 | * Like optimum_sales_now, but minus total sales so far. |
||
126 | * See EE_Event::spaces_remaining_for_sale( true ); |
||
127 | * |
||
128 | * @param array $wpdb_row |
||
129 | * @param WP_REST_Request $request |
||
130 | * @param EventControllerBase $controller |
||
131 | * @return int |
||
132 | * @throws DomainException |
||
133 | * @throws EE_Error |
||
134 | * @throws InvalidArgumentException |
||
135 | * @throws InvalidDataTypeException |
||
136 | * @throws InvalidInterfaceException |
||
137 | * @throws UnexpectedEntityException |
||
138 | */ |
||
139 | View Code Duplication | public function spacesRemaining($wpdb_row, $request, $controller) |
|
161 | |||
162 | |||
163 | /** |
||
164 | * Counts the number of approved registrations for this event (regardless |
||
165 | * of how many datetimes each registrations' ticket purchase is for) |
||
166 | * |
||
167 | * @param array $wpdb_row |
||
168 | * @param WP_REST_Request $request |
||
169 | * @param EventControllerBase $controller |
||
170 | * @return int |
||
171 | * @throws EE_Error |
||
172 | * @throws InvalidArgumentException |
||
173 | * @throws InvalidDataTypeException |
||
174 | * @throws InvalidInterfaceException |
||
175 | */ |
||
176 | public function spotsTaken($wpdb_row, $request, $controller) |
||
202 | |||
203 | |||
204 | /** |
||
205 | * Counts the number of pending-payment registrations for this event (regardless |
||
206 | * of how many datetimes each registrations' ticket purchase is for) |
||
207 | * |
||
208 | * @param array $wpdb_row |
||
209 | * @param WP_REST_Request $request |
||
210 | * @param EventControllerBase $controller |
||
211 | * @return int |
||
212 | * @throws EE_Error |
||
213 | * @throws InvalidArgumentException |
||
214 | * @throws InvalidDataTypeException |
||
215 | * @throws InvalidInterfaceException |
||
216 | * @throws RestException |
||
217 | */ |
||
218 | View Code Duplication | public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
|
245 | |||
246 | |||
247 | /** |
||
248 | * Counts all the registrations who have checked into one of this events' datetimes |
||
249 | * See EE_Event::total_available_spaces( false ); |
||
250 | * |
||
251 | * @param array $wpdb_row |
||
252 | * @param WP_REST_Request $request |
||
253 | * @param EventControllerBase $controller |
||
254 | * @return int|null if permission denied |
||
255 | * @throws EE_Error |
||
256 | * @throws InvalidArgumentException |
||
257 | * @throws InvalidDataTypeException |
||
258 | * @throws InvalidInterfaceException |
||
259 | * @throws RestException |
||
260 | */ |
||
261 | View Code Duplication | public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
|
279 | |||
280 | |||
281 | /** |
||
282 | * Counts all the registrations who have checked out of one of this events' datetimes |
||
283 | * See EE_Event::total_available_spaces( false ); |
||
284 | * |
||
285 | * @param array $wpdb_row |
||
286 | * @param WP_REST_Request $request |
||
287 | * @param EventControllerBase $controller |
||
288 | * @return int |
||
289 | * @throws EE_Error |
||
290 | * @throws InvalidArgumentException |
||
291 | * @throws InvalidDataTypeException |
||
292 | * @throws InvalidInterfaceException |
||
293 | * @throws RestException |
||
294 | */ |
||
295 | View Code Duplication | public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
|
313 | |||
314 | |||
315 | /** |
||
316 | * Gets the thumbnail image |
||
317 | * |
||
318 | * @param array $wpdb_row |
||
319 | * @param WP_REST_Request $request |
||
320 | * @param EventControllerBase $controller |
||
321 | * @return array |
||
322 | * @throws EE_Error |
||
323 | */ |
||
324 | public function imageThumbnail($wpdb_row, $request, $controller) |
||
328 | |||
329 | |||
330 | /** |
||
331 | * Gets the medium image |
||
332 | * |
||
333 | * @param array $wpdb_row |
||
334 | * @param WP_REST_Request $request |
||
335 | * @param EventControllerBase $controller |
||
336 | * @return array |
||
337 | * @throws EE_Error |
||
338 | */ |
||
339 | public function imageMedium($wpdb_row, $request, $controller) |
||
343 | |||
344 | |||
345 | /** |
||
346 | * Gets the medium-large image |
||
347 | * |
||
348 | * @param array $wpdb_row |
||
349 | * @param WP_REST_Request $request |
||
350 | * @param EventControllerBase $controller |
||
351 | * @return array |
||
352 | * @throws EE_Error |
||
353 | */ |
||
354 | public function imageMediumLarge($wpdb_row, $request, $controller) |
||
358 | |||
359 | |||
360 | /** |
||
361 | * Gets the large image |
||
362 | * |
||
363 | * @param array $wpdb_row |
||
364 | * @param WP_REST_Request $request |
||
365 | * @param EventControllerBase $controller |
||
366 | * @return array |
||
367 | * @throws EE_Error |
||
368 | */ |
||
369 | public function imageLarge($wpdb_row, $request, $controller) |
||
373 | |||
374 | |||
375 | /** |
||
376 | * Gets the post-thumbnail image |
||
377 | * |
||
378 | * @param array $wpdb_row |
||
379 | * @param WP_REST_Request $request |
||
380 | * @param EventControllerBase $controller |
||
381 | * @return array |
||
382 | * @throws EE_Error |
||
383 | */ |
||
384 | public function imagePostThumbnail($wpdb_row, $request, $controller) |
||
388 | |||
389 | |||
390 | /** |
||
391 | * Gets the full size image |
||
392 | * |
||
393 | * @param array $wpdb_row |
||
394 | * @param WP_REST_Request $request |
||
395 | * @param EventControllerBase $controller |
||
396 | * @return array |
||
397 | * @throws EE_Error |
||
398 | */ |
||
399 | public function imageFull($wpdb_row, $request, $controller) |
||
403 | |||
404 | |||
405 | /** |
||
406 | * Gets image specs and formats them for the display in the API, |
||
407 | * according to the image size requested |
||
408 | * |
||
409 | * @param array $wpdb_row |
||
410 | * @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full |
||
411 | * @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original' |
||
412 | * @throws EE_Error |
||
413 | */ |
||
414 | protected function calculateImageData($wpdb_row, $image_size) |
||
446 | |||
447 | |||
448 | /** |
||
449 | * Returns true if the array of data contains 'Event_CPT.ID'. False otherwise |
||
450 | * |
||
451 | * @param array $wpdb_row |
||
452 | * @return bool |
||
453 | */ |
||
454 | protected function wpdbRowHasEventId($wpdb_row) |
||
458 | |||
459 | |||
460 | /** |
||
461 | * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
||
462 | * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
||
463 | * |
||
464 | * @since $VID:$ |
||
465 | * @return array |
||
466 | */ |
||
467 | public function schemaForCalculations() |
||
589 | } |
||
590 |