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 |
||
| 36 | class TicketExtension extends DataExtension |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var CalendarEvent_Controller |
||
| 40 | */ |
||
| 41 | protected $controller; |
||
| 42 | |||
| 43 | private static $db = array( |
||
|
|
|||
| 44 | 'Capacity' => 'Int', |
||
| 45 | 'SuccessMessage' => 'HTMLText', |
||
| 46 | 'SuccessMessageMail' => 'HTMLText' |
||
| 47 | ); |
||
| 48 | |||
| 49 | private static $has_many = array( |
||
| 50 | 'Tickets' => 'Broarm\EventTickets\Ticket.Event', |
||
| 51 | 'Reservations' => 'Broarm\EventTickets\Reservation.Event', |
||
| 52 | 'Attendees' => 'Broarm\EventTickets\Attendee.Event' |
||
| 53 | ); |
||
| 54 | |||
| 55 | public function updateCMSFields(FieldList $fields) |
||
| 56 | { |
||
| 57 | $gridFieldConfig = GridFieldConfig_RecordEditor::create(); |
||
| 58 | |||
| 59 | // If the event dates are in the past remove ability to create new tickets, reservations and attendees. |
||
| 60 | // This is done here instead of in the canCreate method because of the lack of context there. |
||
| 61 | if (!$this->canCreateTickets()) { |
||
| 62 | $gridFieldConfig->removeComponentsByType(new GridFieldAddNewButton()); |
||
| 63 | } |
||
| 64 | |||
| 65 | $ticketLabel = _t('TicketExtension.Tickets', 'Tickets'); |
||
| 66 | $fields->addFieldsToTab( |
||
| 67 | "Root.$ticketLabel", array( |
||
| 68 | GridField::create('Tickets', $ticketLabel, $this->owner->Tickets(), $gridFieldConfig), |
||
| 69 | NumericField::create('Capacity', _t('TicketExtension.Capacity', 'Capacity')), |
||
| 70 | HtmlEditorField::create('SuccessMessage', 'Success message')->setRows(4), |
||
| 71 | HtmlEditorField::create('SuccessMessageMail', 'Mail message')->setRows(4) |
||
| 72 | )); |
||
| 73 | |||
| 74 | if ($this->owner->Reservations()->exists()) { |
||
| 75 | $reservationLabel = _t('TicketExtension.Reservations', 'Reservations'); |
||
| 76 | $fields->addFieldToTab( |
||
| 77 | "Root.$reservationLabel", |
||
| 78 | GridField::create('Reservations', $reservationLabel, $this->owner->Reservations(), $gridFieldConfig) |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($this->owner->Attendees()->exists()) { |
||
| 83 | $guestListLabel = _t('TicketExtension.GuestList', 'GuestList'); |
||
| 84 | $fields->addFieldToTab( |
||
| 85 | "Root.$guestListLabel", |
||
| 86 | GridField::create('Attendees', $guestListLabel, $this->owner->Attendees(), $gridFieldConfig) |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | |||
| 90 | return $fields; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Extend the page actions with an start check in action |
||
| 95 | * |
||
| 96 | * @param FieldList $actions |
||
| 97 | */ |
||
| 98 | public function updateCMSActions(FieldList $actions) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get the leftover capacity |
||
| 117 | * |
||
| 118 | * @return int |
||
| 119 | */ |
||
| 120 | public function getAvailability() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get the success message |
||
| 127 | * |
||
| 128 | * @return mixed|string |
||
| 129 | */ |
||
| 130 | View Code Duplication | public function getSuccessContent() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Get the mail message |
||
| 141 | * |
||
| 142 | * @return mixed|string |
||
| 143 | */ |
||
| 144 | View Code Duplication | public function getMailContent() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Get the Ticket logo |
||
| 155 | * |
||
| 156 | * @return \Image |
||
| 157 | */ |
||
| 158 | public function getMailLogo() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Check if the current event can have tickets |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function canCreateTickets() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the calendar controller |
||
| 180 | * |
||
| 181 | * @return CalendarEvent_Controller |
||
| 182 | */ |
||
| 183 | public function getController() |
||
| 189 | } |
||
| 190 |
This check marks private properties in classes that are never used. Those properties can be removed.