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 AbstractDataMapper 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 AbstractDataMapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | abstract class AbstractDataMapper implements RepositoryInterface, MapperInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * адаптер для работы с бд |
||
| 14 | * @var type |
||
| 15 | */ |
||
| 16 | protected $adapter; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * таблица для сущности |
||
| 20 | * @var type |
||
| 21 | */ |
||
| 22 | protected $entityTable; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * первичный ключ |
||
| 26 | * @var type |
||
| 27 | */ |
||
| 28 | protected $key; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Использование join при выборке |
||
| 32 | * @var type |
||
| 33 | */ |
||
| 34 | protected $use_joins = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Использование мягкое удаление |
||
| 38 | * @var type |
||
| 39 | */ |
||
| 40 | protected $use_delete = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * поле для мягкого удаления |
||
| 44 | * @var type |
||
| 45 | */ |
||
| 46 | protected $soft_delete_key; |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * поля сущности |
||
| 51 | * @var type |
||
| 52 | */ |
||
| 53 | protected $mapping_fields = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * псевдонимы полей сущности |
||
| 57 | * @var type |
||
| 58 | */ |
||
| 59 | protected $mapping_fields_aliases = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * связи с другими мапперами |
||
| 63 | * @var type |
||
| 64 | */ |
||
| 65 | protected $relations = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Контейнер |
||
| 69 | * @var League\Container\Container |
||
| 70 | */ |
||
| 71 | protected $DI; |
||
| 72 | |||
| 73 | function __construct(\League\Container\Container $DI, QueryBuilderInterface $adapter, $db_name = null) { |
||
| 88 | |||
| 89 | abstract protected function setMappingFields(); |
||
| 90 | |||
| 91 | public function getAdapter() { |
||
| 94 | |||
| 95 | public function setAdapter(QueryBuilderInterface $adapter){ |
||
| 98 | |||
| 99 | |||
| 100 | protected function getEntityTable() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Уставнока таблицы |
||
| 106 | */ |
||
| 107 | protected function setEntityTable($db_name) { |
||
| 110 | |||
| 111 | |||
| 112 | public function findById($id) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Cохранение сущности |
||
| 121 | * @param EntityInterface $Entity |
||
| 122 | */ |
||
| 123 | public function save(EntityInterface $Entity) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * На успешное сохранение |
||
| 160 | * @param \SimpleORM\EntityInterface $Entity |
||
| 161 | */ |
||
| 162 | View Code Duplication | protected function onSaveSuccess(EntityInterface $Entity){ |
|
| 174 | |||
| 175 | /** |
||
| 176 | * На успешное удаление |
||
| 177 | * @param \SimpleORM\EntityInterface $Entity |
||
| 178 | */ |
||
| 179 | View Code Duplication | protected function onDeleteSuccess(EntityInterface $Entity) { |
|
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Подготавливаем конечный вариант Сущности |
||
| 194 | * |
||
| 195 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
| 196 | * @param array $row |
||
| 197 | * @return \Core\Infrastructure\EntityInterface |
||
| 198 | * @throws BadMethodCallException |
||
| 199 | */ |
||
| 200 | protected function buildEntity(EntityInterface $Entity, array $row){ |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * из объекта формирует массив |
||
| 247 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
| 248 | * @return \Core\Infrastructure\EntityInterface |
||
| 249 | * @throws BadMethodCallException |
||
| 250 | */ |
||
| 251 | protected function unbuildEntity(EntityInterface $Entity){ |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Установка поля для маппинга |
||
| 292 | */ |
||
| 293 | 5 | protected function addMappingField($alias,$field = null){ |
|
| 316 | |||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * Установка ключа |
||
| 321 | */ |
||
| 322 | protected function getPrimaryKey() { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Устанвка поля для мягкого удаляения |
||
| 328 | */ |
||
| 329 | protected function setSoftDeleteKey() { |
||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | public function getFieldAlias($field){ |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * |
||
| 344 | * @param ISpecificationCriteria $specification |
||
| 345 | * @return type |
||
| 346 | */ |
||
| 347 | public function findBySpecification(ISpecificationCriteria $specification){ |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Удаление записи |
||
| 367 | * @param EntityInterface $Entity |
||
| 368 | * @return boolean |
||
| 369 | */ |
||
| 370 | public function delete(EntityInterface $Entity) |
||
| 390 | |||
| 391 | public function findAllBySpecification(ISpecificationCriteria $specification) |
||
| 413 | |||
| 414 | public function findAll() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Выборка удаленных моделей |
||
| 421 | * @param ISpecificationCriteria $specification |
||
| 422 | */ |
||
| 423 | private function setSoftDelete(ISpecificationCriteria $specification){ |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Построение join-ов |
||
| 434 | */ |
||
| 435 | protected function setRelations(ISpecificationCriteria $Specification){ |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Использование join-ов |
||
| 464 | */ |
||
| 465 | public function useJoins() |
||
| 471 | |||
| 472 | public function withDelete(){ |
||
| 477 | |||
| 478 | } |
||
| 479 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.