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 | * Set the title and ticket code before writing |
||
| 199 | */ |
||
| 200 | public function onBeforeWrite() |
||
| 232 | |||
| 233 | public function onAfterWrite() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Delete any stray files before deleting the object |
||
| 243 | */ |
||
| 244 | public function onBeforeDelete() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Create the folder for the qr code and ticket file |
||
| 266 | * |
||
| 267 | * @return Folder|DataObject|null |
||
| 268 | */ |
||
| 269 | public function fileFolder() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Utility method for fetching the default field, FirstName, value |
||
| 276 | * |
||
| 277 | * @return string|null |
||
| 278 | */ |
||
| 279 | public function getFirstName() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Utility method for fetching the default field, Surname, value |
||
| 286 | * |
||
| 287 | * @return string|null |
||
| 288 | */ |
||
| 289 | public function getSurname() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Utility method for fetching the default field, Email, value |
||
| 296 | * |
||
| 297 | * @return string|null |
||
| 298 | */ |
||
| 299 | public function getEmail() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get the combined first and last nave for display on the ticket and attendee list |
||
| 306 | * |
||
| 307 | * @return string|null |
||
| 308 | */ |
||
| 309 | public function getName() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get the user field and store it in a static cache |
||
| 323 | * todo: add a cache that saves the field value on save and retrieves the values here, dumb, so empty fields don't trigger queries |
||
| 324 | * |
||
| 325 | * @param $field |
||
| 326 | * @return mixed|null|string |
||
| 327 | */ |
||
| 328 | public function getUserField($field) |
||
| 341 | |||
| 342 | protected static function getCacheFactory() |
||
| 346 | |||
| 347 | protected function getFieldCacheKey($field) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get the table fields for this attendee |
||
| 354 | * |
||
| 355 | * @return ArrayList |
||
| 356 | */ |
||
| 357 | public function getTableFields() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get the unnamespaced singular name for display in the CMS |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | public function singular_name() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Generate a unique ticket id |
||
| 382 | * Serves as the base for the QR code and ticket file |
||
| 383 | * |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | public function generateTicketCode() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Create a QRCode for the attendee based on the Ticket code |
||
| 393 | * |
||
| 394 | * @return Image |
||
| 395 | */ |
||
| 396 | public function createQRCode() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Creates a printable ticket for the attendee |
||
| 432 | * |
||
| 433 | * @return File |
||
| 434 | */ |
||
| 435 | public function createTicketFile() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Send the attendee ticket |
||
| 478 | */ |
||
| 479 | View Code Duplication | public function sendTicket() |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Get the checkin link |
||
| 505 | * |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | public function getCheckInLink() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Check the attendee out |
||
| 515 | */ |
||
| 516 | public function checkIn() |
||
| 521 | |||
| 522 | public function canCheckOut() |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Check the attendee in |
||
| 529 | */ |
||
| 530 | public function checkOut() |
||
| 537 | |||
| 538 | public function canView($member = null) |
||
| 542 | |||
| 543 | public function canEdit($member = null) |
||
| 547 | |||
| 548 | public function canDelete($member = null) |
||
| 552 | |||
| 553 | public function canCreate($member = null) |
||
| 557 | } |
||
| 558 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: