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 AbstractOptDataMapper 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 AbstractOptDataMapper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class AbstractOptDataMapper extends AbstractDataMapper{ |
||
16 | |||
17 | protected $soft_delete_key; |
||
18 | |||
19 | protected $key; |
||
20 | |||
21 | protected $table; |
||
22 | |||
23 | protected $mapping_fields; |
||
24 | |||
25 | protected $mapping_fields_aliases; |
||
26 | |||
27 | protected $relations = []; |
||
28 | |||
29 | protected $DI; |
||
30 | |||
31 | function __construct(\League\Container\Container $DI, QueryBuilderInterface $adapter, $db_name = null) { |
||
39 | |||
40 | /** |
||
41 | * Установка поля для маппинга |
||
42 | */ |
||
43 | protected function addMappingField($alias,$field = null){ |
||
66 | |||
67 | /** |
||
68 | * Уставнока таблицы |
||
69 | */ |
||
70 | protected function setEntityTable() { |
||
73 | |||
74 | /** |
||
75 | * Установка ключа |
||
76 | */ |
||
77 | protected function getPrimaryKey() { |
||
80 | |||
81 | /** |
||
82 | * Устанвка поля для мягкого удаляения |
||
83 | */ |
||
84 | protected function setSoftDeleteKey() { |
||
87 | |||
88 | /** |
||
89 | * Подготавливаем конечный вариант Сущности |
||
90 | * |
||
91 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
92 | * @param array $row |
||
93 | * @return \Core\Infrastructure\EntityInterface |
||
94 | * @throws BadMethodCallException |
||
95 | */ |
||
96 | protected function buildEntity(EntityInterface $Entity, array $row){ |
||
139 | |||
140 | |||
141 | /** |
||
142 | * из объекта формирует массив |
||
143 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
144 | * @return \Core\Infrastructure\EntityInterface |
||
145 | * @throws BadMethodCallException |
||
146 | */ |
||
147 | protected function unbuildEntity(EntityInterface $Entity){ |
||
185 | |||
186 | |||
187 | public function getFieldAlias($field){ |
||
192 | |||
193 | /** |
||
194 | * Построение join-ов |
||
195 | */ |
||
196 | protected function setRelations(ISpecificationCriteria $Specification){ |
||
222 | |||
223 | /** |
||
224 | * На успешное сохранение |
||
225 | * @param \SimpleORM\EntityInterface $Entity |
||
226 | */ |
||
227 | View Code Duplication | protected function onSaveSuccess(EntityInterface $Entity){ |
|
239 | |||
240 | /** |
||
241 | * На успешное удаление |
||
242 | * @param \SimpleORM\EntityInterface $Entity |
||
243 | */ |
||
244 | View Code Duplication | protected function onDeleteSuccess(EntityInterface $Entity) { |
|
254 | } |
||
255 |
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.