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 ImportEngine 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 ImportEngine, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ImportEngine implements ImportEngineInterface |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * Контейнер с данными |
||
25 | * |
||
26 | * @var DataContainer\DataContainerInterface |
||
27 | */ |
||
28 | private $dataContainer; |
||
29 | |||
30 | /** |
||
31 | * Метаданные необходимые для заполнения бд |
||
32 | * |
||
33 | * @var MetadataInterface |
||
34 | */ |
||
35 | private $metadata; |
||
36 | |||
37 | /** |
||
38 | * Менеджер объектов доктрины |
||
39 | * |
||
40 | * @var ObjectManager |
||
41 | */ |
||
42 | private $objectManager; |
||
43 | |||
44 | /** |
||
45 | * Связывает id контейнера с данными, с сущностью |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | private $dataContainerIdToDoctrineEntity = []; |
||
50 | |||
51 | /** |
||
52 | * Компонет отвечающий за создание сущностей |
||
53 | * |
||
54 | * @var ContainerInterface |
||
55 | */ |
||
56 | protected $entityLocator; |
||
57 | |||
58 | /** |
||
59 | * ImportEngine constructor. |
||
60 | * |
||
61 | * @param ContainerInterface $entityContainer |
||
62 | */ |
||
63 | public function __construct(ContainerInterface $entityContainer) |
||
67 | |||
68 | /** |
||
69 | * Запуск процесса импорта данных |
||
70 | * |
||
71 | * @param DataContainer\DataContainerInterface $dataContainer |
||
72 | * @param MetadataInterface $metadata |
||
73 | * @param ObjectManager $objectManager |
||
74 | * |
||
75 | * |
||
76 | * @return void |
||
77 | * @throws \Nnx\JmsSerializerModule\DoctrineObjectEngine\Exception\InvalidSetValueException |
||
78 | * @throws \Nnx\JmsSerializerModule\DataContainer\Exception\RuntimeException |
||
79 | * |
||
80 | * @throws \Nnx\JmsSerializerModule\DoctrineObjectEngine\Exception\RuntimeException |
||
81 | * @throws \UnexpectedValueException |
||
82 | * @throws \Interop\Container\Exception\ContainerException |
||
83 | * @throws \Interop\Container\Exception\NotFoundException |
||
84 | */ |
||
85 | public function run(DataContainer\DataContainerInterface $dataContainer, MetadataInterface $metadata, ObjectManager $objectManager) |
||
95 | |||
96 | /** |
||
97 | * Заполняет сущности данными |
||
98 | * |
||
99 | * @throws \Nnx\JmsSerializerModule\DoctrineObjectEngine\Exception\RuntimeException |
||
100 | * @throws \Nnx\JmsSerializerModule\DoctrineObjectEngine\Exception\InvalidSetValueException |
||
101 | */ |
||
102 | protected function hydrateProperties() |
||
130 | |||
131 | /** |
||
132 | * Преобразование типов |
||
133 | * |
||
134 | * @param mixed $value |
||
135 | * @param string $typeOfField |
||
136 | * |
||
137 | * @return mixed |
||
138 | */ |
||
139 | protected function handleTypeConversions($value, $typeOfField) |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Проставляет связи между сущностями |
||
163 | * |
||
164 | * @return void |
||
165 | * @throws \Nnx\JmsSerializerModule\DataContainer\Exception\RuntimeException |
||
166 | * @throws \Nnx\JmsSerializerModule\DoctrineObjectEngine\Exception\RuntimeException |
||
167 | */ |
||
168 | protected function hydrateAssociation() |
||
211 | |||
212 | /** |
||
213 | * Инциализация сущностей |
||
214 | * |
||
215 | * @throws \Nnx\JmsSerializerModule\DoctrineObjectEngine\Exception\RuntimeException |
||
216 | * @throws \UnexpectedValueException |
||
217 | * @throws \Interop\Container\Exception\ContainerException |
||
218 | * @throws \Interop\Container\Exception\NotFoundException |
||
219 | */ |
||
220 | protected function initEntities() |
||
227 | |||
228 | |||
229 | /** |
||
230 | * Инциализация сущности |
||
231 | * |
||
232 | * @param DataContainer\EntityInterface $dataItem |
||
233 | * |
||
234 | * @throws \UnexpectedValueException |
||
235 | * @throws \Nnx\JmsSerializerModule\DoctrineObjectEngine\Exception\RuntimeException |
||
236 | * @throws \Interop\Container\Exception\ContainerException |
||
237 | * @throws \Interop\Container\Exception\NotFoundException |
||
238 | */ |
||
239 | protected function initEntity(DataContainer\EntityInterface $dataItem) |
||
247 | |||
248 | /** |
||
249 | * Определяет нужно ли пытаться создать сущност заново, или необходим поиск в хранилище |
||
250 | * |
||
251 | * @param DataContainer\EntityInterface $dataItem |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | protected function isCandidateForSearchInDb(DataContainer\EntityInterface $dataItem) |
||
264 | |||
265 | /** |
||
266 | * Проверяет можно ли искать сущность в хранилище по id |
||
267 | * |
||
268 | * @param DataContainer\EntityInterface $dataItem |
||
269 | * |
||
270 | * @return bool |
||
271 | */ |
||
272 | protected function isFindById(DataContainer\EntityInterface $dataItem) |
||
290 | |||
291 | /** |
||
292 | * Подготавливает список id для поиска |
||
293 | * |
||
294 | * @param DataContainer\EntityInterface $dataItem |
||
295 | * |
||
296 | * @return array |
||
297 | * @throws \Nnx\JmsSerializerModule\DoctrineObjectEngine\Exception\RuntimeException |
||
298 | */ |
||
299 | protected function buildSearchByIdCriteria(DataContainer\EntityInterface $dataItem) |
||
319 | /** |
||
320 | * Ищет сущность в хранилище |
||
321 | * |
||
322 | * @param DataContainer\EntityInterface $dataItem |
||
323 | * |
||
324 | * @throws \Nnx\JmsSerializerModule\DoctrineObjectEngine\Exception\RuntimeException |
||
325 | * @throws \UnexpectedValueException |
||
326 | * @throws \Interop\Container\Exception\ContainerException |
||
327 | * @throws \Interop\Container\Exception\NotFoundException |
||
328 | */ |
||
329 | protected function initEntityFromPersistenceStorage(DataContainer\EntityInterface $dataItem) |
||
359 | |||
360 | /** |
||
361 | * @param DataContainer\EntityInterface $dataItem |
||
362 | * |
||
363 | * @throws \Interop\Container\Exception\ContainerException |
||
364 | * @throws \Interop\Container\Exception\NotFoundException |
||
365 | */ |
||
366 | protected function createEntity(DataContainer\EntityInterface $dataItem) |
||
376 | |||
377 | /** |
||
378 | * Получает сущность, которая соответствует контейнеру с данными |
||
379 | * |
||
380 | * @param DataContainer\EntityInterface $dataItem |
||
381 | * |
||
382 | * @return mixed |
||
383 | * @throws \Nnx\JmsSerializerModule\DoctrineObjectEngine\Exception\RuntimeException |
||
384 | */ |
||
385 | View Code Duplication | public function getDoctrineEntityByDataContainer(DataContainer\EntityInterface $dataItem) |
|
395 | |||
396 | /** |
||
397 | * Подготовка критериев для поиска в базе данных |
||
398 | * |
||
399 | * @param DataContainer\EntityInterface $dataItem |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | protected function buildSearchCriteria(DataContainer\EntityInterface $dataItem) |
||
414 | |||
415 | /** |
||
416 | * Устанавливает компонент отвечаюзий за создание сущностей |
||
417 | * |
||
418 | * @return ContainerInterface |
||
419 | */ |
||
420 | public function getEntityLocator() |
||
424 | |||
425 | /** |
||
426 | * Возвращает компонент отвечаюзий за создание сущностей |
||
427 | * |
||
428 | * @param ContainerInterface $entityLocator |
||
429 | * |
||
430 | * @return $this |
||
431 | */ |
||
432 | public function setEntityLocator(ContainerInterface $entityLocator) |
||
438 | } |
||
439 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.