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 | protected $db; |
||
13 | |||
14 | protected $entityTable; |
||
15 | |||
16 | protected $key; |
||
17 | |||
18 | |||
19 | /** |
||
20 | * Использование join при выборке |
||
21 | * @var type |
||
22 | */ |
||
23 | protected $use_joins = false; |
||
24 | |||
25 | protected $use_delete = false; |
||
26 | |||
27 | public function __construct( QueryBuilderInterface $adapter, $db_name = null) {// \CI_DB_mysqli_driver //DatabaseAdapterInterface |
||
38 | |||
39 | |||
40 | |||
41 | function getEntityTable() { |
||
44 | |||
45 | public function getAdapter() { |
||
48 | |||
49 | public function findById($id) |
||
55 | |||
56 | /** |
||
57 | * Cохранение сущности |
||
58 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
59 | */ |
||
60 | public function save(EntityInterface $Entity) |
||
94 | |||
95 | /** |
||
96 | * из объекта формирует массив |
||
97 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
98 | * @return \Core\Infrastructure\EntityInterface |
||
99 | * @throws BadMethodCallException |
||
100 | */ |
||
101 | protected function unbuildEntity(EntityInterface $Entity){ |
||
127 | |||
128 | /** |
||
129 | * Подготавливаем конечный вариант Сущности |
||
130 | * |
||
131 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
132 | * @param array $row |
||
133 | * @return \Core\Infrastructure\EntityInterface |
||
134 | * @throws BadMethodCallException |
||
135 | */ |
||
136 | protected function buildEntity(EntityInterface $Entity, array $row){ |
||
165 | |||
166 | |||
167 | abstract protected function setEntityTable(); |
||
168 | |||
169 | abstract protected function getPrimaryKey(); |
||
170 | |||
171 | abstract protected function setMappingFields(); |
||
172 | |||
173 | abstract protected function setSoftDeleteKey(); |
||
174 | |||
175 | /** |
||
176 | * |
||
177 | * @param \Core\Infrastructure\ISpecificationCriteria $specification |
||
178 | * @return type |
||
179 | */ |
||
180 | public function findBySpecification(ISpecificationCriteria $specification){ |
||
197 | |||
198 | /** |
||
199 | * Удаление записи |
||
200 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
201 | * @return boolean |
||
202 | */ |
||
203 | public function delete(EntityInterface $Entity) |
||
223 | |||
224 | public function findAllBySpecification(ISpecificationCriteria $specification) |
||
246 | |||
247 | public function findAll() |
||
251 | |||
252 | /** |
||
253 | * Выборка удаленных моделей |
||
254 | * @param \Core\Infrastructure\ISpecificationCriteria $specification |
||
255 | */ |
||
256 | private function setSoftDelete(ISpecificationCriteria $specification){ |
||
264 | |||
265 | /** |
||
266 | * Построение join-ов |
||
267 | */ |
||
268 | protected function setRelations(ISpecificationCriteria $Specification){ |
||
282 | |||
283 | /** |
||
284 | * Использование join-ов |
||
285 | */ |
||
286 | public function useJoins() |
||
292 | |||
293 | public function withDelete(){ |
||
298 | |||
299 | } |
||
300 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.