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()  | 
            ||
| 192 | |||
| 193 | /**  | 
            ||
| 194 | * Initialise hooks for the mapper object actions  | 
            ||
| 195 | */  | 
            ||
| 196 | public function setupHooks()  | 
            ||
| 250 | |||
| 251 | |||
| 252 | /**  | 
            ||
| 253 | * Quote a database fieldname/key  | 
            ||
| 254 | *  | 
            ||
| 255 | * @param string $key String to quote as a database key  | 
            ||
| 256 | * @param string $key  | 
            ||
| 257 | */  | 
            ||
| 258 | public function quotekey(string $key): string  | 
            ||
| 265 | |||
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * Quote a database value  | 
            ||
| 269 | *  | 
            ||
| 270 | * @param mixed $value Value to quote  | 
            ||
| 271 | * @param mixed $value  | 
            ||
| 272 | */  | 
            ||
| 273 | public function quote($value)  | 
            ||
| 277 | |||
| 278 | |||
| 279 | /**  | 
            ||
| 280 | * return string representation of class - json of data  | 
            ||
| 281 | *  | 
            ||
| 282 | * @param string  | 
            ||
| 283 | */  | 
            ||
| 284 | public function __toString(): string  | 
            ||
| 288 | |||
| 289 | |||
| 290 | /**  | 
            ||
| 291 | * return array representation of class - json of data  | 
            ||
| 292 | *  | 
            ||
| 293 | * @param array  | 
            ||
| 294 | */  | 
            ||
| 295 | public function __toArray(): array  | 
            ||
| 299 | |||
| 300 | /**  | 
            ||
| 301 | * Cast the mapper data to an array using only provided fields  | 
            ||
| 302 | *  | 
            ||
| 303 | * @param mixed string|array fields to return in response  | 
            ||
| 304 | * @param array optional data optional data to use instead of fields  | 
            ||
| 305 | * @return array $data  | 
            ||
| 306 | */  | 
            ||
| 307 | public function castFields($fields = null, array $data = []): array  | 
            ||
| 336 | |||
| 337 | /**  | 
            ||
| 338 | * Cast the mapper data to an array and modify (for external clients typically)  | 
            ||
| 339 | * using the visible fields and names for export, converting dates to unixtime  | 
            ||
| 340 | * optionally pass in a comma (or space)-separated list of fields or an array of fields  | 
            ||
| 341 | *  | 
            ||
| 342 | * @param mixed string|array fields to return in response  | 
            ||
| 343 | * @param array optional data optional data to use instead of fields  | 
            ||
| 344 | * @return array $data  | 
            ||
| 345 | */  | 
            ||
| 346 | public function exportArray($fields = null, array $data = []): array  | 
            ||
| 387 | |||
| 388 | |||
| 389 | /**  | 
            ||
| 390 | * Convert the mapper object to format suitable for JSON  | 
            ||
| 391 | *  | 
            ||
| 392 | * @param boolean $unmodified cast as public (visible) data or raw db data?  | 
            ||
| 393 | * @param mixed $fields optional string|array fields to include  | 
            ||
| 394 | * @return string json-encoded data  | 
            ||
| 395 | */  | 
            ||
| 396 | public function exportJson(bool $unmodified = false, $fields = null): string  | 
            ||
| 400 | |||
| 401 | |||
| 402 | /**  | 
            ||
| 403 | * Set a field (default named uuid) to a UUID value if one is not present.  | 
            ||
| 404 | *  | 
            ||
| 405 | * @param string $field the name of the field to check and set  | 
            ||
| 406 | * @param int $len length of uuid to return  | 
            ||
| 407 | * @return null|string $uuid the new uuid generated  | 
            ||
| 408 | */  | 
            ||
| 409 | public function setUUID(string $field = 'uuid', $len = 8)  | 
            ||
| 427 | |||
| 428 | |||
| 429 | /**  | 
            ||
| 430 | * Write data for audit logging  | 
            ||
| 431 | *  | 
            ||
| 432 | * @param $data array of data to audit log  | 
            ||
| 433 | * @return array $this->auditData return the updated audit data for the mapper  | 
            ||
| 434 | */  | 
            ||
| 435 | public function audit(array $data = []): array  | 
            ||
| 440 | |||
| 441 | }  | 
            ||
| 442 | 
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
@returnannotation as described here.