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 EspressoEventAttendees 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 EspressoEventAttendees, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class EspressoEventAttendees extends EspressoShortcode |
||
| 31 | { |
||
| 32 | |||
| 33 | private $query_params = array( |
||
| 34 | 0 => array() |
||
| 35 | ); |
||
| 36 | |||
| 37 | private $template_args = array( |
||
| 38 | 'contacts' => array(), |
||
| 39 | 'event' => null, |
||
| 40 | 'datetime' => null, |
||
| 41 | 'ticket' => null, |
||
| 42 | ); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * the actual shortcode tag that gets registered with WordPress |
||
| 46 | * |
||
| 47 | * @return string |
||
| 48 | */ |
||
| 49 | public function getTag() |
||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * the time in seconds to cache the results of the processShortcode() method |
||
| 58 | * 0 means the processShortcode() results will NOT be cached at all |
||
| 59 | * |
||
| 60 | * @return int |
||
| 61 | */ |
||
| 62 | public function cacheExpiration() |
||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * a place for adding any initialization code that needs to run prior to wp_header(). |
||
| 71 | * this may be required for shortcodes that utilize a corresponding module, |
||
| 72 | * and need to enqueue assets for that module |
||
| 73 | * |
||
| 74 | * @return void |
||
| 75 | */ |
||
| 76 | public function initializeShortcode() |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * process_shortcode - ESPRESSO_EVENT_ATTENDEES - Returns a list of attendees to an event. |
||
| 84 | * [ESPRESSO_EVENT_ATTENDEES] |
||
| 85 | * - defaults to attendees for earliest active event, or earliest upcoming event. |
||
| 86 | * [ESPRESSO_EVENT_ATTENDEES event_id=123] |
||
| 87 | * - attendees for specific event. |
||
| 88 | * [ESPRESSO_EVENT_ATTENDEES datetime_id=245] |
||
| 89 | * - attendees for a specific datetime. |
||
| 90 | * [ESPRESSO_EVENT_ATTENDEES ticket_id=123] |
||
| 91 | * - attendees for a specific ticket. |
||
| 92 | * [ESPRESSO_EVENT_ATTENDEES status=all] |
||
| 93 | * - specific registration status (use status id) or all for all attendees regardless of status. |
||
| 94 | * Note default is to only return approved attendees |
||
| 95 | * [ESPRESSO_EVENT_ATTENDEES show_gravatar=true] |
||
| 96 | * - default is to not return gravatar. Otherwise if this is set then return gravatar for email address given. |
||
| 97 | * [ESPRESSO_EVENT_ATTENDEES display_on_archives=true] |
||
| 98 | * - default is to not display attendees list on archive pages. |
||
| 99 | * Note: because of the relationship between event_id, ticket_id, and datetime_id: |
||
| 100 | * If more than one of those params is included, then preference is given to the following: |
||
| 101 | * - event_id is used whenever its present and any others are ignored. |
||
| 102 | * - if no event_id then datetime is used whenever its present and any others are ignored. |
||
| 103 | * - otherwise ticket_id is used if present. |
||
| 104 | * |
||
| 105 | * @param array $attributes |
||
| 106 | * @return string |
||
| 107 | * @throws EE_Error |
||
| 108 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 109 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
| 110 | * @throws \InvalidArgumentException |
||
| 111 | */ |
||
| 112 | public function processShortcode($attributes = array()) |
||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * merge incoming attributes with filtered defaults |
||
| 149 | * |
||
| 150 | * @param array $attributes |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | private function getAttributes(array $attributes) |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * Set all the base template arguments from the incoming attributes. |
||
| 171 | * |
||
| 172 | * * Note: because of the relationship between event_id, ticket_id, and datetime_id: |
||
| 173 | * If more than one of those params is included, then preference is given to the following: |
||
| 174 | * - event_id is used whenever its present and any others are ignored. |
||
| 175 | * - if no event_id then datetime is used whenever its present and any others are ignored. |
||
| 176 | * - otherwise ticket_id is used if present. |
||
| 177 | * |
||
| 178 | * @param array $attributes |
||
| 179 | * @throws EE_Error |
||
| 180 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 181 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
| 182 | * @throws \InvalidArgumentException |
||
| 183 | */ |
||
| 184 | private function setBaseTemplateArguments(array $attributes) |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * Validates the presence of entities for the given attribute values. |
||
| 199 | * @param array $attributes |
||
| 200 | * @throws EntityNotFoundException |
||
| 201 | */ |
||
| 202 | private function validateEntities(array $attributes) |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * Sets the query params for the base query elements. |
||
| 231 | */ |
||
| 232 | private function setBaseQueryParams() |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * @param array $attributes |
||
| 259 | * @return EE_Event|null |
||
| 260 | * @throws EE_Error |
||
| 261 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 262 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
| 263 | * @throws \InvalidArgumentException |
||
| 264 | */ |
||
| 265 | private function getEvent(array $attributes) |
||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * @param array $attributes |
||
| 311 | * @return EE_Datetime|null |
||
| 312 | * @throws EE_Error |
||
| 313 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 314 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
| 315 | * @throws \InvalidArgumentException |
||
| 316 | */ |
||
| 317 | View Code Duplication | private function getDatetime(array $attributes) |
|
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * @param array $attributes |
||
| 331 | * @return \EE_Base_Class|EE_Ticket|null |
||
| 332 | * @throws EE_Error |
||
| 333 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 334 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
| 335 | * @throws \InvalidArgumentException |
||
| 336 | */ |
||
| 337 | View Code Duplication | private function getTicket(array $attributes) |
|
| 347 | |||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * @param array $attributes |
||
| 352 | * @throws EE_Error |
||
| 353 | */ |
||
| 354 | private function setAdditionalQueryParams(array $attributes) |
||
| 366 | |||
| 367 | |||
| 368 | |||
| 369 | } |
||
| 370 | // End of file EspressoEventAttendees.php |
||
| 372 |