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  | 
            ||
| 58 | class Attendee extends DataObject  | 
            ||
| 59 | { | 
            ||
| 60 | /**  | 
            ||
| 61 | * Set this to true when you want to have a QR code that opens the check in page and validates the code.  | 
            ||
| 62 | * The validation is only done with proper authorisation so guest cannot check themselves in by mistake.  | 
            ||
| 63 | * By default only the ticket number is translated to an QR code. (for use with USB QR scanners)  | 
            ||
| 64 | *  | 
            ||
| 65 | * @var bool  | 
            ||
| 66 | */  | 
            ||
| 67 | private static $qr_as_link = false;  | 
            ||
| 68 | |||
| 69 | private static $default_fields = array(  | 
            ||
| 70 | 'FirstName' => array(  | 
            ||
| 71 | 'Title' => 'First name',  | 
            ||
| 72 | 'FieldType' => 'UserTextField',  | 
            ||
| 73 | 'Required' => true,  | 
            ||
| 74 | 'Editable' => false  | 
            ||
| 75 | ),  | 
            ||
| 76 | 'Surname' => array(  | 
            ||
| 77 | 'Title' => 'Surname',  | 
            ||
| 78 | 'FieldType' => 'UserTextField',  | 
            ||
| 79 | 'Required' => true,  | 
            ||
| 80 | 'Editable' => false  | 
            ||
| 81 | ),  | 
            ||
| 82 | 'Email' => array(  | 
            ||
| 83 | 'Title' => 'Email',  | 
            ||
| 84 | 'FieldType' => 'UserEmailField',  | 
            ||
| 85 | 'Required' => true,  | 
            ||
| 86 | 'Editable' => false  | 
            ||
| 87 | )  | 
            ||
| 88 | );  | 
            ||
| 89 | |||
| 90 | private static $table_fields = array(  | 
            ||
| 91 | 'Title',  | 
            ||
| 92 | 'Email'  | 
            ||
| 93 | );  | 
            ||
| 94 | |||
| 95 | private static $db = array(  | 
            ||
| 96 | 'Title' => 'Varchar(255)',  | 
            ||
| 97 | 'TicketReceiver' => 'Boolean',  | 
            ||
| 98 | 'TicketCode' => 'Varchar(255)',  | 
            ||
| 99 | 'CheckedIn' => 'Boolean'  | 
            ||
| 100 | );  | 
            ||
| 101 | |||
| 102 | private static $default_sort = 'Created DESC';  | 
            ||
| 103 | |||
| 104 | private static $indexes = array(  | 
            ||
| 105 |         'TicketCode' => 'unique("TicketCode")' | 
            ||
| 106 | );  | 
            ||
| 107 | |||
| 108 | private static $has_one = array(  | 
            ||
| 109 | 'Reservation' => 'Broarm\EventTickets\Reservation',  | 
            ||
| 110 | 'Ticket' => 'Broarm\EventTickets\Ticket',  | 
            ||
| 111 | 'Event' => 'CalendarEvent',  | 
            ||
| 112 | 'Member' => 'Member',  | 
            ||
| 113 | 'TicketQRCode' => 'Image',  | 
            ||
| 114 | 'TicketFile' => 'File'  | 
            ||
| 115 | );  | 
            ||
| 116 | |||
| 117 | private static $many_many = array(  | 
            ||
| 118 | 'Fields' => 'Broarm\EventTickets\UserField'  | 
            ||
| 119 | );  | 
            ||
| 120 | |||
| 121 | private static $many_many_extraFields = array(  | 
            ||
| 122 | 'Fields' => array(  | 
            ||
| 123 | 'Value' => 'Varchar(255)'  | 
            ||
| 124 | )  | 
            ||
| 125 | );  | 
            ||
| 126 | |||
| 127 | private static $summary_fields = array(  | 
            ||
| 128 | 'Title' => 'Name',  | 
            ||
| 129 | 'Ticket.Title' => 'Ticket',  | 
            ||
| 130 | 'TicketCode' => 'Ticket #',  | 
            ||
| 131 | 'CheckedIn.Nice' => 'Checked in',  | 
            ||
| 132 | );  | 
            ||
| 133 | |||
| 134 | /**  | 
            ||
| 135 | * Actions usable on the cms detail view  | 
            ||
| 136 | *  | 
            ||
| 137 | * @var array  | 
            ||
| 138 | */  | 
            ||
| 139 | private static $better_buttons_actions = array(  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 140 | 'sendTicket',  | 
            ||
| 141 | 'createTicketFile'  | 
            ||
| 142 | );  | 
            ||
| 143 | |||
| 144 | public function getCMSFields()  | 
            ||
| 173 | |||
| 174 | /**  | 
            ||
| 175 | * Add utility actions to the attendee details view  | 
            ||
| 176 | *  | 
            ||
| 177 | * @return FieldList  | 
            ||
| 178 | */  | 
            ||
| 179 | public function getBetterButtonsActions()  | 
            ||
| 193 | |||
| 194 | /**  | 
            ||
| 195 | * Attendee constructor  | 
            ||
| 196 | * If the fields don't exist yet add them  | 
            ||
| 197 | * If they do populate the record with the set data  | 
            ||
| 198 | *  | 
            ||
| 199 | * @param null $record  | 
            ||
| 200 | * @param bool $isSingleton  | 
            ||
| 201 | * @param null $model  | 
            ||
| 202 | */  | 
            ||
| 203 | public function __construct($record = null, $isSingleton = false, $model = null)  | 
            ||
| 220 | |||
| 221 | /**  | 
            ||
| 222 | * Set the title and ticket code before writing  | 
            ||
| 223 | */  | 
            ||
| 224 | public function onBeforeWrite()  | 
            ||
| 254 | |||
| 255 | /**  | 
            ||
| 256 | * Delete any stray files before deleting the object  | 
            ||
| 257 | */  | 
            ||
| 258 | public function onBeforeDelete()  | 
            ||
| 273 | |||
| 274 | /**  | 
            ||
| 275 | * Create the folder for the qr code and ticket file  | 
            ||
| 276 | *  | 
            ||
| 277 | * @return Folder|DataObject|null  | 
            ||
| 278 | */  | 
            ||
| 279 | public function fileFolder()  | 
            ||
| 283 | |||
| 284 | /**  | 
            ||
| 285 | * todo Extend the get field property to lookup data from the User field set  | 
            ||
| 286 | *  | 
            ||
| 287 | * @param string $field  | 
            ||
| 288 | *  | 
            ||
| 289 | * @return mixed|string  | 
            ||
| 290 | * /  | 
            ||
| 291 | public function getField($field)  | 
            ||
| 292 |     {} */ | 
            ||
| 293 | |||
| 294 | |||
| 295 | /**  | 
            ||
| 296 | * Utility method for fetching the default field, FirstName, value  | 
            ||
| 297 | *  | 
            ||
| 298 | * @return string|null  | 
            ||
| 299 | */  | 
            ||
| 300 | public function getFirstName()  | 
            ||
| 308 | |||
| 309 | /**  | 
            ||
| 310 | * Utility method for fetching the default field, Surname, value  | 
            ||
| 311 | *  | 
            ||
| 312 | * @return string|null  | 
            ||
| 313 | */  | 
            ||
| 314 | public function getSurname()  | 
            ||
| 322 | |||
| 323 | /**  | 
            ||
| 324 | * Get the combined first and last nave for display on the ticket and attendee list  | 
            ||
| 325 | *  | 
            ||
| 326 | * @return string|null  | 
            ||
| 327 | */  | 
            ||
| 328 | public function getName()  | 
            ||
| 339 | |||
| 340 | /**  | 
            ||
| 341 | * Utility method for fetching the default field, Email, value  | 
            ||
| 342 | *  | 
            ||
| 343 | * @return string|null  | 
            ||
| 344 | */  | 
            ||
| 345 | public function getEmail()  | 
            ||
| 353 | |||
| 354 | /**  | 
            ||
| 355 | * Get the table fields for this attendee  | 
            ||
| 356 | *  | 
            ||
| 357 | * @return ArrayList  | 
            ||
| 358 | */  | 
            ||
| 359 | public function getTableFields()  | 
            ||
| 370 | |||
| 371 | /**  | 
            ||
| 372 | * Get the unnamespaced singular name for display in the CMS  | 
            ||
| 373 | *  | 
            ||
| 374 | * @return string  | 
            ||
| 375 | */  | 
            ||
| 376 | public function singular_name()  | 
            ||
| 381 | |||
| 382 | /**  | 
            ||
| 383 | * Generate a unique ticket id  | 
            ||
| 384 | * Serves as the base for the QR code and ticket file  | 
            ||
| 385 | *  | 
            ||
| 386 | * @return string  | 
            ||
| 387 | */  | 
            ||
| 388 | public function generateTicketCode()  | 
            ||
| 392 | |||
| 393 | /**  | 
            ||
| 394 | * Create a QRCode for the attendee based on the Ticket code  | 
            ||
| 395 | *  | 
            ||
| 396 | * @return Image  | 
            ||
| 397 | */  | 
            ||
| 398 | public function createQRCode()  | 
            ||
| 431 | |||
| 432 | /**  | 
            ||
| 433 | * Creates a printable ticket for the attendee  | 
            ||
| 434 | *  | 
            ||
| 435 | * @return File  | 
            ||
| 436 | */  | 
            ||
| 437 | public function createTicketFile()  | 
            ||
| 473 | |||
| 474 | /**  | 
            ||
| 475 | * Send the attendee ticket  | 
            ||
| 476 | */  | 
            ||
| 477 | View Code Duplication | public function sendTicket()  | 
            |
| 500 | |||
| 501 | /**  | 
            ||
| 502 | * Get the checkin link  | 
            ||
| 503 | *  | 
            ||
| 504 | * @return string  | 
            ||
| 505 | */  | 
            ||
| 506 | public function getCheckInLink()  | 
            ||
| 510 | |||
| 511 | /**  | 
            ||
| 512 | * Check the attendee out  | 
            ||
| 513 | */  | 
            ||
| 514 | public function checkIn()  | 
            ||
| 519 | |||
| 520 | public function canCheckOut()  | 
            ||
| 524 | |||
| 525 | /**  | 
            ||
| 526 | * Check the attendee in  | 
            ||
| 527 | */  | 
            ||
| 528 | public function checkOut()  | 
            ||
| 535 | |||
| 536 | public function canView($member = null)  | 
            ||
| 540 | |||
| 541 | public function canEdit($member = null)  | 
            ||
| 545 | |||
| 546 | public function canDelete($member = null)  | 
            ||
| 550 | |||
| 551 | public function canCreate($member = null)  | 
            ||
| 555 | }  | 
            ||
| 556 | 
This check marks private properties in classes that are never used. Those properties can be removed.