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 |
||
27 | abstract class Mapper extends \DB\SQL\Mapper |
||
28 | { |
||
29 | use Traits\Validation; |
||
30 | |||
31 | /** |
||
32 | * Fields and their visibility to clients, boolean or string of visible field name |
||
33 | * |
||
34 | * @var array $fieldsVisible |
||
35 | */ |
||
36 | protected $fieldsVisible = []; |
||
37 | |||
38 | /** |
||
39 | * Fields that are editable to clients, boolean or string of visible field name |
||
40 | * |
||
41 | * @var array $fieldsEditable |
||
42 | */ |
||
43 | protected $fieldsEditable = []; |
||
44 | |||
45 | /** |
||
46 | * @var object database class |
||
47 | */ |
||
48 | protected $db; |
||
49 | |||
50 | /** |
||
51 | * @var string $table for the mapper - this string gets automatically quoted |
||
52 | */ |
||
53 | protected $table; |
||
54 | |||
55 | /** |
||
56 | * @var string $mapperName name for the mapper |
||
57 | */ |
||
58 | protected $mapperName; |
||
59 | |||
60 | /** |
||
61 | * @var string $uuid the fieldname used for the uuid |
||
62 | */ |
||
63 | protected $uuidField = 'uuid'; |
||
64 | |||
65 | /** |
||
66 | * @var boolean $valid the data after validation is valid? |
||
67 | */ |
||
68 | protected $valid = null; |
||
69 | |||
70 | /** |
||
71 | * @var array $originalData the original data when object created/loaded |
||
72 | */ |
||
73 | protected $originalData = []; |
||
74 | |||
75 | /** |
||
76 | * @var array $auditData data to write to audit log |
||
77 | */ |
||
78 | protected $auditData = []; |
||
79 | |||
80 | /** |
||
81 | * initialize with array of params |
||
82 | * |
||
83 | */ |
||
84 | public function __construct(array $params = []) |
||
162 | |||
163 | |||
164 | /** |
||
165 | * return string representation of class - json of data |
||
166 | * |
||
167 | * @param string |
||
168 | */ |
||
169 | public function __toString(): string |
||
173 | |||
174 | |||
175 | /** |
||
176 | * return array representation of class - json of data |
||
177 | * |
||
178 | * @param array |
||
179 | */ |
||
180 | public function __toArray(): array |
||
184 | |||
185 | /** |
||
186 | * Cast the mapper data to an array using only provided fields |
||
187 | * |
||
188 | * @param mixed string|array fields to return in response |
||
189 | * @param array optional data optional data to use instead of fields |
||
190 | * @return array $data |
||
191 | */ |
||
192 | public function castFields($fields = null, array $data = []): array |
||
221 | |||
222 | /** |
||
223 | * Cast the mapper data to an array and modify (for external clients typically) |
||
224 | * using the visible fields and names for export, converting dates to unixtime |
||
225 | * optionally pass in a comma (or space)-separated list of fields or an array of fields |
||
226 | * |
||
227 | * @param mixed string|array fields to return in response |
||
228 | * @param array optional data optional data to use instead of fields |
||
229 | * @return array $data |
||
230 | */ |
||
231 | public function exportArray($fields = null, array $data = []): array |
||
272 | |||
273 | |||
274 | /** |
||
275 | * Convert the mapper object to format suitable for JSON |
||
276 | * |
||
277 | * @param boolean $unmodified cast as public (visible) data or raw db data? |
||
278 | * @param mixed $fields optional string|array fields to include |
||
279 | * @return string json-encoded data |
||
280 | */ |
||
281 | public function exportJson(bool $unmodified = false, $fields = null): string |
||
285 | |||
286 | |||
287 | /** |
||
288 | * Set a field (default named uuid) to a UUID value if one is not present. |
||
289 | * |
||
290 | * @param string $field the name of the field to check and set |
||
291 | * @return null|string $uuid the new uuid generated |
||
292 | */ |
||
293 | public function setUUID(string $field = 'uuid') |
||
312 | |||
313 | |||
314 | /** |
||
315 | * Write data for audit logging |
||
316 | * |
||
317 | * @param $data array of data to audit log |
||
318 | * @return $this->auditData return the updated audit data for the mapper |
||
|
|||
319 | */ |
||
320 | public function audit(array $data = []): array |
||
325 | |||
326 | } |
||
327 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.