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 |
||
| 26 | abstract class Mapper extends \DB\SQL\Mapper |
||
| 27 | { |
||
| 28 | use Traits\Logger, |
||
| 29 | 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 |
||
| 52 | */ |
||
| 53 | protected $table; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string $uuid the fieldname used for the uuid |
||
| 57 | */ |
||
| 58 | protected $uuidField = 'uuid'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var boolean $valid the data after validation is valid? |
||
| 62 | */ |
||
| 63 | protected $valid = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * initialize with array of params |
||
| 67 | * |
||
| 68 | */ |
||
| 69 | public function __construct(array $params = []) |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * return string representation of class - json of data |
||
| 117 | * |
||
| 118 | * @param string |
||
| 119 | */ |
||
| 120 | public function __toString(): string |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * return array representation of class - json of data |
||
| 128 | * |
||
| 129 | * @param array |
||
| 130 | */ |
||
| 131 | public function __toArray(): array |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Cast the mapper data to an array using only provided fields |
||
| 138 | * |
||
| 139 | * @param mixed string|array fields to return in response |
||
| 140 | * @param array optional data optional data to use instead of fields |
||
| 141 | * @return array $data |
||
| 142 | */ |
||
| 143 | public function castFields($fields = null, array $data = []): array |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Cast the mapper data to an array and modify (for external clients typically) |
||
| 175 | * using the visible fields and names for export, converting dates to unixtime |
||
| 176 | * optionally pass in a comma (or space)-separated list of fields or an array of fields |
||
| 177 | * |
||
| 178 | * @param mixed string|array fields to return in response |
||
| 179 | * @param array optional data optional data to use instead of fields |
||
| 180 | * @return array $data |
||
| 181 | */ |
||
| 182 | public function exportArray($fields = null, array $data = []): array |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * Convert the mapper object to format suitable for JSON |
||
| 227 | * |
||
| 228 | * @param boolean $unmodified cast as public (visible) data or raw db data? |
||
| 229 | * @param mixed $fields optional string|array fields to include |
||
| 230 | * @return string json-encoded data |
||
| 231 | */ |
||
| 232 | public function exportJson(bool $unmodified = false, $fields = null): string |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * Set a field (default named uuid) to a UUID value if one is not present. |
||
| 240 | * |
||
| 241 | * @param string $field the name of the field to check and set |
||
| 242 | * @return null|string $uuid the new uuid generated |
||
| 243 | */ |
||
| 244 | public function setUUID(string $field = 'uuid') |
||
| 263 | } |
||
| 264 |