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){ |
||
69 | |||
70 | /** |
||
71 | * Уставнока таблицы |
||
72 | */ |
||
73 | protected function setEntityTable() { |
||
76 | |||
77 | /** |
||
78 | * Установка ключа |
||
79 | */ |
||
80 | protected function getPrimaryKey() { |
||
83 | |||
84 | /** |
||
85 | * Устанвка поля для мягкого удаляения |
||
86 | */ |
||
87 | protected function setSoftDeleteKey() { |
||
90 | |||
91 | /** |
||
92 | * Подготавливаем конечный вариант Сущности |
||
93 | * |
||
94 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
95 | * @param array $row |
||
96 | * @return \Core\Infrastructure\EntityInterface |
||
97 | * @throws BadMethodCallException |
||
98 | */ |
||
99 | protected function buildEntity(EntityInterface $Entity, array $row){ |
||
142 | |||
143 | |||
144 | /** |
||
145 | * из объекта формирует массив |
||
146 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
147 | * @return \Core\Infrastructure\EntityInterface |
||
148 | * @throws BadMethodCallException |
||
149 | */ |
||
150 | protected function unbuildEntity(EntityInterface $Entity){ |
||
188 | |||
189 | |||
190 | public function getFieldAlias($field){ |
||
195 | |||
196 | /** |
||
197 | * Построение join-ов |
||
198 | */ |
||
199 | protected function setRelations(ISpecificationCriteria $Specification){ |
||
225 | |||
226 | /** |
||
227 | * На успешное сохранение |
||
228 | * @param \SimpleORM\EntityInterface $Entity |
||
229 | */ |
||
230 | View Code Duplication | protected function onSaveSuccess(EntityInterface $Entity){ |
|
242 | |||
243 | /** |
||
244 | * На успешное удаление |
||
245 | * @param \SimpleORM\EntityInterface $Entity |
||
246 | */ |
||
247 | View Code Duplication | protected function onDeleteSuccess(EntityInterface $Entity) { |
|
257 | } |
||
258 |
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.