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 Entry 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 Entry, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Entry |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * The constant for when an Entry is ok, that is, no errors have |
||
| 18 | * been raised by any of it's Fields. |
||
| 19 | * @var integer |
||
| 20 | */ |
||
| 21 | const __ENTRY_OK__ = 0; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The constant for an Entry if there is an error is raised by any of |
||
| 25 | * it's Fields. |
||
| 26 | * @var integer |
||
| 27 | */ |
||
| 28 | const __ENTRY_FIELD_ERROR__ = 100; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * An associative array of basic metadata/settings for this Entry |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $_fields = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * An associative array of the data for each of the Fields that make up |
||
| 38 | * this Entry. The key is the Field ID, and the value is typically an array |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $_data = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * An ISO 8601 representation of when this Entry was created |
||
| 45 | * eg. `2004-02-12T15:19:21+00:00` |
||
| 46 | * @deprecated Since Symphony 2.3.1, use $entry->get('creation_date') instead. This |
||
| 47 | * variable will be removed in Symphony 3.0 |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | public $creationDate = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Entries have some basic metadata settings such as the Entry ID, the Author ID |
||
| 54 | * who created it and the Section that the Entry sits in. This function will set a |
||
| 55 | * setting to a value overwriting any existing value for this setting |
||
| 56 | * |
||
| 57 | * @param string $setting |
||
| 58 | * the setting key. |
||
| 59 | * @param mixed $value |
||
| 60 | * the value of the setting. |
||
| 61 | */ |
||
| 62 | public function set($setting, $value) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Accessor to the a setting by name. If no setting is provided all the |
||
| 69 | * settings of this Entry instance are returned. |
||
| 70 | * |
||
| 71 | * @param string $setting (optional) |
||
| 72 | * the name of the setting to access the value for. This is optional and |
||
| 73 | * defaults to null in which case all settings are returned. |
||
| 74 | * @return null|mixed|array |
||
| 75 | * the value of the setting if there is one, all settings if the input setting |
||
| 76 | * was omitted or null if the setting was supplied but there is no value |
||
| 77 | * for that setting. |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function get($setting = null) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Creates the initial entry row in tbl_entries and returns the resulting |
||
| 94 | * Entry ID using `getInsertID()`. |
||
| 95 | * |
||
| 96 | * @see toolkit.MySQL#getInsertID() |
||
| 97 | * @throws DatabaseException |
||
| 98 | * @return integer |
||
| 99 | */ |
||
| 100 | public function assignEntryId() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Set the data for a Field in this Entry, given the Field ID and it's data |
||
| 120 | * |
||
| 121 | * @param integer $field_id |
||
| 122 | * The ID of the Field this data is for |
||
| 123 | * @param mixed $data |
||
| 124 | * Often an array |
||
| 125 | */ |
||
| 126 | public function setData($field_id, $data) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * When an entry is saved from a form (either Frontend/Backend) this |
||
| 133 | * function will find all the fields in this set and loop over them, setting |
||
| 134 | * the data to each of the fields for processing. If any errors occur during |
||
| 135 | * this, `_ENTRY_FIELD_ERROR_` is returned, and an array is available with |
||
| 136 | * the errors. |
||
| 137 | * |
||
| 138 | * @param array $data |
||
| 139 | * An associative array of the data for this entry where they key is the |
||
| 140 | * Field's handle for this Section and the value is the data from the form |
||
| 141 | * @param array $errors |
||
| 142 | * An associative array of errors, by reference. The key is the `field_id`, the value |
||
| 143 | * is the message text. Defaults to an empty array |
||
| 144 | * @param boolean $simulate |
||
| 145 | * If $simulate is given as true, a dry run of this function will occur, where |
||
| 146 | * regardless of errors, an Entry will not be saved in the database. Defaults to |
||
| 147 | * false |
||
| 148 | * @param boolean $ignore_missing_fields |
||
| 149 | * This parameter allows Entries to be updated, rather than replaced. This is |
||
| 150 | * useful if the input form only contains a couple of the fields for this Entry. |
||
| 151 | * Defaults to false, which will set Fields to their default values if they are not |
||
| 152 | * provided in the $data |
||
| 153 | * @throws DatabaseException |
||
| 154 | * @throws Exception |
||
| 155 | * @return integer |
||
| 156 | * Either `Entry::__ENTRY_OK__` or `Entry::__ENTRY_FIELD_ERROR__` |
||
| 157 | */ |
||
| 158 | public function setDataFromPost($data, &$errors = null, $simulate = false, $ignore_missing_fields = false) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Accessor function to return data from this Entry for a particular |
||
| 202 | * field. Optional parameter to return this data as an object instead |
||
| 203 | * of an array. If a Field is not provided, an associative array of all data |
||
| 204 | * assigned to this Entry will be returned. |
||
| 205 | * |
||
| 206 | * @param integer $field_id |
||
| 207 | * The ID of the Field whose data you want |
||
| 208 | * @param boolean $asObject |
||
| 209 | * If true, the data will be returned as an object instead of an |
||
| 210 | * array. Defaults to false. Note that if a `$field_id` is not provided |
||
| 211 | * the result will always be an array. |
||
| 212 | * @return array|object |
||
| 213 | * Depending on the value of `$asObject`, return the field's data |
||
| 214 | * as either an array or an object. If no data exists, null will be |
||
| 215 | * returned. |
||
| 216 | */ |
||
| 217 | public function getData($field_id = null, $asObject = false) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Given a array of data from a form, this function will iterate over all the fields |
||
| 232 | * in this Entry's Section and call their `checkPostFieldData()` function. |
||
| 233 | * |
||
| 234 | * @param array $data |
||
| 235 | * An associative array of the data for this entry where they key is the |
||
| 236 | * Field's handle for this Section and the value is the data from the form |
||
| 237 | * @param null|array $errors |
||
| 238 | * An array of errors, by reference. Defaults to empty* An array of errors, by reference. |
||
| 239 | * Defaults to empty |
||
| 240 | * @param boolean $ignore_missing_fields |
||
| 241 | * This parameter allows Entries to be updated, rather than replaced. This is |
||
| 242 | * useful if the input form only contains a couple of the fields for this Entry. |
||
| 243 | * Defaults to false, which will check all Fields even if they are not |
||
| 244 | * provided in the $data |
||
| 245 | * @throws Exception |
||
| 246 | * @return integer |
||
| 247 | * Either `Entry::__ENTRY_OK__` or `Entry::__ENTRY_FIELD_ERROR__` |
||
| 248 | */ |
||
| 249 | public function checkPostData($data, &$errors = null, $ignore_missing_fields = false) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Iterates over all the Fields in this Entry calling their |
||
| 301 | * `processRawFieldData()` function to set default values for this Entry. |
||
| 302 | * |
||
| 303 | * @see toolkit.Field#processRawFieldData() |
||
| 304 | */ |
||
| 305 | public function findDefaultData() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Commits this Entry's data to the database, by first finding the default |
||
| 334 | * data for this `Entry` and then utilising the `EntryManager`'s |
||
| 335 | * add or edit function. The `EntryManager::edit` function is used if |
||
| 336 | * the current `Entry` object has an ID, otherwise `EntryManager::add` |
||
| 337 | * is used. |
||
| 338 | * |
||
| 339 | * @see toolkit.Entry#findDefaultData() |
||
| 340 | * @throws Exception |
||
| 341 | * @return boolean |
||
| 342 | * true if the commit was successful, false otherwise. |
||
| 343 | */ |
||
| 344 | public function commit() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Entries may link to other Entries through fields. This function will return the |
||
| 353 | * number of entries that are associated with the current entry as an associative |
||
| 354 | * array. If there are no associated entries, null will be returned. |
||
| 355 | * |
||
| 356 | * @param array $associated_sections |
||
| 357 | * An associative array of sections to return the Entry counts from. Defaults to |
||
| 358 | * null, which will fetch all the associations of this Entry. |
||
| 359 | * @throws Exception |
||
| 360 | * @return array |
||
| 361 | * An associative array with the key being the associated Section's ID and the |
||
| 362 | * value being the number of entries associated with this Entry. |
||
| 363 | */ |
||
| 364 | public function fetchAllAssociatedEntryCounts($associated_sections = null) |
||
| 400 | } |
||
| 401 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.