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:
1 | <?php |
||
28 | class RouteHandler extends AbstractListenerAggregate |
||
29 | { |
||
30 | use EventManagerAwareTrait; |
||
31 | |||
32 | /** |
||
33 | * Адаптер для чтения метаданных |
||
34 | * |
||
35 | * @var ReaderInterface |
||
36 | */ |
||
37 | protected $metadataReader; |
||
38 | |||
39 | /** |
||
40 | * Имя класса событие, бросаемого когда требуется определить entryId |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $resolveEntryIdEventClassName = ResolveEntryIdEvent::class; |
||
45 | |||
46 | /** |
||
47 | * Сервис для работы с workflow |
||
48 | * |
||
49 | * @var WorkflowServiceInterface |
||
50 | */ |
||
51 | protected $workflowService; |
||
52 | |||
53 | /** |
||
54 | * Логер |
||
55 | * |
||
56 | * @var LoggerInterface |
||
57 | */ |
||
58 | protected $log; |
||
59 | |||
60 | /** |
||
61 | * RouteHandler constructor. |
||
62 | * |
||
63 | * @param array $options |
||
64 | */ |
||
65 | public function __construct(array $options = []) |
||
74 | |||
75 | /** |
||
76 | * @param ReaderInterface $metadataReader |
||
77 | * @param WorkflowServiceInterface $workflowService |
||
78 | * @param LoggerInterface $log |
||
79 | */ |
||
80 | protected function init(ReaderInterface $metadataReader, WorkflowServiceInterface $workflowService, LoggerInterface $log) |
||
86 | |||
87 | /** |
||
88 | * @param EventManagerInterface $events |
||
89 | */ |
||
90 | public function attach(EventManagerInterface $events, $priority = 1) |
||
91 | { |
||
92 | $events->getSharedManager()->attach(Dispatcher::class, WorkflowDispatchEventInterface::METADATA_WORKFLOW_TO_RUN_EVENT, [$this, 'onMetadataWorkflowToRun'], 80); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Получение метаданных для запуска workflow |
||
97 | * |
||
98 | * @param WorkflowDispatchEventInterface $e |
||
99 | * |
||
100 | * @return RunWorkflowParam|null |
||
101 | * |
||
102 | * @throws Exception\InvalidMetadataException |
||
103 | * @throws Exception\ResolveEntryIdEventException |
||
104 | * @throws Exception\InvalidWorkflowManagerAliasException |
||
105 | * @throws Exception\RuntimeException |
||
106 | */ |
||
107 | public function onMetadataWorkflowToRun(WorkflowDispatchEventInterface $e) |
||
218 | |||
219 | /** |
||
220 | * Подписчики по умолчанию |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | public function attachDefaultListeners() |
||
228 | |||
229 | /** |
||
230 | * Определение entryId на основе параметров роута |
||
231 | * |
||
232 | * @param ResolveEntryIdEventInterface $event |
||
233 | * |
||
234 | * @return integer|null |
||
235 | * |
||
236 | * @throws Exception\InvalidMetadataException |
||
237 | */ |
||
238 | public function onResolveEntryIdHandler(ResolveEntryIdEventInterface $event) |
||
286 | |||
287 | |||
288 | /** |
||
289 | * @return ReaderInterface |
||
290 | */ |
||
291 | public function getMetadataReader() |
||
295 | |||
296 | /** |
||
297 | * @param ReaderInterface $metadataReader |
||
298 | * |
||
299 | * @return $this |
||
300 | */ |
||
301 | public function setMetadataReader(ReaderInterface $metadataReader) |
||
307 | |||
308 | /** |
||
309 | * Имя класса событие, бросаемого когда требуется определить entryId |
||
310 | * |
||
311 | * @return string |
||
312 | */ |
||
313 | public function getResolveEntryIdEventClassName() |
||
317 | |||
318 | /** |
||
319 | * Устанавливает имя класса событие, бросаемого когда требуется определить entryId |
||
320 | * |
||
321 | * @param string $resolveEntryIdEventClassName |
||
322 | * |
||
323 | * @return $this |
||
324 | */ |
||
325 | public function setResolveEntryIdEventClassName($resolveEntryIdEventClassName) |
||
331 | |||
332 | /** |
||
333 | * Фабрика для создания объекта события бросаемого когда нужно определить значение entryId |
||
334 | * |
||
335 | * @return ResolveEntryIdEventInterface |
||
336 | * |
||
337 | * @throws Exception\ResolveEntryIdEventException |
||
338 | */ |
||
339 | public function resolveEntryIdEventFactory() |
||
353 | |||
354 | /** |
||
355 | * Устанавливает логер |
||
356 | * |
||
357 | * @return LoggerInterface |
||
358 | */ |
||
359 | public function getLog() |
||
363 | |||
364 | /** |
||
365 | * Возвращает логер |
||
366 | * |
||
367 | * @param LoggerInterface $log |
||
368 | * |
||
369 | * @return $this |
||
370 | */ |
||
371 | public function setLog(LoggerInterface $log) |
||
377 | |||
378 | /** |
||
379 | * Сервис для работы с workflow |
||
380 | * |
||
381 | * @return WorkflowServiceInterface |
||
382 | */ |
||
383 | public function getWorkflowService() |
||
387 | |||
388 | /** |
||
389 | * Устанавливает сервис для работы с workflow |
||
390 | * |
||
391 | * @param WorkflowServiceInterface $workflowService |
||
392 | * |
||
393 | * @return $this |
||
394 | */ |
||
395 | public function setWorkflowService(WorkflowServiceInterface $workflowService) |
||
401 | } |
||
402 |
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.