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 DisplayTicketSelector 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 DisplayTicketSelector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class DisplayTicketSelector |
||
| 35 | { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * event that ticket selector is being generated for |
||
| 39 | * |
||
| 40 | * @access protected |
||
| 41 | * @var EE_Event $event |
||
| 42 | */ |
||
| 43 | protected $event; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Used to flag when the ticket selector is being called from an external iframe. |
||
| 47 | * |
||
| 48 | * @var bool $iframe |
||
| 49 | */ |
||
| 50 | protected $iframe = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * max attendees that can register for event at one time |
||
| 54 | * |
||
| 55 | * @var int $max_attendees |
||
| 56 | */ |
||
| 57 | private $max_attendees = EE_INF; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string $date_format |
||
| 61 | */ |
||
| 62 | private $date_format; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string $time_format |
||
| 66 | */ |
||
| 67 | private $time_format; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var boolean $display_full_ui |
||
| 71 | */ |
||
| 72 | private $display_full_ui; |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * DisplayTicketSelector constructor. |
||
| 77 | * |
||
| 78 | * @param bool $iframe |
||
| 79 | */ |
||
| 80 | public function __construct($iframe = false) |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | public function isIframe() |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * @param boolean $iframe |
||
| 105 | */ |
||
| 106 | public function setIframe($iframe = true) |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * finds and sets the \EE_Event object for use throughout class |
||
| 114 | * |
||
| 115 | * @param mixed $event |
||
| 116 | * @return bool |
||
| 117 | * @throws EE_Error |
||
| 118 | * @throws InvalidDataTypeException |
||
| 119 | * @throws InvalidInterfaceException |
||
| 120 | * @throws InvalidArgumentException |
||
| 121 | */ |
||
| 122 | protected function setEvent($event = null) |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * @return int |
||
| 152 | */ |
||
| 153 | public function getMaxAttendees() |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * @param int $max_attendees |
||
| 161 | */ |
||
| 162 | public function setMaxAttendees($max_attendees) |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns whether or not the full ticket selector should be shown or not. |
||
| 175 | * Currently, it displays on the frontend (including ajax requests) but not the backend |
||
| 176 | * |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | private function display_full_ui() |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * creates buttons for selecting number of attendees for an event |
||
| 190 | * |
||
| 191 | * @param WP_Post|int $event |
||
| 192 | * @param bool $view_details |
||
| 193 | * @return string |
||
| 194 | * @throws EE_Error |
||
| 195 | * @throws InvalidArgumentException |
||
| 196 | * @throws InvalidDataTypeException |
||
| 197 | * @throws InvalidInterfaceException |
||
| 198 | */ |
||
| 199 | public function display($event = null, $view_details = false) |
||
| 200 | { |
||
| 201 | // reset filter for displaying submit button |
||
| 202 | remove_filter('FHEE__EE_Ticket_Selector__display_ticket_selector_submit', '__return_true'); |
||
| 203 | // poke and prod incoming event till it tells us what it is |
||
| 204 | if (! $this->setEvent($event)) { |
||
| 205 | return $this->handleMissingEvent(); |
||
| 206 | } |
||
| 207 | // is the event expired ? |
||
| 208 | $template_args['event_is_expired'] = ! is_admin() ? $this->event->is_expired() : false; |
||
| 209 | if ($template_args['event_is_expired']) { |
||
| 210 | return is_single() ? $this->expiredEventMessage() : $this->expiredEventMessage() . $this->displayViewDetailsButton(); |
||
| 211 | } |
||
| 212 | // begin gathering template arguments by getting event status |
||
| 213 | $template_args = array('event_status' => $this->event->get_active_status()); |
||
| 214 | if ($this->activeEventAndShowTicketSelector( |
||
| 215 | $event, |
||
| 216 | $template_args['event_status'], |
||
| 217 | $view_details |
||
| 218 | )) { |
||
| 219 | return ! is_single() ? $this->displayViewDetailsButton() : ''; |
||
| 220 | } |
||
| 221 | // filter the maximum qty that can appear in the Ticket Selector qty dropdowns |
||
| 222 | $this->setMaxAttendees($this->event->additional_limit()); |
||
| 223 | if ($this->getMaxAttendees() < 1) { |
||
| 224 | return $this->ticketSalesClosedMessage(); |
||
| 225 | } |
||
| 226 | // get all tickets for this event ordered by the datetime |
||
| 227 | $tickets = $this->getTickets(); |
||
| 228 | if (count($tickets) < 1) { |
||
| 229 | return $this->noTicketAvailableMessage(); |
||
| 230 | } |
||
| 231 | // redirecting to another site for registration ?? |
||
| 232 | $external_url = (string) $this->event->external_url() |
||
| 233 | && $this->event->external_url() !== get_the_permalink() |
||
| 234 | ? $this->event->external_url() |
||
| 235 | : ''; |
||
| 236 | // if redirecting to another site for registration, then we don't load the TS |
||
| 237 | $ticket_selector = $external_url |
||
| 238 | ? $this->externalEventRegistration() |
||
| 239 | : $this->loadTicketSelector($tickets, $template_args); |
||
| 240 | // now set up the form (but not for the admin) |
||
| 241 | $ticket_selector = $this->display_full_ui() |
||
| 242 | ? $this->formOpen($this->event->ID(), $external_url) . $ticket_selector |
||
| 243 | : $ticket_selector; |
||
| 244 | // submit button and form close tag |
||
| 245 | $ticket_selector .= $this->display_full_ui() ? $this->displaySubmitButton($external_url) : ''; |
||
| 246 | return $ticket_selector; |
||
| 247 | } |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * displayTicketSelector |
||
| 252 | * examines the event properties and determines whether a Ticket Selector should be displayed |
||
| 253 | * |
||
| 254 | * @param WP_Post|int $event |
||
| 255 | * @param string $_event_active_status |
||
| 256 | * @param bool $view_details |
||
| 257 | * @return bool |
||
| 258 | * @throws EE_Error |
||
| 259 | */ |
||
| 260 | protected function activeEventAndShowTicketSelector($event, $_event_active_status, $view_details) |
||
| 279 | |||
| 280 | |||
| 281 | /** |
||
| 282 | * noTicketAvailableMessage |
||
| 283 | * notice displayed if event is expired |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | * @throws EE_Error |
||
| 287 | */ |
||
| 288 | protected function expiredEventMessage() |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * noTicketAvailableMessage |
||
| 299 | * notice displayed if event has no more tickets available |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | * @throws EE_Error |
||
| 303 | */ |
||
| 304 | View Code Duplication | protected function noTicketAvailableMessage() |
|
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * ticketSalesClosed |
||
| 330 | * notice displayed if event ticket sales are turned off |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | * @throws EE_Error |
||
| 334 | */ |
||
| 335 | View Code Duplication | protected function ticketSalesClosedMessage() |
|
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * getTickets |
||
| 361 | * |
||
| 362 | * @return \EE_Base_Class[]|\EE_Ticket[] |
||
| 363 | * @throws EE_Error |
||
| 364 | * @throws InvalidDataTypeException |
||
| 365 | * @throws InvalidInterfaceException |
||
| 366 | * @throws InvalidArgumentException |
||
| 367 | */ |
||
| 368 | protected function getTickets() |
||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * loadTicketSelector |
||
| 398 | * begins to assemble template arguments |
||
| 399 | * and decides whether to load a "simple" ticket selector, or the standard |
||
| 400 | * |
||
| 401 | * @param \EE_Ticket[] $tickets |
||
| 402 | * @param array $template_args |
||
| 403 | * @return string |
||
| 404 | * @throws EE_Error |
||
| 405 | */ |
||
| 406 | protected function loadTicketSelector(array $tickets, array $template_args) |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * simpleTicketSelector |
||
| 444 | * there's one ticket, and max attendees is set to one, |
||
| 445 | * so if the event is free, then this is a "simple" ticket selector |
||
| 446 | * a.k.a. "Dude Where's my Ticket Selector?" |
||
| 447 | * |
||
| 448 | * @param \EE_Ticket[] $tickets |
||
| 449 | * @param array $template_args |
||
| 450 | * @return string |
||
| 451 | * @throws EE_Error |
||
| 452 | */ |
||
| 453 | protected function simpleTicketSelector($tickets, array $template_args) |
||
| 476 | |||
| 477 | |||
| 478 | /** |
||
| 479 | * externalEventRegistration |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public function externalEventRegistration() |
||
| 495 | |||
| 496 | |||
| 497 | /** |
||
| 498 | * formOpen |
||
| 499 | * |
||
| 500 | * @param int $ID |
||
| 501 | * @param string $external_url |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | public function formOpen($ID = 0, $external_url = '') |
||
| 551 | |||
| 552 | |||
| 553 | /** |
||
| 554 | * displaySubmitButton |
||
| 555 | * |
||
| 556 | * @param string $external_url |
||
| 557 | * @return string |
||
| 558 | * @throws EE_Error |
||
| 559 | */ |
||
| 560 | public function displaySubmitButton($external_url = '') |
||
| 634 | |||
| 635 | |||
| 636 | /** |
||
| 637 | * @return string |
||
| 638 | * @throws EE_Error |
||
| 639 | */ |
||
| 640 | public function displayRegisterNowButton() |
||
| 669 | |||
| 670 | |||
| 671 | /** |
||
| 672 | * displayViewDetailsButton |
||
| 673 | * |
||
| 674 | * @param bool $DWMTS indicates a "Dude Where's my Ticket Selector?" (DWMTS) type event |
||
| 675 | * (ie: $_max_atndz === 1) where there are no available tickets, |
||
| 676 | * either because they are sold out, expired, or not yet on sale. |
||
| 677 | * In this case, we need to close the form BEFORE adding any closing divs |
||
| 678 | * @return string |
||
| 679 | * @throws EE_Error |
||
| 680 | */ |
||
| 681 | public function displayViewDetailsButton($DWMTS = false) |
||
| 729 | |||
| 730 | |||
| 731 | /** |
||
| 732 | * @return string |
||
| 733 | */ |
||
| 734 | public function ticketSelectorEndDiv() |
||
| 738 | |||
| 739 | |||
| 740 | /** |
||
| 741 | * @return string |
||
| 742 | */ |
||
| 743 | public function clearTicketSelector() |
||
| 748 | |||
| 749 | |||
| 750 | /** |
||
| 751 | * @access public |
||
| 752 | * @return string |
||
| 753 | */ |
||
| 754 | public function formClose() |
||
| 758 | |||
| 759 | |||
| 760 | /** |
||
| 761 | * handleMissingEvent |
||
| 762 | * Returns either false or an error to display when no valid event is passed. |
||
| 763 | * |
||
| 764 | * @return mixed |
||
| 765 | * @throws ExceptionStackTraceDisplay |
||
| 766 | * @throws InvalidInterfaceException |
||
| 767 | */ |
||
| 768 | protected function handleMissingEvent() |
||
| 801 | } |
||
| 802 |