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 SectionEvent 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 SectionEvent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class SectionEvent extends Event |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * An associative array of results from the filters that have run |
||
| 18 | * on this event. |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | public $filter_results = array(); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * An associative array of errors from the filters that have run |
||
| 25 | * on this event. |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | public $filter_errors = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * This method will construct XML that represents the result of |
||
| 32 | * an Event filter. |
||
| 33 | * |
||
| 34 | * @param string $name |
||
| 35 | * The name of the filter |
||
| 36 | * @param string $status |
||
| 37 | * The status of the filter, either passed or failed. |
||
| 38 | * @param XMLElement|string $message |
||
| 39 | * Optionally, an XMLElement or string to be appended to this |
||
| 40 | * `<filter>` element. XMLElement allows for more complex return |
||
| 41 | * types. |
||
| 42 | * @param array $attributes |
||
| 43 | * An associative array of additional attributes to add to this |
||
| 44 | * `<filter>` element |
||
| 45 | * @return XMLElement |
||
| 46 | */ |
||
| 47 | public static function buildFilterElement($name, $status, $message = null, array $attributes = null) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Appends errors generated from fields during the execution of an Event |
||
| 64 | * |
||
| 65 | * @param XMLElement $result |
||
| 66 | * @param array $fields |
||
| 67 | * @param array $errors |
||
| 68 | * @param object $post_values |
||
| 69 | * @throws Exception |
||
| 70 | * @return XMLElement |
||
| 71 | */ |
||
| 72 | public static function appendErrors(XMLElement $result, array $fields, $errors, $post_values) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Given a Field instance, the type of error, and the message, this function |
||
| 114 | * creates an XMLElement node so that it can be added to the `?debug` for the |
||
| 115 | * Event |
||
| 116 | * |
||
| 117 | * @since Symphony 2.5.0 |
||
| 118 | * @param Field $field |
||
| 119 | * @param string $type |
||
| 120 | * At the moment 'missing' or 'invalid' accepted |
||
| 121 | * @param string $message |
||
| 122 | * @return XMLElement |
||
| 123 | */ |
||
| 124 | public static function createError(Field $field, $type, $message = null) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * This function searches the `$haystack` for the given `$needle`, |
||
| 138 | * where the needle is a string representation of where the desired |
||
| 139 | * value exists in the `$haystack` array. For example `fields[name]` |
||
| 140 | * would look in the `$haystack` for the key of `fields` that has the |
||
| 141 | * key `name` and return the value. |
||
| 142 | * |
||
| 143 | * @param string $needle |
||
| 144 | * The needle, ie. `fields[name]`. |
||
| 145 | * @param array $haystack |
||
| 146 | * Associative array to find the needle, ie. |
||
| 147 | * `array('fields' => array( |
||
| 148 | * 'name' => 'Bob', |
||
| 149 | * 'age' => '10' |
||
| 150 | * ))` |
||
| 151 | * @param string $default |
||
| 152 | * If the `$needle` is not found, return this value. Defaults to null. |
||
| 153 | * @param boolean $discard_field_name |
||
| 154 | * When matches are found in the `$haystack`, they are added to results |
||
| 155 | * array. This parameter defines if this should be an associative array |
||
| 156 | * or just an array of the matches. Used in conjunction with `$collapse` |
||
| 157 | * @param boolean $collapse |
||
| 158 | * If multiple values are found, this will cause them to be reduced |
||
| 159 | * to single string with ' ' as the separator. Defaults to true. |
||
| 160 | * @return string|array |
||
| 161 | */ |
||
| 162 | public static function replaceFieldToken($needle, $haystack, $default = null, $discard_field_name = true, $collapse = true) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Helper method to determine if a field is missing, or if the data |
||
| 193 | * provided was invalid. Used in conjunction with `array_reduce`. |
||
| 194 | * |
||
| 195 | * @param array $a, |
||
| 196 | * @param array $b |
||
| 197 | * @return string |
||
| 198 | * 'missing' or 'invalid' |
||
| 199 | */ |
||
| 200 | public function __reduceType($a, $b) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * This function will process the core Filters, Admin Only and Expect |
||
| 211 | * Multiple, before invoking the `__doit` function, which actually |
||
| 212 | * processes the Event. Once the Event has executed, this function will |
||
| 213 | * determine if the user should be redirected to a URL, or to just return |
||
| 214 | * the XML. |
||
| 215 | * |
||
| 216 | * @throws Exception |
||
| 217 | * @return XMLElement|void |
||
| 218 | * If `$_REQUEST{'redirect']` is set, and the Event executed successfully, |
||
| 219 | * the user will be redirected to the given location. If `$_REQUEST['redirect']` |
||
| 220 | * is not set, or the Event encountered errors, an XMLElement of the Event |
||
| 221 | * result will be returned. |
||
| 222 | */ |
||
| 223 | public function execute() |
||
| 224 | { |
||
| 225 | if (!isset($this->eParamFILTERS) || !is_array($this->eParamFILTERS)) { |
||
| 226 | $this->eParamFILTERS = array(); |
||
| 227 | } |
||
| 228 | |||
| 229 | $result = new XMLElement($this->ROOTELEMENT); |
||
| 230 | |||
| 231 | if (in_array('admin-only', $this->eParamFILTERS) && !Symphony::Engine()->isLoggedIn()) { |
||
| 232 | $result->setAttribute('result', 'error'); |
||
| 233 | $result->appendChild(new XMLElement('message', __('Entry encountered errors when saving.'), array( |
||
| 234 | 'message-id' => EventMessages::ENTRY_ERRORS |
||
| 235 | ))); |
||
| 236 | $result->appendChild(self::buildFilterElement('admin-only', 'failed')); |
||
| 237 | return $result; |
||
| 238 | } |
||
| 239 | |||
| 240 | $entry_id = $position = $fields = null; |
||
| 241 | $post = General::getPostData(); |
||
| 242 | $success = true; |
||
| 243 | if (!is_array($post['fields'])) { |
||
| 244 | $post['fields'] = array(); |
||
| 245 | } |
||
| 246 | |||
| 247 | if (in_array('expect-multiple', $this->eParamFILTERS)) { |
||
| 248 | foreach ($post['fields'] as $position => $fields) { |
||
| 249 | if (isset($post['id'][$position]) && is_numeric($post['id'][$position])) { |
||
| 250 | $entry_id = $post['id'][$position]; |
||
| 251 | } else { |
||
| 252 | $entry_id = null; |
||
| 253 | } |
||
| 254 | |||
| 255 | $entry = new XMLElement('entry', null, array('position' => $position)); |
||
| 256 | |||
| 257 | // Reset errors for each entry execution |
||
| 258 | $this->filter_results = $this->filter_errors = array(); |
||
| 259 | |||
| 260 | // Ensure that we are always dealing with an array. |
||
| 261 | if (!is_array($fields)) { |
||
| 262 | $fields = array(); |
||
| 263 | } |
||
| 264 | |||
| 265 | // Execute the event for this entry |
||
| 266 | if (!$this->__doit($fields, $entry, $position, $entry_id)) { |
||
| 267 | $success = false; |
||
| 268 | } |
||
| 269 | |||
| 270 | $result->appendChild($entry); |
||
| 271 | } |
||
| 272 | } else { |
||
| 273 | $fields = $post['fields']; |
||
| 274 | |||
| 275 | if (isset($post['id']) && is_numeric($post['id'])) { |
||
| 276 | $entry_id = $post['id']; |
||
| 277 | } |
||
| 278 | |||
| 279 | $success = $this->__doit($fields, $result, null, $entry_id); |
||
| 280 | } |
||
| 281 | |||
| 282 | if ($success && isset($_REQUEST['redirect'])) { |
||
| 283 | redirect($_REQUEST['redirect']); |
||
| 284 | } |
||
| 285 | |||
| 286 | return $result; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * This function does the bulk of processing the Event, from running the delegates |
||
| 291 | * to validating the data and eventually saving the data into Symphony. The result |
||
| 292 | * of the Event is returned via the `$result` parameter. |
||
| 293 | * |
||
| 294 | * @param array $fields |
||
| 295 | * An array of $_POST data, to process and add/edit an entry. |
||
| 296 | * @param XMLElement $result |
||
| 297 | * The XMLElement contains the result of the Event, it is passed by |
||
| 298 | * reference. |
||
| 299 | * @param integer $position |
||
| 300 | * When the Expect Multiple filter is added, this event should expect |
||
| 301 | * to deal with adding (or editing) multiple entries at once. |
||
| 302 | * @param integer $entry_id |
||
| 303 | * If this Event is editing an existing entry, that Entry ID will |
||
| 304 | * be passed to this function. |
||
| 305 | * @throws Exception |
||
| 306 | * @return XMLElement |
||
| 307 | * The result of the Event |
||
| 308 | */ |
||
| 309 | public function __doit(array $fields = array(), XMLElement &$result, $position = null, $entry_id = null) |
||
| 310 | { |
||
| 311 | $post_values = new XMLElement('post-values'); |
||
| 312 | |||
| 313 | if (!is_array($this->eParamFILTERS)) { |
||
| 314 | $this->eParamFILTERS = array(); |
||
| 315 | } |
||
| 316 | |||
| 317 | // Check to see if the Section of this Event is valid. |
||
| 318 | if (!$section = SectionManager::fetch($this->getSource())) { |
||
| 319 | $result->setAttribute('result', 'error'); |
||
| 320 | $result->appendChild(new XMLElement('message', __('The Section, %s, could not be found.', array($this->getSource())), array( |
||
| 321 | 'message-id' => EventMessages::SECTION_MISSING |
||
| 322 | ))); |
||
| 323 | return false; |
||
| 324 | } |
||
| 325 | |||
| 326 | // Create the post data element |
||
| 327 | if (!empty($fields)) { |
||
| 328 | General::array_to_xml($post_values, $fields, true); |
||
| 329 | } |
||
| 330 | |||
| 331 | // If the EventPreSaveFilter fails, return early |
||
| 332 | if ($this->processPreSaveFilters($result, $fields, $post_values, $entry_id) === false) { |
||
| 333 | return false; |
||
| 334 | } |
||
| 335 | |||
| 336 | // If the `$entry_id` is provided, check to see if it exists. |
||
| 337 | // @todo If this was moved above PreSaveFilters, we can pass the |
||
| 338 | // Entry object to the delegate meaning extensions don't have to |
||
| 339 | // do that step. |
||
| 340 | if (isset($entry_id)) { |
||
| 341 | $entry = EntryManager::fetch($entry_id); |
||
| 342 | $entry = $entry[0]; |
||
| 343 | |||
| 344 | View Code Duplication | if (!is_object($entry)) { |
|
| 345 | $result->setAttribute('result', 'error'); |
||
| 346 | $result->appendChild(new XMLElement('message', __('The Entry, %s, could not be found.', array($entry_id)), array( |
||
| 347 | 'message-id' => EventMessages::ENTRY_MISSING |
||
| 348 | ))); |
||
| 349 | |||
| 350 | return false; |
||
| 351 | } |
||
| 352 | |||
| 353 | // `$entry_id` wasn't provided, create a new Entry object. |
||
| 354 | } else { |
||
| 355 | $entry = EntryManager::create(); |
||
| 356 | $entry->set('section_id', $this->getSource()); |
||
| 357 | } |
||
| 358 | |||
| 359 | // Validate the data. `$entry->checkPostData` loops over all fields calling |
||
| 360 | // their `checkPostFieldData` function. If the return of the function is |
||
| 361 | // `Entry::__ENTRY_FIELD_ERROR__` then abort the event and add the error |
||
| 362 | // messages to the `$result`. |
||
| 363 | if (Entry::__ENTRY_FIELD_ERROR__ === $entry->checkPostData($fields, $errors, ($entry->get('id') ? true : false))) { |
||
| 364 | $result = self::appendErrors($result, $fields, $errors, $post_values); |
||
| 365 | return false; |
||
| 366 | |||
| 367 | // If the data is good, process the data, almost ready to save it to the |
||
| 368 | // Database. If processing fails, abort the event and display the errors |
||
| 369 | } elseif (Entry::__ENTRY_OK__ !== $entry->setDataFromPost($fields, $errors, false, ($entry->get('id') ? true : false))) { |
||
| 370 | $result = self::appendErrors($result, $fields, $errors, $post_values); |
||
| 371 | return false; |
||
| 372 | |||
| 373 | // Data is checked, data has been processed, by trying to save the |
||
| 374 | // Entry caused an error to occur, so abort and return. |
||
| 375 | } elseif ($entry->commit() === false) { |
||
| 376 | $result->setAttribute('result', 'error'); |
||
| 377 | $result->appendChild(new XMLElement('message', __('Unknown errors where encountered when saving.'), array( |
||
| 378 | 'message-id' => EventMessages::UNKNOWN_ERROR |
||
| 379 | ))); |
||
| 380 | |||
| 381 | if (isset($post_values) && is_object($post_values)) { |
||
| 382 | $result->appendChild($post_values); |
||
| 383 | } |
||
| 384 | |||
| 385 | return false; |
||
| 386 | |||
| 387 | // Entry was created, add the good news to the return `$result` |
||
| 388 | } else { |
||
| 389 | $result->setAttributeArray(array( |
||
| 390 | 'result' => 'success', |
||
| 391 | 'type' => (isset($entry_id) ? 'edited' : 'created'), |
||
| 392 | 'id' => $entry->get('id') |
||
| 393 | )); |
||
| 394 | |||
| 395 | if (isset($entry_id)) { |
||
| 396 | $result->appendChild(new XMLElement('message', __('Entry edited successfully.'), array( |
||
| 397 | 'message-id' => EventMessages::ENTRY_EDITED_SUCCESS |
||
| 398 | ))); |
||
| 399 | } else { |
||
| 400 | $result->appendChild(new XMLElement('message', __('Entry created successfully.'), array( |
||
| 401 | 'message-id' => EventMessages::ENTRY_CREATED_SUCCESS |
||
| 402 | ))); |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | // PASSIVE FILTERS ONLY AT THIS STAGE. ENTRY HAS ALREADY BEEN CREATED. |
||
| 407 | if (in_array('send-email', $this->eParamFILTERS) && !in_array('expect-multiple', $this->eParamFILTERS)) { |
||
| 408 | $result = $this->processSendMailFilter($result, $_POST['send-email'], $fields, $section, $entry); |
||
| 409 | } |
||
| 410 | |||
| 411 | $result = $this->processPostSaveFilters($result, $fields, $entry); |
||
| 412 | $result = $this->processFinalSaveFilters($result, $fields, $entry); |
||
| 413 | |||
| 414 | if (isset($post_values) && is_object($post_values)) { |
||
| 415 | $result->appendChild($post_values); |
||
| 416 | } |
||
| 417 | |||
| 418 | return true; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Processes all extensions attached to the `EventPreSaveFilter` delegate |
||
| 423 | * |
||
| 424 | * @uses EventPreSaveFilter |
||
| 425 | * |
||
| 426 | * @param XMLElement $result |
||
| 427 | * @param array $fields |
||
| 428 | * @param XMLElement $post_values |
||
| 429 | * @param integer $entry_id |
||
| 430 | * @return boolean |
||
| 431 | */ |
||
| 432 | protected function processPreSaveFilters(XMLElement $result, array &$fields, XMLElement &$post_values, $entry_id = null) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Processes all extensions attached to the `EventPostSaveFilter` delegate |
||
| 501 | * |
||
| 502 | * @uses EventPostSaveFilter |
||
| 503 | * |
||
| 504 | * @param XMLElement $result |
||
| 505 | * @param array $fields |
||
| 506 | * @param Entry $entry |
||
| 507 | * @return XMLElement |
||
| 508 | */ |
||
| 509 | View Code Duplication | protected function processPostSaveFilters(XMLElement $result, array $fields, Entry $entry = null) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Processes all extensions attached to the `EventFinalSaveFilter` delegate |
||
| 555 | * |
||
| 556 | * @uses EventFinalSaveFilter |
||
| 557 | * |
||
| 558 | * @param XMLElement $result |
||
| 559 | * @param array $fields |
||
| 560 | * @param Entry $entry |
||
| 561 | * @return XMLElement |
||
| 562 | */ |
||
| 563 | View Code Duplication | protected function processFinalSaveFilters(XMLElement $result, array $fields, Entry $entry = null) |
|
| 613 | |||
| 614 | /** |
||
| 615 | * This function handles the Send Mail filter which will send an email |
||
| 616 | * to each specified recipient informing them that an Entry has been |
||
| 617 | * created. |
||
| 618 | * |
||
| 619 | * @param XMLElement $result |
||
| 620 | * The XMLElement of the XML that is going to be returned as part |
||
| 621 | * of this event to the page. |
||
| 622 | * @param array $send_email |
||
| 623 | * Associative array of `send-mail` parameters.* Associative array of `send-mail` parameters. |
||
| 624 | * @param array $fields |
||
| 625 | * Array of post data to extract the values from |
||
| 626 | * @param Section $section |
||
| 627 | * This current Entry that has just been updated or created |
||
| 628 | * @param Entry $entry |
||
| 629 | * @throws Exception |
||
| 630 | * @return XMLElement |
||
| 631 | * The modified `$result` with the results of the filter. |
||
| 632 | */ |
||
| 633 | public function processSendMailFilter(XMLElement $result, array $send_email, array &$fields, Section $section, Entry $entry) |
||
| 745 | } |
||
| 746 | |||
| 774 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: