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 or reload if null |
||
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) { |
||
127 | |||
128 | /** |
||
129 | * Initialise automatic validation settings for fields |
||
130 | */ |
||
131 | public function initValidation() |
||
191 | |||
192 | /** |
||
193 | * Initialise hooks for the mapper object actions |
||
194 | */ |
||
195 | public function setupHooks() |
||
249 | |||
250 | /** |
||
251 | * return string representation of class - json of data |
||
252 | * |
||
253 | * @param string |
||
254 | */ |
||
255 | public function __toString(): string |
||
259 | |||
260 | |||
261 | /** |
||
262 | * return array representation of class - json of data |
||
263 | * |
||
264 | * @param array |
||
265 | */ |
||
266 | public function __toArray(): array |
||
270 | |||
271 | /** |
||
272 | * Cast the mapper data to an array using only provided fields |
||
273 | * |
||
274 | * @param mixed string|array fields to return in response |
||
275 | * @param array optional data optional data to use instead of fields |
||
276 | * @return array $data |
||
277 | */ |
||
278 | public function castFields($fields = null, array $data = []): array |
||
307 | |||
308 | /** |
||
309 | * Cast the mapper data to an array and modify (for external clients typically) |
||
310 | * using the visible fields and names for export, converting dates to unixtime |
||
311 | * optionally pass in a comma (or space)-separated list of fields or an array of fields |
||
312 | * |
||
313 | * @param mixed string|array fields to return in response |
||
314 | * @param array optional data optional data to use instead of fields |
||
315 | * @return array $data |
||
316 | */ |
||
317 | public function exportArray($fields = null, array $data = []): array |
||
358 | |||
359 | |||
360 | /** |
||
361 | * Convert the mapper object to format suitable for JSON |
||
362 | * |
||
363 | * @param boolean $unmodified cast as public (visible) data or raw db data? |
||
364 | * @param mixed $fields optional string|array fields to include |
||
365 | * @return string json-encoded data |
||
366 | */ |
||
367 | public function exportJson(bool $unmodified = false, $fields = null): string |
||
371 | |||
372 | |||
373 | /** |
||
374 | * Set a field (default named uuid) to a UUID value if one is not present. |
||
375 | * |
||
376 | * @param string $field the name of the field to check and set |
||
377 | * @param int $len length of uuid to return |
||
378 | * @return null|string $uuid the new uuid generated |
||
379 | */ |
||
380 | public function setUUID(string $field = 'uuid', $len = 8) |
||
399 | |||
400 | |||
401 | /** |
||
402 | * Write data for audit logging |
||
403 | * |
||
404 | * @param $data array of data to audit log |
||
405 | * @return array $this->auditData return the updated audit data for the mapper |
||
406 | */ |
||
407 | public function audit(array $data = []): array |
||
412 | } |
||
413 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.