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 |
||
29 | abstract class Mapper extends \DB\SQL\Mapper |
||
30 | { |
||
31 | use Traits\Validation; |
||
32 | |||
33 | /** |
||
34 | * Fields and their visibility to clients, boolean or string of visible field name |
||
35 | * all fields are visible except where 'false' |
||
36 | * |
||
37 | * @var array $fieldsVisible |
||
38 | */ |
||
39 | protected $fieldsVisible = []; |
||
40 | |||
41 | /** |
||
42 | * Fields that are editable to clients, boolean or string of visible field name |
||
43 | * |
||
44 | * @var array $fieldsEditable |
||
45 | */ |
||
46 | protected $fieldsEditable = []; |
||
47 | |||
48 | /** |
||
49 | * @var object database class |
||
50 | */ |
||
51 | protected $db; |
||
52 | |||
53 | /** |
||
54 | * @var string $table for the mapper - this string gets automatically quoted |
||
55 | */ |
||
56 | protected $table; |
||
57 | |||
58 | /** |
||
59 | * @var string $mapperName name for the mapper |
||
60 | */ |
||
61 | protected $mapperName; |
||
62 | |||
63 | /** |
||
64 | * @var string $uuid the fieldname used for the uuid |
||
65 | */ |
||
66 | protected $uuidField = 'uuid'; |
||
67 | |||
68 | /** |
||
69 | * @var boolean $valid the data after validation is valid? |
||
70 | */ |
||
71 | protected $valid = null; |
||
72 | |||
73 | /** |
||
74 | * @var array $originalData the original data when object created/loaded |
||
75 | */ |
||
76 | protected $originalData = []; |
||
77 | |||
78 | /** |
||
79 | * @var array $auditData data to write to audit log |
||
80 | */ |
||
81 | protected $auditData = []; |
||
82 | |||
83 | /** |
||
84 | * @var boolean $initValidation automatically append validation settings for fields $this->validationRules/Default? |
||
85 | */ |
||
86 | protected $initValidation = true; |
||
87 | |||
88 | /** |
||
89 | * initialize with array of params |
||
90 | * |
||
91 | */ |
||
92 | public function __construct() |
||
105 | |||
106 | /** |
||
107 | * Load by internal integer ID or by UUID if no array passed or reload if null |
||
108 | * |
||
109 | * @param $filter string|array |
||
110 | * @param $options array |
||
111 | * @param $ttl int |
||
112 | * @see DB\Cursor::load($filter = NULL, array $options = NULL, $ttl = 0) |
||
113 | * @return array|FALSE |
||
114 | */ |
||
115 | public function load($filter = NULL, array $options = null, $ttl = 0) { |
||
128 | |||
129 | /** |
||
130 | * Initialise automatic validation settings for fields |
||
131 | */ |
||
132 | public function initValidation() |
||
195 | |||
196 | /** |
||
197 | * Initialise hooks for the mapper object actions |
||
198 | */ |
||
199 | public function setupHooks() |
||
253 | |||
254 | |||
255 | /** |
||
256 | * Quote a database fieldname/key |
||
257 | * |
||
258 | * @param string $key String to quote as a database key |
||
259 | * @param string $key |
||
260 | * @param return string |
||
261 | */ |
||
262 | public function quotekey(string $key): string |
||
269 | |||
270 | |||
271 | /** |
||
272 | * Quote a database value |
||
273 | * |
||
274 | * @param mixed $value Value to quote |
||
275 | * @param mixed $value |
||
276 | * @param null|return string |
||
277 | */ |
||
278 | public function quote($value) |
||
282 | |||
283 | |||
284 | /** |
||
285 | * return string representation of class - json of data |
||
286 | * |
||
287 | * @param string |
||
288 | */ |
||
289 | public function __toString(): string |
||
293 | |||
294 | |||
295 | /** |
||
296 | * return array representation of class - json of data |
||
297 | * |
||
298 | * @param array |
||
299 | */ |
||
300 | public function __toArray(): array |
||
304 | |||
305 | /** |
||
306 | * Cast the mapper data to an array using only provided fields |
||
307 | * |
||
308 | * @param mixed string|array fields to return in response |
||
309 | * @param array optional data optional data to use instead of fields |
||
310 | * @return array $data |
||
311 | */ |
||
312 | public function castFields($fields = null, array $data = []): array |
||
341 | |||
342 | /** |
||
343 | * Cast the mapper data to an array and modify (for external clients typically) |
||
344 | * using the visible fields and names for export, converting dates to unixtime |
||
345 | * optionally pass in a comma (or space)-separated list of fields or an array of fields |
||
346 | * |
||
347 | * @param mixed string|array fields to return in response |
||
348 | * @param array optional data optional data to use instead of fields |
||
349 | * @return array $data |
||
350 | */ |
||
351 | public function exportArray($fields = null, array $data = []): array |
||
392 | |||
393 | |||
394 | /** |
||
395 | * Convert the mapper object to format suitable for JSON |
||
396 | * |
||
397 | * @param boolean $unmodified cast as public (visible) data or raw db data? |
||
398 | * @param mixed $fields optional string|array fields to include |
||
399 | * @return string json-encoded data |
||
400 | */ |
||
401 | public function exportJson(bool $unmodified = false, $fields = null): string |
||
405 | |||
406 | |||
407 | /** |
||
408 | * Set a field (default named uuid) to a UUID value if one is not present. |
||
409 | * |
||
410 | * @param string $field the name of the field to check and set |
||
411 | * @param int $len length of uuid to return |
||
412 | * @return null|string $uuid the new uuid generated |
||
413 | */ |
||
414 | public function setUUID(string $field = 'uuid', $len = 8) |
||
432 | |||
433 | |||
434 | /** |
||
435 | * Write data for audit logging |
||
436 | * |
||
437 | * @param $data array of data to audit log |
||
438 | * @return array $this->auditData return the updated audit data for the mapper |
||
439 | */ |
||
440 | public function audit(array $data = []): array |
||
445 | |||
446 | } |
||
447 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.