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 |
||
| 26 | abstract class Mapper extends \DB\SQL\Mapper |
||
| 27 | { |
||
| 28 | use Traits\Validation; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Fields and their visibility to clients, boolean or string of visible field name |
||
| 32 | * |
||
| 33 | * @var array $fieldsVisible |
||
| 34 | */ |
||
| 35 | protected $fieldsVisible = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Fields that are editable to clients, boolean or string of visible field name |
||
| 39 | * |
||
| 40 | * @var array $fieldsEditable |
||
| 41 | */ |
||
| 42 | protected $fieldsEditable = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var object database class |
||
| 46 | */ |
||
| 47 | protected $db; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string $table for the mapper - this string gets automatically quoted |
||
| 51 | */ |
||
| 52 | protected $table; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string $mapperName name for the mapper |
||
| 56 | */ |
||
| 57 | protected $mapperName; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string $uuid the fieldname used for the uuid |
||
| 61 | */ |
||
| 62 | protected $uuidField = 'uuid'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var boolean $valid the data after validation is valid? |
||
| 66 | */ |
||
| 67 | protected $valid = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array $originalData the original data when object created/loaded |
||
| 71 | */ |
||
| 72 | protected $originalData = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array $auditData data to write to audit log |
||
| 76 | */ |
||
| 77 | protected $auditData = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * initialize with array of params |
||
| 81 | * |
||
| 82 | */ |
||
| 83 | public function __construct(array $params = []) |
||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * return string representation of class - json of data |
||
| 218 | * |
||
| 219 | * @param string |
||
| 220 | */ |
||
| 221 | public function __toString(): string |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * return array representation of class - json of data |
||
| 229 | * |
||
| 230 | * @param array |
||
| 231 | */ |
||
| 232 | public function __toArray(): array |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Cast the mapper data to an array using only provided fields |
||
| 239 | * |
||
| 240 | * @param mixed string|array fields to return in response |
||
| 241 | * @param array optional data optional data to use instead of fields |
||
| 242 | * @return array $data |
||
| 243 | */ |
||
| 244 | public function castFields($fields = null, array $data = []): array |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Cast the mapper data to an array and modify (for external clients typically) |
||
| 276 | * using the visible fields and names for export, converting dates to unixtime |
||
| 277 | * optionally pass in a comma (or space)-separated list of fields or an array of fields |
||
| 278 | * |
||
| 279 | * @param mixed string|array fields to return in response |
||
| 280 | * @param array optional data optional data to use instead of fields |
||
| 281 | * @return array $data |
||
| 282 | */ |
||
| 283 | public function exportArray($fields = null, array $data = []): array |
||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * Convert the mapper object to format suitable for JSON |
||
| 328 | * |
||
| 329 | * @param boolean $unmodified cast as public (visible) data or raw db data? |
||
| 330 | * @param mixed $fields optional string|array fields to include |
||
| 331 | * @return string json-encoded data |
||
| 332 | */ |
||
| 333 | public function exportJson(bool $unmodified = false, $fields = null): string |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * Set a field (default named uuid) to a UUID value if one is not present. |
||
| 341 | * |
||
| 342 | * @param string $field the name of the field to check and set |
||
| 343 | * @return null|string $uuid the new uuid generated |
||
| 344 | */ |
||
| 345 | public function setUUID(string $field = 'uuid') |
||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * Write data for audit logging |
||
| 368 | * |
||
| 369 | * @param $data array of data to audit log |
||
| 370 | * @return array $this->auditData return the updated audit data for the mapper |
||
| 371 | */ |
||
| 372 | public function audit(array $data = []): array |
||
| 377 | |||
| 378 | } |
||
| 379 |