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 | * all fields are visible except where 'false' |
||
35 | * |
||
36 | * @var array $fieldsVisible |
||
37 | */ |
||
38 | protected $fieldsVisible = []; |
||
39 | |||
40 | /** |
||
41 | * Fields that are editable to clients, boolean or string of visible field name |
||
42 | * |
||
43 | * @var array $fieldsEditable |
||
44 | */ |
||
45 | protected $fieldsEditable = []; |
||
46 | |||
47 | /** |
||
48 | * @var object database class |
||
49 | */ |
||
50 | protected $db; |
||
51 | |||
52 | /** |
||
53 | * @var string $table for the mapper - this string gets automatically quoted |
||
54 | */ |
||
55 | protected $table; |
||
56 | |||
57 | /** |
||
58 | * @var string $mapperName name for the mapper |
||
59 | */ |
||
60 | protected $mapperName; |
||
61 | |||
62 | /** |
||
63 | * @var string $uuid the fieldname used for the uuid |
||
64 | */ |
||
65 | protected $uuidField = 'uuid'; |
||
66 | |||
67 | /** |
||
68 | * @var boolean $valid the data after validation is valid? |
||
69 | */ |
||
70 | protected $valid = null; |
||
71 | |||
72 | /** |
||
73 | * @var array $originalData the original data when object created/loaded |
||
74 | */ |
||
75 | protected $originalData = []; |
||
76 | |||
77 | /** |
||
78 | * @var array $auditData data to write to audit log |
||
79 | */ |
||
80 | protected $auditData = []; |
||
81 | |||
82 | /** |
||
83 | * @var boolean $initValidation automatically append validation settings for fields $this->validationRules/Default? |
||
84 | */ |
||
85 | protected $initValidation = true; |
||
86 | |||
87 | /** |
||
88 | * initialize with array of params |
||
89 | * |
||
90 | */ |
||
91 | public function __construct() |
||
104 | |||
105 | /** |
||
106 | * Load by internal integer ID or by UUID if no array passed |
||
107 | * |
||
108 | * @param $filter string|array |
||
109 | * @param $options array |
||
110 | * @param $ttl int |
||
111 | * @see DB\Cursor::load($filter = NULL, array $options = NULL, $ttl = 0) |
||
112 | * @return array|FALSE |
||
113 | */ |
||
114 | public function load($filter = NULL, array $options = null, $ttl = 0) { |
||
124 | |||
125 | /** |
||
126 | * Initialise automatic validation settings for fields |
||
127 | */ |
||
128 | public function initValidation() |
||
188 | |||
189 | /** |
||
190 | * Initialise hooks for the mapper object actions |
||
191 | */ |
||
192 | public function setupHooks() |
||
246 | |||
247 | /** |
||
248 | * return string representation of class - json of data |
||
249 | * |
||
250 | * @param string |
||
251 | */ |
||
252 | public function __toString(): string |
||
256 | |||
257 | |||
258 | /** |
||
259 | * return array representation of class - json of data |
||
260 | * |
||
261 | * @param array |
||
262 | */ |
||
263 | public function __toArray(): array |
||
267 | |||
268 | /** |
||
269 | * Cast the mapper data to an array using only provided fields |
||
270 | * |
||
271 | * @param mixed string|array fields to return in response |
||
272 | * @param array optional data optional data to use instead of fields |
||
273 | * @return array $data |
||
274 | */ |
||
275 | public function castFields($fields = null, array $data = []): array |
||
304 | |||
305 | /** |
||
306 | * Cast the mapper data to an array and modify (for external clients typically) |
||
307 | * using the visible fields and names for export, converting dates to unixtime |
||
308 | * optionally pass in a comma (or space)-separated list of fields or an array of fields |
||
309 | * |
||
310 | * @param mixed string|array fields to return in response |
||
311 | * @param array optional data optional data to use instead of fields |
||
312 | * @return array $data |
||
313 | */ |
||
314 | public function exportArray($fields = null, array $data = []): array |
||
355 | |||
356 | |||
357 | /** |
||
358 | * Convert the mapper object to format suitable for JSON |
||
359 | * |
||
360 | * @param boolean $unmodified cast as public (visible) data or raw db data? |
||
361 | * @param mixed $fields optional string|array fields to include |
||
362 | * @return string json-encoded data |
||
363 | */ |
||
364 | public function exportJson(bool $unmodified = false, $fields = null): string |
||
368 | |||
369 | |||
370 | /** |
||
371 | * Set a field (default named uuid) to a UUID value if one is not present. |
||
372 | * |
||
373 | * @param string $field the name of the field to check and set |
||
374 | * @param int $len length of uuid to return |
||
375 | * @return null|string $uuid the new uuid generated |
||
376 | */ |
||
377 | public function setUUID(string $field = 'uuid', $len = 8) |
||
396 | |||
397 | |||
398 | /** |
||
399 | * Write data for audit logging |
||
400 | * |
||
401 | * @param $data array of data to audit log |
||
402 | * @return array $this->auditData return the updated audit data for the mapper |
||
403 | */ |
||
404 | public function audit(array $data = []): array |
||
409 | } |
||
410 |