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 | public function updateCMSFields(FieldList $fields) |
||
| 73 | { |
||
| 74 | $ticketLabel = _t('TicketExtension.Tickets', 'Tickets'); |
||
| 75 | $fields->addFieldsToTab( |
||
| 76 | "Root.$ticketLabel", array( |
||
| 77 | GridField::create('Tickets', $ticketLabel, $this->owner->Tickets(), TicketsGridFieldConfig::create($this->canCreateTickets())), |
||
|
1 ignored issue
–
show
|
|||
| 78 | NumericField::create('Capacity', _t('TicketExtension.Capacity', 'Capacity')), |
||
| 79 | HtmlEditorField::create('SuccessMessage', _t('TicketExtension.SuccessMessage', 'Success message'))->setRows(4), |
||
| 80 | HtmlEditorField::create('SuccessMessageMail', _t('TicketExtension.MailMessage', 'Mail message'))->setRows(4) |
||
| 81 | )); |
||
| 82 | |||
| 83 | // Create Reservations tab |
||
| 84 | if ($this->owner->Reservations()->exists()) { |
||
| 85 | $reservationLabel = _t('TicketExtension.Reservations', 'Reservations'); |
||
| 86 | $fields->addFieldToTab( |
||
| 87 | "Root.$reservationLabel", |
||
| 88 | GridField::create('Reservations', $reservationLabel, $this->owner->Reservations(), GridFieldConfig_RecordEditor::create()) |
||
|
1 ignored issue
–
show
|
|||
| 89 | ); |
||
| 90 | } |
||
| 91 | |||
| 92 | // Create Attendees tab |
||
| 93 | View Code Duplication | if ($this->owner->Attendees()->exists()) { |
|
|
1 ignored issue
–
show
|
|||
| 94 | $guestListLabel = _t('TicketExtension.GuestList', 'GuestList'); |
||
| 95 | $fields->addFieldToTab( |
||
| 96 | "Root.$guestListLabel", |
||
| 97 | GridField::create('Attendees', $guestListLabel, $this->owner->Attendees(), GuestListGridFieldConfig::create($this->owner)) |
||
|
1 ignored issue
–
show
|
|||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | // Create WaitingList tab |
||
| 102 | View Code Duplication | if ($this->owner->WaitingList()->exists()) { |
|
|
1 ignored issue
–
show
|
|||
| 103 | $waitingListLabel = _t('TicketExtension.WaitingList', 'WaitingList'); |
||
| 104 | $fields->addFieldToTab( |
||
| 105 | "Root.$waitingListLabel", |
||
| 106 | GridField::create('WaitingList', $waitingListLabel, $this->owner->WaitingList(), GridFieldConfig_RecordEditor::create()) |
||
|
1 ignored issue
–
show
|
|||
| 107 | ); |
||
| 108 | } |
||
| 109 | |||
| 110 | // Create Fields tab |
||
| 111 | $extraFieldsLabel = _t('TicketExtension.ExtraFields', 'Attendee fields'); |
||
| 112 | $fields->addFieldToTab( |
||
| 113 | "Root.$extraFieldsLabel", |
||
| 114 | GridField::create('ExtraFields', $extraFieldsLabel, $this->owner->Fields(), UserFieldsGridFieldConfig::create()) |
||
|
1 ignored issue
–
show
|
|||
| 115 | ); |
||
| 116 | |||
| 117 | $this->owner->extend('updateTicketExtensionFields', $fields); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Trigger actions after write |
||
| 122 | */ |
||
| 123 | public function onAfterWrite() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Creates and sets up the default fields |
||
| 131 | */ |
||
| 132 | public function createDefaultFields() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Extend the page actions with an start check in action |
||
| 145 | * |
||
| 146 | * @param FieldList $actions |
||
| 147 | */ |
||
| 148 | public function updateCMSActions(FieldList $actions) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the leftover capacity |
||
| 167 | * |
||
| 168 | * @return int |
||
| 169 | */ |
||
| 170 | public function getAvailability() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Check if the tickets are still available |
||
| 177 | * |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function getTicketsAvailable() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Check if the tickets are sold out |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | public function getTicketsSoldOut() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * get The sale start date |
||
| 196 | * |
||
| 197 | * @return \SS_DateTime |
||
| 198 | */ |
||
| 199 | public function getTicketSaleStartDate() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Check if the event is expired, either by unavailable tickets or because the date has passed |
||
| 216 | * |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | public function getEventExpired() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Check if the ticket sale is still pending |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | public function getTicketSalePending() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get only the attendees who are certain to attend |
||
| 244 | * Fixme: Using callback filter instead of join, because join gives ambiguous error on 'EventID' |
||
| 245 | * |
||
| 246 | * @return \ArrayList |
||
| 247 | */ |
||
| 248 | public function getGuestList() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get the success message |
||
| 257 | * |
||
| 258 | * @return mixed|string |
||
| 259 | */ |
||
| 260 | View Code Duplication | public function getSuccessContent() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Get the mail message |
||
| 271 | * |
||
| 272 | * @return mixed|string |
||
| 273 | */ |
||
| 274 | View Code Duplication | public function getMailContent() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Get the Ticket logo |
||
| 285 | * |
||
| 286 | * @return \Image |
||
| 287 | */ |
||
| 288 | public function getMailLogo() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Check if the current event can have tickets |
||
| 295 | * |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | public function canCreateTickets() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get the calendar controller |
||
| 310 | * |
||
| 311 | * @return CalendarEvent_Controller |
||
| 312 | */ |
||
| 313 | public function getController() |
||
| 319 | } |
||
| 320 |
This check marks private properties in classes that are never used. Those properties can be removed.