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 | use TraitDataMapperEvent; |
||
| 14 | |||
| 15 | |||
| 16 | /** |
||
| 17 | * адаптер для работы с бд |
||
| 18 | * @var type |
||
| 19 | */ |
||
| 20 | protected $adapter; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * таблица для сущности |
||
| 24 | * @var type |
||
| 25 | */ |
||
| 26 | protected $entityTable; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * первичный ключ |
||
| 30 | * @var type |
||
| 31 | */ |
||
| 32 | protected $key; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Использование join при выборке |
||
| 36 | * @var type |
||
| 37 | */ |
||
| 38 | protected $use_joins = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Использование мягкое удаление |
||
| 42 | * @var type |
||
| 43 | */ |
||
| 44 | protected $use_delete = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * поле для мягкого удаления |
||
| 48 | * @var type |
||
| 49 | */ |
||
| 50 | protected $soft_delete_key; |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * поля сущности |
||
| 55 | * @var type |
||
| 56 | */ |
||
| 57 | protected $mapping_fields = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * псевдонимы полей сущности |
||
| 61 | * @var type |
||
| 62 | */ |
||
| 63 | protected $mapping_fields_aliases = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * связи с другими мапперами |
||
| 67 | * @var type |
||
| 68 | */ |
||
| 69 | protected $relations = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Контейнер |
||
| 73 | * @var League\Container\Container |
||
| 74 | */ |
||
| 75 | protected $DI; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Возврат данных в массиве |
||
| 79 | * @var type |
||
| 80 | */ |
||
| 81 | protected $result_array = false; |
||
| 82 | |||
| 83 | function __construct(\League\Container\Container $DI, QueryBuilderInterface $adapter, $db_name = null) { |
||
| 98 | |||
| 99 | abstract protected function setMappingFields(); |
||
| 100 | |||
| 101 | public function getAdapter() { |
||
| 104 | |||
| 105 | public function setAdapter(QueryBuilderInterface $adapter){ |
||
| 108 | |||
| 109 | |||
| 110 | protected function getEntityTable() { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Уставнока таблицы |
||
| 116 | */ |
||
| 117 | protected function setEntityTable($db_name) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Получение записи по ключу |
||
| 123 | * @param type $id |
||
| 124 | * @return type |
||
| 125 | */ |
||
| 126 | public function findById($id) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Сохранение сущности без спобытий |
||
| 135 | * @param \SimpleORM\EntityInterface $Entity |
||
| 136 | */ |
||
| 137 | public function saveWithoutEvents(EntityInterface $Entity) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Cохранение сущности |
||
| 173 | * @param EntityInterface $Entity |
||
| 174 | */ |
||
| 175 | public function save(EntityInterface $Entity) |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * получение мапперов в порядке их использования с учетом вложенности |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | protected function createListRelation(){ |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Выстроивает порядок использования мапперов в иерархии |
||
| 205 | * @param array $rel_map |
||
| 206 | * @param type $rel_list |
||
| 207 | */ |
||
| 208 | protected function createListRelationReq(array $rel_map,&$rel_list,$obj_parent_link = null) { |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * получить связи |
||
| 227 | */ |
||
| 228 | protected function getRelations(){ |
||
| 242 | |||
| 243 | |||
| 244 | |||
| 245 | |||
| 246 | |||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * Подготавливаем конечный вариант Сущности |
||
| 251 | * |
||
| 252 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
| 253 | * @param array $row |
||
| 254 | * @return \Core\Infrastructure\EntityInterface |
||
| 255 | * @throws BadMethodCallException |
||
| 256 | */ |
||
| 257 | protected function buildEntity(EntityInterface $Entity, array $row){ |
||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * из объекта формирует массив |
||
| 320 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
| 321 | * @return \Core\Infrastructure\EntityInterface |
||
| 322 | * @throws BadMethodCallException |
||
| 323 | */ |
||
| 324 | protected function unbuildEntity(EntityInterface $Entity){ |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Установка поля для маппинга |
||
| 372 | */ |
||
| 373 | 5 | protected function addMappingField($alias,$field = null){ |
|
| 396 | |||
| 397 | |||
| 398 | |||
| 399 | /** |
||
| 400 | * Установка ключа |
||
| 401 | */ |
||
| 402 | 1 | public function getPrimaryKey() { |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Устанвка поля для мягкого удаляения |
||
| 408 | */ |
||
| 409 | 1 | protected function setSoftDeleteKey() { |
|
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * получение алиаса поля |
||
| 416 | * @param type $field |
||
| 417 | * @return type |
||
| 418 | */ |
||
| 419 | 1 | public function getFieldAlias($field){ |
|
| 422 | |||
| 423 | /** |
||
| 424 | * получение поля по алиасу |
||
| 425 | * @param type $alias |
||
| 426 | * @return type |
||
| 427 | */ |
||
| 428 | public function getAliasField($alias) |
||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * |
||
| 436 | * @param ISpecificationCriteria $specification |
||
| 437 | * @return type |
||
| 438 | */ |
||
| 439 | public function findBySpecification(ISpecificationCriteria $specification){ |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Удаление записи |
||
| 465 | * @param EntityInterface $Entity |
||
| 466 | * @return boolean |
||
| 467 | */ |
||
| 468 | public function delete(EntityInterface $Entity) |
||
| 488 | |||
| 489 | public function findAllBySpecification(ISpecificationCriteria $specification) |
||
| 515 | |||
| 516 | public function findAll() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Выборка удаленных моделей |
||
| 523 | * @param ISpecificationCriteria $specification |
||
| 524 | */ |
||
| 525 | protected function setSoftDelete(ISpecificationCriteria $specification){ |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Построение join-ов |
||
| 536 | * |
||
| 537 | * @todo добавить типы связей |
||
| 538 | * has_many - один к многим (пост и коммеентарии) |
||
| 539 | * belongs_to - многие к многим (пользователь имет множество оплат одного заказа) |
||
| 540 | * has_one - один к одному |
||
| 541 | */ |
||
| 542 | protected function setRelations(ISpecificationCriteria $Specification){ |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Использование join-ов |
||
| 576 | */ |
||
| 577 | public function useJoins() |
||
| 583 | |||
| 584 | public function withDelete(){ |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Данные только в массиве |
||
| 592 | * @return \SimpleORM\AbstractDataMapper |
||
| 593 | */ |
||
| 594 | public function resultArray(){ |
||
| 599 | |||
| 600 | } |
||
| 601 |
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.