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 |
||
| 40 | class TicketExtension extends DataExtension |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var CalendarEvent_Controller |
||
| 44 | */ |
||
| 45 | protected $controller; |
||
| 46 | |||
| 47 | private static $db = array( |
||
|
|
|||
| 48 | 'Capacity' => 'Int', |
||
| 49 | 'OrderMin' => 'Int', |
||
| 50 | 'OrderMax' => 'Int', |
||
| 51 | 'SuccessMessage' => 'HTMLText', |
||
| 52 | 'SuccessMessageMail' => 'HTMLText' |
||
| 53 | ); |
||
| 54 | |||
| 55 | private static $has_many = array( |
||
| 56 | 'Tickets' => 'Broarm\EventTickets\Ticket.Event', |
||
| 57 | 'Reservations' => 'Broarm\EventTickets\Reservation.Event', |
||
| 58 | 'Attendees' => 'Broarm\EventTickets\Attendee.Event', |
||
| 59 | 'WaitingList' => 'Broarm\EventTickets\WaitingListRegistration.Event', |
||
| 60 | 'Fields' => 'Broarm\EventTickets\UserField.Event' |
||
| 61 | ); |
||
| 62 | |||
| 63 | private static $defaults = array( |
||
| 64 | 'Capacity' => 50 |
||
| 65 | ); |
||
| 66 | |||
| 67 | private static $translate = array( |
||
| 68 | 'SuccessMessage', |
||
| 69 | 'SuccessMessageMail' |
||
| 70 | ); |
||
| 71 | |||
| 72 | protected $cachedGuestList; |
||
| 73 | |||
| 74 | public function updateCMSFields(FieldList $fields) |
||
| 75 | { |
||
| 76 | $ticketLabel = _t('TicketExtension.Tickets', 'Tickets'); |
||
| 77 | $fields->addFieldsToTab( |
||
| 78 | "Root.$ticketLabel", array( |
||
| 79 | GridField::create('Tickets', $ticketLabel, $this->owner->Tickets(), TicketsGridFieldConfig::create($this->canCreateTickets())), |
||
| 80 | NumericField::create('Capacity', _t('TicketExtension.Capacity', 'Capacity')), |
||
| 81 | HtmlEditorField::create('SuccessMessage', _t('TicketExtension.SuccessMessage', 'Success message'))->setRows(4), |
||
| 82 | HtmlEditorField::create('SuccessMessageMail', _t('TicketExtension.MailMessage', 'Mail message'))->setRows(4) |
||
| 83 | )); |
||
| 84 | |||
| 85 | // Create Reservations tab |
||
| 86 | View Code Duplication | if ($this->owner->Reservations()->exists()) { |
|
| 87 | $reservationLabel = _t('TicketExtension.Reservations', 'Reservations'); |
||
| 88 | $fields->addFieldToTab( |
||
| 89 | "Root.$reservationLabel", |
||
| 90 | GridField::create('Reservations', $reservationLabel, $this->owner->Reservations(), ReservationGridFieldConfig::create()) |
||
| 91 | ); |
||
| 92 | } |
||
| 93 | |||
| 94 | // Create Attendees tab |
||
| 95 | View Code Duplication | if ($this->owner->Attendees()->exists()) { |
|
| 96 | $guestListLabel = _t('TicketExtension.GuestList', 'GuestList'); |
||
| 97 | $fields->addFieldToTab( |
||
| 98 | "Root.$guestListLabel", |
||
| 99 | GridField::create('Attendees', $guestListLabel, $this->owner->Attendees(), GuestListGridFieldConfig::create($this->owner)) |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | |||
| 103 | // Create WaitingList tab |
||
| 104 | View Code Duplication | if ($this->owner->WaitingList()->exists()) { |
|
| 105 | $waitingListLabel = _t('TicketExtension.WaitingList', 'WaitingList'); |
||
| 106 | $fields->addFieldToTab( |
||
| 107 | "Root.$waitingListLabel", |
||
| 108 | GridField::create('WaitingList', $waitingListLabel, $this->owner->WaitingList(), GridFieldConfig_RecordEditor::create()) |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | |||
| 112 | // Create Fields tab |
||
| 113 | $extraFieldsLabel = _t('TicketExtension.ExtraFields', 'Attendee fields'); |
||
| 114 | $fields->addFieldToTab( |
||
| 115 | "Root.$extraFieldsLabel", |
||
| 116 | GridField::create('ExtraFields', $extraFieldsLabel, $this->owner->Fields(), UserFieldsGridFieldConfig::create()) |
||
| 117 | ); |
||
| 118 | |||
| 119 | $this->owner->extend('updateTicketExtensionFields', $fields); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Trigger actions after write |
||
| 124 | */ |
||
| 125 | public function onAfterWrite() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Creates and sets up the default fields |
||
| 133 | */ |
||
| 134 | public function createDefaultFields() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Extend the page actions with an start check in action |
||
| 147 | * |
||
| 148 | * @param FieldList $actions |
||
| 149 | */ |
||
| 150 | public function updateCMSActions(FieldList $actions) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get the leftover capacity |
||
| 169 | * |
||
| 170 | * @return int |
||
| 171 | */ |
||
| 172 | public function getAvailability() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Check if the tickets are still available |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | public function getTicketsAvailable() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Check if the tickets are sold out |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | public function getTicketsSoldOut() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * get The sale start date |
||
| 198 | * |
||
| 199 | * @return \SS_DateTime |
||
| 200 | */ |
||
| 201 | public function getTicketSaleStartDate() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Check if the event is expired, either by unavailable tickets or because the date has passed |
||
| 218 | * |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | public function getEventExpired() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Check if the ticket sale is still pending |
||
| 236 | * |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | public function getTicketSalePending() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get only the attendees who are certain to attend |
||
| 246 | * Also includes attendees without any reservation, these are manually added |
||
| 247 | * |
||
| 248 | * @return \DataList |
||
| 249 | */ |
||
| 250 | public function getGuestList() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get the checked in count for display in templates |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function getCheckedInCount() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get the success message |
||
| 276 | * |
||
| 277 | * @return mixed|string |
||
| 278 | */ |
||
| 279 | View Code Duplication | public function getSuccessContent() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Get the mail message |
||
| 290 | * |
||
| 291 | * @return mixed|string |
||
| 292 | */ |
||
| 293 | View Code Duplication | public function getMailContent() |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Get the Ticket logo |
||
| 304 | * |
||
| 305 | * @return \Image |
||
| 306 | */ |
||
| 307 | public function getMailLogo() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Check if the current event can have tickets |
||
| 314 | * |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | public function canCreateTickets() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get the calendar controller |
||
| 329 | * |
||
| 330 | * @return CalendarEvent_Controller |
||
| 331 | */ |
||
| 332 | public function getController() |
||
| 338 | } |
||
| 339 |
This check marks private properties in classes that are never used. Those properties can be removed.