Complex classes like Dispatcher 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 Dispatcher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Dispatcher implements DispatcherInterface |
||
31 | { |
||
32 | use EventManagerAwareTrait; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | const WORKFLOW_DISPATCH_EVENT = 'workflowDispatchEvent'; |
||
38 | |||
39 | /** |
||
40 | * Имя класса события |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $workflowDispatchEventClassName = WorkflowDispatchEvent::class; |
||
45 | |||
46 | /** |
||
47 | * Имя класса события |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $transientVarsClassName = BaseTransientVars::class; |
||
52 | |||
53 | /** |
||
54 | * @var WorkflowService |
||
55 | */ |
||
56 | protected $workflowService; |
||
57 | |||
58 | /** |
||
59 | * @var ReaderInterface |
||
60 | */ |
||
61 | protected $metadataReader; |
||
62 | |||
63 | /** |
||
64 | * @var ValidatorPluginManager |
||
65 | */ |
||
66 | protected $validatorManager; |
||
67 | |||
68 | /** |
||
69 | * Логер |
||
70 | * |
||
71 | * @var LoggerInterface |
||
72 | */ |
||
73 | protected $log; |
||
74 | |||
75 | /** |
||
76 | * @param array $options |
||
77 | */ |
||
78 | public function __construct(array $options = []) |
||
88 | |||
89 | /** |
||
90 | * @param WorkflowService $workflowService |
||
91 | * @param ReaderInterface $metadataReader |
||
92 | * @param ValidatorPluginManager $validatorManager |
||
93 | * @param LoggerInterface $log |
||
94 | */ |
||
95 | protected function init(WorkflowService $workflowService, ReaderInterface $metadataReader, ValidatorPluginManager $validatorManager, LoggerInterface $log) |
||
102 | |||
103 | /** |
||
104 | * Диспетчирезация работы с workflow |
||
105 | * |
||
106 | * @param WorkflowDispatchEventInterface $event |
||
107 | * |
||
108 | * @return void |
||
109 | * |
||
110 | * @throws Exception\RunWorkflowParamException |
||
111 | */ |
||
112 | public function dispatch(WorkflowDispatchEventInterface $event) |
||
206 | |||
207 | /** |
||
208 | * Добавление подписчиков по умолчанию |
||
209 | * |
||
210 | */ |
||
211 | public function attachDefaultListeners() |
||
219 | |||
220 | /** |
||
221 | * Получение метаданных |
||
222 | * |
||
223 | * @param WorkflowDispatchEventInterface $e |
||
224 | * |
||
225 | * @return MetadataInterface|null |
||
226 | */ |
||
227 | public function onLoadMetadataHandler(WorkflowDispatchEventInterface $e) |
||
265 | |||
266 | /** |
||
267 | * @param WorkflowDispatchEventInterface $e |
||
268 | * |
||
269 | * @return mixed|null |
||
270 | * |
||
271 | * @throws Exception\PrepareDataException |
||
272 | */ |
||
273 | public function onPrepareDataHandler(WorkflowDispatchEventInterface $e) |
||
311 | |||
312 | /** |
||
313 | * Проверка, на то нужно ли запускать workflow |
||
314 | * |
||
315 | * @param WorkflowDispatchEventInterface $e |
||
316 | * |
||
317 | * @return boolean|null |
||
318 | * |
||
319 | * @throws \Zend\Validator\Exception\InvalidArgumentException |
||
320 | * @throws Exception\PrepareDataException |
||
321 | */ |
||
322 | public function onCheckRunWorkflowHandler(WorkflowDispatchEventInterface $e) |
||
389 | |||
390 | /** |
||
391 | * Запуск workflow |
||
392 | * |
||
393 | * @param WorkflowDispatchEventInterface $e |
||
394 | * |
||
395 | * @return TransitionResultInterface |
||
396 | * |
||
397 | * |
||
398 | * @throws Exception\InvalidArgumentException |
||
399 | * @throws Exception\WorkflowDispatchEventException |
||
400 | * @throws \OldTown\Workflow\ZF2\ServiceEngine\Exception\InvalidInitializeWorkflowEntryException |
||
401 | * @throws \OldTown\Workflow\ZF2\ServiceEngine\Exception\DoActionException |
||
402 | */ |
||
403 | public function onRunWorkflowHandler(WorkflowDispatchEventInterface $e) |
||
443 | |||
444 | /** |
||
445 | * |
||
446 | * @return TransientVarsInterface |
||
447 | * |
||
448 | * @throws Exception\WorkflowDispatchEventException |
||
449 | */ |
||
450 | public function factoryTransientVars() |
||
456 | |||
457 | /** |
||
458 | * Фабрика для создания событий |
||
459 | * |
||
460 | * @return WorkflowDispatchEventInterface |
||
461 | * |
||
462 | * @throws Exception\WorkflowDispatchEventException |
||
463 | */ |
||
464 | public function workflowDispatchEventFactory() |
||
470 | |||
471 | /** |
||
472 | * Создает экземпляр класса и проверяет то что созданный объект имплементирует заданный интерфейс |
||
473 | * |
||
474 | * @param $className |
||
475 | * @param $interface |
||
476 | * |
||
477 | * @return mixed |
||
478 | * |
||
479 | * @throws Exception\WorkflowDispatchEventException |
||
480 | */ |
||
481 | protected function factoryClassName($className, $interface) |
||
494 | |||
495 | /** |
||
496 | * @return WorkflowService |
||
497 | */ |
||
498 | public function getWorkflowService() |
||
502 | |||
503 | /** |
||
504 | * @param WorkflowService $workflowService |
||
505 | * |
||
506 | * @return $this |
||
507 | */ |
||
508 | public function setWorkflowService(WorkflowService $workflowService) |
||
514 | |||
515 | /** |
||
516 | * @return ReaderInterface |
||
517 | */ |
||
518 | public function getMetadataReader() |
||
522 | |||
523 | /** |
||
524 | * @param ReaderInterface $metadataReader |
||
525 | * |
||
526 | * @return $this |
||
527 | */ |
||
528 | public function setMetadataReader(ReaderInterface $metadataReader) |
||
534 | |||
535 | /** |
||
536 | * @return ValidatorPluginManager |
||
537 | */ |
||
538 | public function getValidatorManager() |
||
542 | |||
543 | /** |
||
544 | * @param ValidatorPluginManager $validatorManager |
||
545 | * |
||
546 | * @return $this |
||
547 | */ |
||
548 | public function setValidatorManager(ValidatorPluginManager $validatorManager) |
||
554 | |||
555 | /** |
||
556 | * @return string |
||
557 | */ |
||
558 | public function getWorkflowDispatchEventClassName() |
||
562 | |||
563 | /** |
||
564 | * @param string $workflowDispatchEventClassName |
||
565 | * |
||
566 | * @return $this |
||
567 | */ |
||
568 | public function setWorkflowDispatchEventClassName($workflowDispatchEventClassName) |
||
574 | |||
575 | /** |
||
576 | * @return string |
||
577 | */ |
||
578 | public function getTransientVarsClassName() |
||
582 | |||
583 | /** |
||
584 | * @param string $transientVarsClassName |
||
585 | * |
||
586 | * @return $this |
||
587 | */ |
||
588 | public function setTransientVarsClassName($transientVarsClassName) |
||
594 | |||
595 | |||
596 | /** |
||
597 | * Устанавливает логер |
||
598 | * |
||
599 | * @return LoggerInterface |
||
600 | */ |
||
601 | public function getLog() |
||
605 | |||
606 | /** |
||
607 | * Возвращает логер |
||
608 | * |
||
609 | * @param LoggerInterface $log |
||
610 | * |
||
611 | * @return $this |
||
612 | */ |
||
613 | public function setLog(LoggerInterface $log) |
||
619 | } |
||
620 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.