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 Mapper 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 Mapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | abstract class Mapper extends \DB\SQL\Mapper |
||
| 29 | { |
||
| 30 | use Traits\Validation; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Fields and their visibility to clients, boolean or string of visible field name |
||
| 34 | * |
||
| 35 | * @var array $fieldsVisible |
||
| 36 | */ |
||
| 37 | protected $fieldsVisible = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Fields that are editable to clients, boolean or string of visible field name |
||
| 41 | * |
||
| 42 | * @var array $fieldsEditable |
||
| 43 | */ |
||
| 44 | protected $fieldsEditable = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var object database class |
||
| 48 | */ |
||
| 49 | protected $db; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string $table for the mapper - this string gets automatically quoted |
||
| 53 | */ |
||
| 54 | protected $table; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string $mapperName name for the mapper |
||
| 58 | */ |
||
| 59 | protected $mapperName; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string $uuid the fieldname used for the uuid |
||
| 63 | */ |
||
| 64 | protected $uuidField = 'uuid'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var boolean $valid the data after validation is valid? |
||
| 68 | */ |
||
| 69 | protected $valid = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array $originalData the original data when object created/loaded |
||
| 73 | */ |
||
| 74 | protected $originalData = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array $auditData data to write to audit log |
||
| 78 | */ |
||
| 79 | protected $auditData = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var boolean $initValidation automatically append validation settings for fields $this->validationRules/Default? |
||
| 83 | */ |
||
| 84 | protected $initValidation = true; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * initialize with array of params |
||
| 88 | * |
||
| 89 | */ |
||
| 90 | public function __construct() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Initialise automatic validation settings for fields |
||
| 106 | */ |
||
| 107 | public function initValidation() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Initialise hooks for the mapper object actions |
||
| 170 | */ |
||
| 171 | public function setupHooks() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * return string representation of class - json of data |
||
| 225 | * |
||
| 226 | * @param string |
||
| 227 | */ |
||
| 228 | public function __toString(): string |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * return array representation of class - json of data |
||
| 236 | * |
||
| 237 | * @param array |
||
| 238 | */ |
||
| 239 | public function __toArray(): array |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Cast the mapper data to an array using only provided fields |
||
| 246 | * |
||
| 247 | * @param mixed string|array fields to return in response |
||
| 248 | * @param array optional data optional data to use instead of fields |
||
| 249 | * @return array $data |
||
| 250 | */ |
||
| 251 | public function castFields($fields = null, array $data = []): array |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Cast the mapper data to an array and modify (for external clients typically) |
||
| 283 | * using the visible fields and names for export, converting dates to unixtime |
||
| 284 | * optionally pass in a comma (or space)-separated list of fields or an array of fields |
||
| 285 | * |
||
| 286 | * @param mixed string|array fields to return in response |
||
| 287 | * @param array optional data optional data to use instead of fields |
||
| 288 | * @return array $data |
||
| 289 | */ |
||
| 290 | public function exportArray($fields = null, array $data = []): array |
||
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * Convert the mapper object to format suitable for JSON |
||
| 335 | * |
||
| 336 | * @param boolean $unmodified cast as public (visible) data or raw db data? |
||
| 337 | * @param mixed $fields optional string|array fields to include |
||
| 338 | * @return string json-encoded data |
||
| 339 | */ |
||
| 340 | public function exportJson(bool $unmodified = false, $fields = null): string |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * Set a field (default named uuid) to a UUID value if one is not present. |
||
| 348 | * |
||
| 349 | * @param string $field the name of the field to check and set |
||
| 350 | * @return null|string $uuid the new uuid generated |
||
| 351 | */ |
||
| 352 | public function setUUID(string $field = 'uuid') |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Write data for audit logging |
||
| 375 | * |
||
| 376 | * @param $data array of data to audit log |
||
| 377 | * @return array $this->auditData return the updated audit data for the mapper |
||
| 378 | */ |
||
| 379 | public function audit(array $data = []): array |
||
| 384 | |||
| 385 | } |
||
| 386 |