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){ |
||
306 | |||
307 | |||
308 | /** |
||
309 | * из объекта формирует массив |
||
310 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
311 | * @return \Core\Infrastructure\EntityInterface |
||
312 | * @throws BadMethodCallException |
||
313 | */ |
||
314 | protected function unbuildEntity(EntityInterface $Entity){ |
||
352 | |||
353 | /** |
||
354 | * Установка поля для маппинга |
||
355 | */ |
||
356 | 5 | protected function addMappingField($alias,$field = null){ |
|
379 | |||
380 | |||
381 | |||
382 | /** |
||
383 | * Установка ключа |
||
384 | */ |
||
385 | 1 | public function getPrimaryKey() { |
|
388 | |||
389 | /** |
||
390 | * Устанвка поля для мягкого удаляения |
||
391 | */ |
||
392 | 1 | protected function setSoftDeleteKey() { |
|
395 | |||
396 | |||
397 | /** |
||
398 | * получение алиаса поля |
||
399 | * @param type $field |
||
400 | * @return type |
||
401 | */ |
||
402 | 1 | public function getFieldAlias($field){ |
|
405 | |||
406 | /** |
||
407 | * получение поля по алиасу |
||
408 | * @param type $alias |
||
409 | * @return type |
||
410 | */ |
||
411 | public function getAliasField($alias) |
||
415 | |||
416 | |||
417 | /** |
||
418 | * |
||
419 | * @param ISpecificationCriteria $specification |
||
420 | * @return type |
||
421 | */ |
||
422 | public function findBySpecification(ISpecificationCriteria $specification){ |
||
445 | |||
446 | /** |
||
447 | * Удаление записи |
||
448 | * @param EntityInterface $Entity |
||
449 | * @return boolean |
||
450 | */ |
||
451 | public function delete(EntityInterface $Entity) |
||
471 | |||
472 | public function findAllBySpecification(ISpecificationCriteria $specification) |
||
498 | |||
499 | public function findAll() |
||
503 | |||
504 | /** |
||
505 | * Выборка удаленных моделей |
||
506 | * @param ISpecificationCriteria $specification |
||
507 | */ |
||
508 | private function setSoftDelete(ISpecificationCriteria $specification){ |
||
516 | |||
517 | /** |
||
518 | * Построение join-ов |
||
519 | * |
||
520 | * @todo добавить типы связей |
||
521 | * has_many - один к многим (пост и коммеентарии) |
||
522 | * belongs_to - многие к многим (пользователь имет множество оплат одного заказа) |
||
523 | * has_one - один к одному |
||
524 | */ |
||
525 | protected function setRelations(ISpecificationCriteria $Specification){ |
||
554 | |||
555 | /** |
||
556 | * Использование join-ов |
||
557 | */ |
||
558 | public function useJoins() |
||
564 | |||
565 | public function withDelete(){ |
||
570 | |||
571 | /** |
||
572 | * Данные только в массиве |
||
573 | * @return \SimpleORM\AbstractDataMapper |
||
574 | */ |
||
575 | public function resultArray(){ |
||
580 | |||
581 | } |
||
582 |
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.