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 |
||
| 16 | abstract class Mapper extends API |
||
| 17 | { |
||
| 18 | use Traits\Validation, |
||
| 19 | Traits\SearchController; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * For admin listing and search results |
||
| 23 | */ |
||
| 24 | use Traits\SearchController; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var object database class |
||
| 28 | */ |
||
| 29 | protected $db; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string table in the db |
||
| 33 | */ |
||
| 34 | protected $table = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string class name |
||
| 38 | */ |
||
| 39 | protected $mapperClass; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \FFMVC\Mappers\Mapper mapper for class |
||
| 43 | */ |
||
| 44 | protected $mapper; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * is the user authorised for access? |
||
| 48 | * |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | protected $isAuthorised = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * is the user authorised for access to the object type? |
||
| 55 | * |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | protected $adminOnly = true; |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * initialize |
||
| 63 | * @param \Base $f3 |
||
| 64 | */ |
||
| 65 | public function __construct(\Base $f3) |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the associated mapper for the table |
||
| 93 | * |
||
| 94 | * @return |
||
| 95 | */ |
||
| 96 | public function &getMapper() |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * Check permissions and load the mapper with the object in the URL param @id |
||
| 104 | * for the user |
||
| 105 | * |
||
| 106 | * @param \Base $f3 |
||
| 107 | * @param array $params |
||
| 108 | * @param string $idField the field used for the unique id to load by |
||
| 109 | * @param string|null $defaultId defaule value to use if not found |
||
| 110 | * @return null|array|boolean|\FFMVC\Mappers\Mapper |
||
| 111 | */ |
||
| 112 | public function getIdObjectIfUser(\Base $f3, array $params, string $idField = 'uuid', $defaultId = null) |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * Check permissions and load the mapper with the object in the URL param @id |
||
| 148 | * if the user is an admin |
||
| 149 | * |
||
| 150 | * @param \Base $f3 |
||
| 151 | * @param array $params |
||
| 152 | * @param string $idField the field used for the unique id to load by |
||
| 153 | * @param string|null $defaultId defaule value to use if not found |
||
| 154 | * @return null|array|boolean|\FFMVC\Mappers\Mapper |
||
| 155 | */ |
||
| 156 | public function getIdObjectIfAdmin(\Base $f3, array $params, string $idField = 'uuid', $defaultId = null) |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Display the data item |
||
| 195 | * |
||
| 196 | * @param \Base $f3 |
||
| 197 | * @param array $params |
||
| 198 | * @return null|array|boolean |
||
| 199 | */ |
||
| 200 | public function get(\Base $f3, array $params) |
||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * Replace data |
||
| 218 | * |
||
| 219 | * @param \Base $f3 |
||
| 220 | * @param array $params |
||
| 221 | * @return null|array|boolean |
||
| 222 | */ |
||
| 223 | public function put(\Base $f3, array $params) |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * Create new data |
||
| 241 | * |
||
| 242 | * @param \Base $f3 |
||
| 243 | * @return null|array|boolean |
||
| 244 | */ |
||
| 245 | public function post(\Base $f3) |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Update data |
||
| 263 | * |
||
| 264 | * @param \Base $f3 |
||
| 265 | * @param array $params |
||
| 266 | * @return null|array|boolean |
||
| 267 | */ |
||
| 268 | public function patch(\Base $f3, array $params) |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * Delete the data object indicated by @id in the request |
||
| 286 | * |
||
| 287 | * @param \Base $f3 |
||
| 288 | * @param array $params |
||
| 289 | * @return null|array|boolean |
||
| 290 | */ |
||
| 291 | public function delete(\Base $f3, array $params) |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * list objects (list is a reserved keyword) |
||
| 315 | * |
||
| 316 | * @param \Base $f3 |
||
| 317 | * @return null|array|boolean |
||
| 318 | */ |
||
| 319 | public function listingAdmin(\Base $f3) |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * list objects (list is a reserved keyword) |
||
| 334 | * |
||
| 335 | * @param \Base $f3 |
||
| 336 | * @param array $params |
||
| 337 | * @return array|boolean|null |
||
| 338 | */ |
||
| 339 | public function listing(\Base $f3, array $params) |
||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * search objects |
||
| 359 | * |
||
| 360 | * @param \Base $f3 |
||
| 361 | * @return null|array|boolean |
||
| 362 | */ |
||
| 363 | public function searchAdmin(\Base $f3) |
||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * search objects |
||
| 378 | * |
||
| 379 | * @param \Base $f3 |
||
| 380 | * @param array $params |
||
| 381 | * @return null|array|boolean |
||
| 382 | */ |
||
| 383 | public function search(\Base $f3, array $params) |
||
| 399 | |||
| 400 | } |
||
| 401 |