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 |
||
| 27 | class EspressoEventAttendees extends EspressoShortcode |
||
| 28 | { |
||
| 29 | |||
| 30 | private $query_params = array( |
||
| 31 | 0 => array(), |
||
| 32 | ); |
||
| 33 | |||
| 34 | private $template_args = array( |
||
| 35 | 'contacts' => array(), |
||
| 36 | 'event' => null, |
||
| 37 | 'datetime' => null, |
||
| 38 | 'ticket' => null, |
||
| 39 | ); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * the actual shortcode tag that gets registered with WordPress |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public function getTag() |
||
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * the time in seconds to cache the results of the processShortcode() method |
||
| 54 | * 0 means the processShortcode() results will NOT be cached at all |
||
| 55 | * |
||
| 56 | * @return int |
||
| 57 | */ |
||
| 58 | public function cacheExpiration() |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * a place for adding any initialization code that needs to run prior to wp_header(). |
||
| 66 | * this may be required for shortcodes that utilize a corresponding module, |
||
| 67 | * and need to enqueue assets for that module |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | public function initializeShortcode() |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * process_shortcode - ESPRESSO_EVENT_ATTENDEES - Returns a list of attendees to an event. |
||
| 79 | * [ESPRESSO_EVENT_ATTENDEES] |
||
| 80 | * - defaults to attendees for earliest active event, or earliest upcoming event. |
||
| 81 | * [ESPRESSO_EVENT_ATTENDEES event_id=123] |
||
| 82 | * - attendees for specific event. |
||
| 83 | * [ESPRESSO_EVENT_ATTENDEES datetime_id=245] |
||
| 84 | * - attendees for a specific datetime. |
||
| 85 | * [ESPRESSO_EVENT_ATTENDEES ticket_id=123] |
||
| 86 | * - attendees for a specific ticket. |
||
| 87 | * [ESPRESSO_EVENT_ATTENDEES status=all] |
||
| 88 | * - specific registration status (use status id) or all for all attendees regardless of status. |
||
| 89 | * Note default is to only return approved attendees |
||
| 90 | * [ESPRESSO_EVENT_ATTENDEES show_gravatar=true] |
||
| 91 | * - default is to not return gravatar. Otherwise if this is set then return gravatar for email address given. |
||
| 92 | * [ESPRESSO_EVENT_ATTENDEES display_on_archives=true] |
||
| 93 | * - default is to not display attendees list on archive pages. |
||
| 94 | * Note: because of the relationship between event_id, ticket_id, and datetime_id: |
||
| 95 | * If more than one of those params is included, then preference is given to the following: |
||
| 96 | * - event_id is used whenever its present and any others are ignored. |
||
| 97 | * - if no event_id then datetime is used whenever its present and any others are ignored. |
||
| 98 | * - otherwise ticket_id is used if present. |
||
| 99 | * |
||
| 100 | * @param array $attributes |
||
| 101 | * @return string |
||
| 102 | * @throws EE_Error |
||
| 103 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 104 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
| 105 | * @throws \InvalidArgumentException |
||
| 106 | */ |
||
| 107 | public function processShortcode($attributes = array()) |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * merge incoming attributes with filtered defaults |
||
| 143 | * |
||
| 144 | * @param array $attributes |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | private function getAttributes(array $attributes) |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Set all the base template arguments from the incoming attributes. |
||
| 165 | * * Note: because of the relationship between event_id, ticket_id, and datetime_id: |
||
| 166 | * If more than one of those params is included, then preference is given to the following: |
||
| 167 | * - event_id is used whenever its present and any others are ignored. |
||
| 168 | * - if no event_id then datetime is used whenever its present and any others are ignored. |
||
| 169 | * - otherwise ticket_id is used if present. |
||
| 170 | * |
||
| 171 | * @param array $attributes |
||
| 172 | * @throws EE_Error |
||
| 173 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 174 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
| 175 | * @throws \InvalidArgumentException |
||
| 176 | */ |
||
| 177 | private function setBaseTemplateArguments(array $attributes) |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * Validates the presence of entities for the given attribute values. |
||
| 192 | * |
||
| 193 | * @param array $attributes |
||
| 194 | * @throws EntityNotFoundException |
||
| 195 | */ |
||
| 196 | private function validateEntities(array $attributes) |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Sets the query params for the base query elements. |
||
| 225 | */ |
||
| 226 | private function setBaseQueryParams() |
||
| 249 | |||
| 250 | |||
| 251 | /** |
||
| 252 | * @param array $attributes |
||
| 253 | * @return EE_Event|null |
||
| 254 | * @throws EE_Error |
||
| 255 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 256 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
| 257 | * @throws \InvalidArgumentException |
||
| 258 | */ |
||
| 259 | private function getEvent(array $attributes) |
||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * @param array $attributes |
||
| 305 | * @return EE_Datetime|null |
||
| 306 | * @throws EE_Error |
||
| 307 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 308 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
| 309 | * @throws \InvalidArgumentException |
||
| 310 | */ |
||
| 311 | View Code Duplication | private function getDatetime(array $attributes) |
|
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * @param array $attributes |
||
| 325 | * @return \EE_Base_Class|EE_Ticket|null |
||
| 326 | * @throws EE_Error |
||
| 327 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
| 328 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
| 329 | * @throws \InvalidArgumentException |
||
| 330 | */ |
||
| 331 | View Code Duplication | private function getTicket(array $attributes) |
|
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * @param array $attributes |
||
| 345 | * @throws EE_Error |
||
| 346 | */ |
||
| 347 | private function setAdditionalQueryParams(array $attributes) |
||
| 359 | } |
||
| 360 |