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 TableService 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 TableService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class TableService extends AbstractTca |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $tca; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $columnTca; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $tableName; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $instances; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @throws InvalidKeyInArrayException |
||
| 45 | * @param string $tableName |
||
| 46 | * @return \Fab\Vidi\Tca\TableService |
||
|
|
|||
| 47 | */ |
||
| 48 | public function __construct($tableName) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Tell whether the table has a label field. |
||
| 60 | * |
||
| 61 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public function hasLabelField() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get the label name of table name. |
||
| 71 | * |
||
| 72 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public function getLabelField() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Returns the translated label of the table name. |
||
| 86 | * |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | View Code Duplication | public function getLabel() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Returns the title of the table. |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | View Code Duplication | public function getTitle() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Return the "disabled" field. |
||
| 122 | * |
||
| 123 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 124 | * @return string|null |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function getHiddenField() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Return the "starttime" field. |
||
| 138 | * |
||
| 139 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 140 | * @return string|null |
||
| 141 | */ |
||
| 142 | View Code Duplication | public function getStartTimeField() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Return the "endtime" field. |
||
| 154 | * |
||
| 155 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 156 | * @return string|null |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function getEndTimeField() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Tells whether the table is hidden. |
||
| 170 | * |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public function isHidden() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Tells whether the table is not hidden. |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function isNotHidden() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the "deleted" field for the table. |
||
| 190 | * |
||
| 191 | * @return string|null |
||
| 192 | */ |
||
| 193 | public function getDeletedField() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get the modification time stamp field. |
||
| 200 | * |
||
| 201 | * @return string|null |
||
| 202 | */ |
||
| 203 | public function getTimeModificationField() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the creation time stamp field. |
||
| 210 | * |
||
| 211 | * @return string|null |
||
| 212 | */ |
||
| 213 | public function getTimeCreationField() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the language field for the table. |
||
| 220 | * |
||
| 221 | * @return string|null |
||
| 222 | */ |
||
| 223 | public function getLanguageField() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the field which points to the parent. |
||
| 230 | * |
||
| 231 | * @return string|null |
||
| 232 | */ |
||
| 233 | public function getLanguageParentField() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Returns the default order in the form of a SQL segment. |
||
| 240 | * |
||
| 241 | * @return string|null |
||
| 242 | */ |
||
| 243 | public function getDefaultOrderSql() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns the parsed default orderings. |
||
| 252 | * Returns array looks like array('title' => 'ASC'); |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public function getDefaultOrderings() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Returns the searchable fields. |
||
| 277 | * |
||
| 278 | * @return string|null |
||
| 279 | */ |
||
| 280 | public function getSearchFields() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns an array containing the field names. |
||
| 287 | * |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | public function getFields() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Returns an array containing the fields and their configuration. |
||
| 297 | * |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | public function getFieldsAndConfiguration() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Tell whether we have a field "sorting". |
||
| 307 | * |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | public function hasSortableField() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Tell whether the field exists or not. |
||
| 317 | * |
||
| 318 | * @param string $fieldName |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | public function hasField($fieldName) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Tell whether the field name contains a path, e.g. metadata.title |
||
| 342 | * |
||
| 343 | * @param string $fieldName |
||
| 344 | * @return boolean |
||
| 345 | */ |
||
| 346 | public function isComposite($fieldName) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Tells whether the $key exists. |
||
| 353 | * |
||
| 354 | * @param string $key |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public function has($key) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Tells whether the table name has "workspace" support. |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function hasWorkspaceSupport() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Tells whether the table name has "language" support. |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function hasLanguageSupport() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Return configuration value given a key. |
||
| 384 | * |
||
| 385 | * @param string $key |
||
| 386 | * @return string|null |
||
| 387 | */ |
||
| 388 | public function get($key) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | public function getTca() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Tell whether the current BE User has access to this field. |
||
| 403 | * |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | public function hasAccess() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param string $fieldName |
||
| 417 | * @throws \Exception |
||
| 418 | * @return \Fab\Vidi\Tca\FieldService |
||
| 419 | */ |
||
| 420 | public function field($fieldName) |
||
| 465 | |||
| 466 | } |
||
| 467 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.