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 |
||
| 44 | class TicketExtension extends DataExtension |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @var CalendarEvent_Controller |
||
| 48 | */ |
||
| 49 | protected $controller; |
||
| 50 | |||
| 51 | private static $db = array( |
||
|
|
|||
| 52 | 'Capacity' => 'Int', |
||
| 53 | 'OrderMin' => 'Int', |
||
| 54 | 'OrderMax' => 'Int', |
||
| 55 | 'SuccessMessage' => 'HTMLText', |
||
| 56 | 'SuccessMessageMail' => 'HTMLText' |
||
| 57 | ); |
||
| 58 | |||
| 59 | private static $has_many = array( |
||
| 60 | 'Tickets' => 'Broarm\EventTickets\Ticket.Event', |
||
| 61 | 'Reservations' => 'Broarm\EventTickets\Reservation.Event', |
||
| 62 | 'Attendees' => 'Broarm\EventTickets\Attendee.Event', |
||
| 63 | 'WaitingList' => 'Broarm\EventTickets\WaitingListRegistration.Event', |
||
| 64 | 'Fields' => 'Broarm\EventTickets\UserField.Event' |
||
| 65 | ); |
||
| 66 | |||
| 67 | private static $defaults = array( |
||
| 68 | 'Capacity' => 50, |
||
| 69 | 'OrderMin' => 1, |
||
| 70 | 'OrderMax' => 5 |
||
| 71 | ); |
||
| 72 | |||
| 73 | private static $translate = array( |
||
| 74 | 'SuccessMessage', |
||
| 75 | 'SuccessMessageMail' |
||
| 76 | ); |
||
| 77 | |||
| 78 | public function updateCMSFields(FieldList $fields) |
||
| 79 | { |
||
| 80 | $ticketLabel = _t('TicketExtension.Tickets', 'Tickets'); |
||
| 81 | $fields->addFieldsToTab( |
||
| 82 | "Root.$ticketLabel", array( |
||
| 83 | GridField::create('Tickets', $ticketLabel, $this->owner->Tickets(), TicketsGridFieldConfig::create($this->canCreateTickets())), |
||
|
1 ignored issue
–
show
|
|||
| 84 | NumericField::create('Capacity', _t('TicketExtension.Capacity', 'Capacity')), |
||
| 85 | NumericField::create('OrderMin', _t('TicketExtension.OrderMin', 'Minimum amount of tickets required per reservation')), |
||
| 86 | NumericField::create('OrderMax', _t('TicketExtension.OrderMax', 'Maximum amount of tickets allowed per reservation')), |
||
| 87 | HtmlEditorField::create('SuccessMessage', _t('TicketExtension.SuccessMessage', 'Success message'))->setRows(4), |
||
| 88 | HtmlEditorField::create('SuccessMessageMail', _t('TicketExtension.MailMessage', 'Mail message'))->setRows(4) |
||
| 89 | )); |
||
| 90 | |||
| 91 | // Create Reservations tab |
||
| 92 | if ($this->owner->Reservations()->exists()) { |
||
| 93 | $reservationLabel = _t('TicketExtension.Reservations', 'Reservations'); |
||
| 94 | $fields->addFieldToTab( |
||
| 95 | "Root.$reservationLabel", |
||
| 96 | GridField::create('Reservations', $reservationLabel, $this->owner->Reservations(), GridFieldConfig_RecordEditor::create()) |
||
|
1 ignored issue
–
show
|
|||
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | // Create Attendees tab |
||
| 101 | View Code Duplication | if ($this->owner->Attendees()->exists()) { |
|
|
1 ignored issue
–
show
|
|||
| 102 | $guestListLabel = _t('TicketExtension.GuestList', 'GuestList'); |
||
| 103 | $fields->addFieldToTab( |
||
| 104 | "Root.$guestListLabel", |
||
| 105 | GridField::create('Attendees', $guestListLabel, $this->owner->Attendees(), GuestListGridFieldConfig::create($this->owner)) |
||
|
1 ignored issue
–
show
|
|||
| 106 | ); |
||
| 107 | } |
||
| 108 | |||
| 109 | // Create WaitingList tab |
||
| 110 | View Code Duplication | if ($this->owner->WaitingList()->exists()) { |
|
|
1 ignored issue
–
show
|
|||
| 111 | $waitingListLabel = _t('TicketExtension.WaitingList', 'WaitingList'); |
||
| 112 | $fields->addFieldToTab( |
||
| 113 | "Root.$waitingListLabel", |
||
| 114 | GridField::create('WaitingList', $waitingListLabel, $this->owner->WaitingList(), GridFieldConfig_RecordEditor::create()) |
||
|
1 ignored issue
–
show
|
|||
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 118 | // Create Fields tab |
||
| 119 | $extraFieldsLabel = _t('TicketExtension.ExtraFields', 'Attendee fields'); |
||
| 120 | $fields->addFieldToTab( |
||
| 121 | "Root.$extraFieldsLabel", |
||
| 122 | GridField::create('ExtraFields', $extraFieldsLabel, $this->owner->Fields(), UserFieldsGridFieldConfig::create()) |
||
|
1 ignored issue
–
show
|
|||
| 123 | ); |
||
| 124 | |||
| 125 | $this->owner->extend('updateTicketExtensionFields', $fields); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Trigger actions after write |
||
| 130 | */ |
||
| 131 | public function onAfterWrite() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Creates and sets up the default fields |
||
| 139 | */ |
||
| 140 | public function createDefaultFields() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Extend the page actions with an start check in action |
||
| 153 | * |
||
| 154 | * @param FieldList $actions |
||
| 155 | */ |
||
| 156 | public function updateCMSActions(FieldList $actions) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the leftover capacity |
||
| 175 | * |
||
| 176 | * @return int |
||
| 177 | */ |
||
| 178 | public function getAvailability() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Check if the event is expired, either by unavailable tickets or because the date has passed |
||
| 185 | * |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function getEventExpired() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get only the attendees who are certain to attend |
||
| 200 | * Fixme: Using callback filter instead of join, because join gives ambiguous error on 'EventID' |
||
| 201 | * |
||
| 202 | * @return \ArrayList |
||
| 203 | */ |
||
| 204 | public function getGuestList() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the success message |
||
| 213 | * |
||
| 214 | * @return mixed|string |
||
| 215 | */ |
||
| 216 | View Code Duplication | public function getSuccessContent() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Get the mail message |
||
| 227 | * |
||
| 228 | * @return mixed|string |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function getMailContent() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Get the Ticket logo |
||
| 241 | * |
||
| 242 | * @return \Image |
||
| 243 | */ |
||
| 244 | public function getMailLogo() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Check if the current event can have tickets |
||
| 251 | * |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | public function canCreateTickets() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get the calendar controller |
||
| 266 | * |
||
| 267 | * @return CalendarEvent_Controller |
||
| 268 | */ |
||
| 269 | public function getController() |
||
| 275 | } |
||
| 276 |
This check marks private properties in classes that are never used. Those properties can be removed.