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 Attendee 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 Attendee, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 59 | class Attendee extends DataObject |
||
| 60 | { |
||
| 61 | /** |
||
| 62 | * Set this to true when you want to have a QR code that opens the check in page and validates the code. |
||
| 63 | * The validation is only done with proper authorisation so guest cannot check themselves in by mistake. |
||
| 64 | * By default only the ticket number is translated to an QR code. (for use with USB QR scanners) |
||
| 65 | * |
||
| 66 | * @var bool |
||
| 67 | */ |
||
| 68 | private static $qr_as_link = false; |
||
| 69 | |||
| 70 | private static $default_fields = array( |
||
| 71 | 'FirstName' => array( |
||
| 72 | 'Title' => 'First name', |
||
| 73 | 'FieldType' => 'UserTextField', |
||
| 74 | 'Required' => true, |
||
| 75 | 'Editable' => false |
||
| 76 | ), |
||
| 77 | 'Surname' => array( |
||
| 78 | 'Title' => 'Surname', |
||
| 79 | 'FieldType' => 'UserTextField', |
||
| 80 | 'Required' => true, |
||
| 81 | 'Editable' => false |
||
| 82 | ), |
||
| 83 | 'Email' => array( |
||
| 84 | 'Title' => 'Email', |
||
| 85 | 'FieldType' => 'UserEmailField', |
||
| 86 | 'Required' => true, |
||
| 87 | 'Editable' => false |
||
| 88 | ) |
||
| 89 | ); |
||
| 90 | |||
| 91 | private static $table_fields = array( |
||
| 92 | 'Title', |
||
| 93 | 'Email' |
||
| 94 | ); |
||
| 95 | |||
| 96 | private static $db = array( |
||
| 97 | 'Title' => 'Varchar(255)', |
||
| 98 | 'TicketReceiver' => 'Boolean', |
||
| 99 | 'TicketCode' => 'Varchar(255)', |
||
| 100 | 'CheckedIn' => 'Boolean' |
||
| 101 | ); |
||
| 102 | |||
| 103 | private static $default_sort = 'Created DESC'; |
||
| 104 | |||
| 105 | private static $indexes = array( |
||
| 106 | 'TicketCode' => 'unique("TicketCode")' |
||
| 107 | ); |
||
| 108 | |||
| 109 | private static $has_one = array( |
||
| 110 | 'Reservation' => 'Broarm\EventTickets\Reservation', |
||
| 111 | 'Ticket' => 'Broarm\EventTickets\Ticket', |
||
| 112 | 'Event' => 'CalendarEvent', |
||
| 113 | 'Member' => 'Member', |
||
| 114 | 'TicketQRCode' => 'Image', |
||
| 115 | 'TicketFile' => 'File' |
||
| 116 | ); |
||
| 117 | |||
| 118 | private static $many_many = array( |
||
| 119 | 'Fields' => 'Broarm\EventTickets\UserField' |
||
| 120 | ); |
||
| 121 | |||
| 122 | private static $many_many_extraFields = array( |
||
| 123 | 'Fields' => array( |
||
| 124 | 'Value' => 'Varchar(255)' |
||
| 125 | ) |
||
| 126 | ); |
||
| 127 | |||
| 128 | private static $summary_fields = array( |
||
| 129 | 'Title' => 'Name', |
||
| 130 | 'Ticket.Title' => 'Ticket', |
||
| 131 | 'TicketCode' => 'Ticket #', |
||
| 132 | 'CheckedIn.Nice' => 'Checked in', |
||
| 133 | ); |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Actions usable on the cms detail view |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | private static $better_buttons_actions = array( |
||
| 141 | 'sendTicket', |
||
| 142 | 'createTicketFile' |
||
| 143 | ); |
||
| 144 | |||
| 145 | protected static $cachedFields = array(); |
||
| 146 | |||
| 147 | public function getCMSFields() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Add utility actions to the attendee details view |
||
| 179 | * |
||
| 180 | * @return FieldList |
||
| 181 | */ |
||
| 182 | public function getBetterButtonsActions() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Attendee constructor |
||
| 199 | * If the fields don't exist yet add them |
||
| 200 | * If they do populate the record with the set data |
||
| 201 | * |
||
| 202 | * @param null $record |
||
| 203 | * @param bool $isSingleton |
||
| 204 | * @param null $model |
||
| 205 | */ |
||
| 206 | public function __construct($record = null, $isSingleton = false, $model = null) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Set the title and ticket code before writing |
||
| 226 | */ |
||
| 227 | public function onBeforeWrite() |
||
| 259 | |||
| 260 | public function onAfterWrite() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Delete any stray files before deleting the object |
||
| 270 | */ |
||
| 271 | public function onBeforeDelete() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Create the folder for the qr code and ticket file |
||
| 293 | * |
||
| 294 | * @return Folder|DataObject|null |
||
| 295 | */ |
||
| 296 | public function fileFolder() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Utility method for fetching the default field, FirstName, value |
||
| 303 | * |
||
| 304 | * @return string|null |
||
| 305 | */ |
||
| 306 | public function getFirstName() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Utility method for fetching the default field, Surname, value |
||
| 313 | * |
||
| 314 | * @return string|null |
||
| 315 | */ |
||
| 316 | public function getSurname() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Utility method for fetching the default field, Email, value |
||
| 323 | * |
||
| 324 | * @return string|null |
||
| 325 | */ |
||
| 326 | public function getEmail() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get the combined first and last nave for display on the ticket and attendee list |
||
| 333 | * |
||
| 334 | * @return string|null |
||
| 335 | */ |
||
| 336 | public function getName() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get the user field and store it in a static cache |
||
| 350 | * todo: add a cache that saves the field value on save and retrieves the values here, dumb, so empty fields don't trigger queries |
||
| 351 | * |
||
| 352 | * @param $field |
||
| 353 | * @return mixed|null|string |
||
| 354 | */ |
||
| 355 | public function getUserField($field) |
||
| 368 | |||
| 369 | protected static function getCacheFactory() |
||
| 373 | |||
| 374 | protected function getFieldCacheKey($field) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get the table fields for this attendee |
||
| 381 | * |
||
| 382 | * @return ArrayList |
||
| 383 | */ |
||
| 384 | public function getTableFields() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get the unnamespaced singular name for display in the CMS |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | public function singular_name() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Generate a unique ticket id |
||
| 409 | * Serves as the base for the QR code and ticket file |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function generateTicketCode() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Create a QRCode for the attendee based on the Ticket code |
||
| 420 | * |
||
| 421 | * @return Image |
||
| 422 | */ |
||
| 423 | public function createQRCode() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Creates a printable ticket for the attendee |
||
| 459 | * |
||
| 460 | * @return File |
||
| 461 | */ |
||
| 462 | public function createTicketFile() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Send the attendee ticket |
||
| 501 | */ |
||
| 502 | View Code Duplication | public function sendTicket() |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Get the checkin link |
||
| 528 | * |
||
| 529 | * @return string |
||
| 530 | */ |
||
| 531 | public function getCheckInLink() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Check the attendee out |
||
| 538 | */ |
||
| 539 | public function checkIn() |
||
| 544 | |||
| 545 | public function canCheckOut() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Check the attendee in |
||
| 552 | */ |
||
| 553 | public function checkOut() |
||
| 560 | |||
| 561 | public function canView($member = null) |
||
| 565 | |||
| 566 | public function canEdit($member = null) |
||
| 570 | |||
| 571 | public function canDelete($member = null) |
||
| 575 | |||
| 576 | public function canCreate($member = null) |
||
| 580 | } |
||
| 581 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.