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 | * @return mixed |
||
480 | */ |
||
481 | public function sendTicket() |
||
482 | { |
||
483 | // Get the mail sender or fallback to the admin email |
||
484 | if (empty($from = Reservation::config()->get('mail_sender'))) { |
||
485 | $from = Config::inst()->get('Email', 'admin_email'); |
||
486 | } |
||
487 | |||
488 | $email = new Email(); |
||
489 | $email->setSubject(_t( |
||
490 | 'AttendeeMail.TITLE', |
||
491 | 'Your ticket for {event}', |
||
492 | null, |
||
493 | array( |
||
494 | 'event' => $this->Event()->Title |
||
495 | ) |
||
496 | )); |
||
497 | $email->setFrom($from); |
||
498 | $email->setTo($this->getEmail()); |
||
499 | $email->setTemplate('AttendeeMail'); |
||
500 | $email->populateTemplate($this); |
||
501 | $this->extend('updateTicketMail', $email); |
||
502 | return $email->send(); |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * Get the checkin link |
||
507 | * |
||
508 | * @return string |
||
509 | */ |
||
510 | public function getCheckInLink() |
||
514 | |||
515 | /** |
||
516 | * Check the attendee out |
||
517 | */ |
||
518 | public function checkIn() |
||
523 | |||
524 | public function canCheckOut() |
||
528 | |||
529 | /** |
||
530 | * Check the attendee in |
||
531 | */ |
||
532 | public function checkOut() |
||
539 | |||
540 | public function canView($member = null) |
||
544 | |||
545 | public function canEdit($member = null) |
||
549 | |||
550 | public function canDelete($member = null) |
||
554 | |||
555 | public function canCreate($member = null) |
||
559 | } |
||
560 |
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: