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 |
||
29 | class Dispatcher implements DispatcherInterface |
||
30 | { |
||
31 | use EventManagerAwareTrait; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | const WORKFLOW_DISPATCH_EVENT = 'workflowDispatchEvent'; |
||
37 | |||
38 | /** |
||
39 | * Имя класса события |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $workflowDispatchEventClassName = WorkflowDispatchEvent::class; |
||
44 | |||
45 | /** |
||
46 | * Имя класса события |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $transientVarsClassName = BaseTransientVars::class; |
||
51 | |||
52 | /** |
||
53 | * @var WorkflowService |
||
54 | */ |
||
55 | protected $workflowService; |
||
56 | |||
57 | /** |
||
58 | * @var ReaderInterface |
||
59 | */ |
||
60 | protected $metadataReader; |
||
61 | |||
62 | /** |
||
63 | * @var ValidatorPluginManager |
||
64 | */ |
||
65 | protected $validatorManager; |
||
66 | |||
67 | /** |
||
68 | * @param array $options |
||
69 | */ |
||
70 | public function __construct(array $options = []) |
||
74 | |||
75 | /** |
||
76 | * @param WorkflowService $workflowService |
||
77 | * @param ReaderInterface $metadataReader |
||
78 | * @param ValidatorPluginManager $validatorManager |
||
79 | */ |
||
80 | protected function init(WorkflowService $workflowService, ReaderInterface $metadataReader, ValidatorPluginManager $validatorManager) |
||
86 | |||
87 | /** |
||
88 | * Диспетчирезация работы с workflow |
||
89 | * |
||
90 | * @param WorkflowDispatchEventInterface $event |
||
91 | * |
||
92 | * @return void |
||
93 | * |
||
94 | * @throws Exception\RunWorkflowParamException |
||
95 | */ |
||
96 | public function dispatch(WorkflowDispatchEventInterface $event) |
||
158 | |||
159 | /** |
||
160 | * Добавление подписчиков по умолчанию |
||
161 | * |
||
162 | */ |
||
163 | public function attachDefaultListeners() |
||
171 | |||
172 | /** |
||
173 | * Получение метаданных |
||
174 | * |
||
175 | * @param WorkflowDispatchEventInterface $e |
||
176 | * |
||
177 | * @return MetadataInterface|null |
||
178 | */ |
||
179 | public function onLoadMetadataHandler(WorkflowDispatchEventInterface $e) |
||
204 | |||
205 | /** |
||
206 | * @param WorkflowDispatchEventInterface $e |
||
207 | * |
||
208 | * @return mixed|null |
||
209 | * |
||
210 | * @throws Exception\PrepareDataException |
||
211 | */ |
||
212 | public function onPrepareDataHandler(WorkflowDispatchEventInterface $e) |
||
250 | |||
251 | /** |
||
252 | * Проверка, на то нужно ли запускать workflow |
||
253 | * |
||
254 | * @param WorkflowDispatchEventInterface $e |
||
255 | * |
||
256 | * @return boolean|null |
||
257 | * |
||
258 | * @throws \Zend\Validator\Exception\InvalidArgumentException |
||
259 | * @throws Exception\PrepareDataException |
||
260 | */ |
||
261 | public function onCheckRunWorkflowHandler(WorkflowDispatchEventInterface $e) |
||
328 | |||
329 | /** |
||
330 | * Запуск workflow |
||
331 | * |
||
332 | * @param WorkflowDispatchEventInterface $e |
||
333 | * |
||
334 | * @return TransitionResultInterface |
||
335 | * |
||
336 | * |
||
337 | * @throws Exception\InvalidArgumentException |
||
338 | * @throws Exception\WorkflowDispatchEventException |
||
339 | * @throws \OldTown\Workflow\ZF2\ServiceEngine\Exception\InvalidInitializeWorkflowEntryException |
||
340 | * @throws \OldTown\Workflow\ZF2\ServiceEngine\Exception\DoActionException |
||
341 | */ |
||
342 | public function onRunWorkflowHandler(WorkflowDispatchEventInterface $e) |
||
382 | |||
383 | /** |
||
384 | * |
||
385 | * @return TransientVarsInterface |
||
386 | * |
||
387 | * @throws Exception\WorkflowDispatchEventException |
||
388 | */ |
||
389 | public function factoryTransientVars() |
||
395 | |||
396 | /** |
||
397 | * Фабрика для создания событий |
||
398 | * |
||
399 | * @return WorkflowDispatchEventInterface |
||
400 | * |
||
401 | * @throws Exception\WorkflowDispatchEventException |
||
402 | */ |
||
403 | public function workflowDispatchEventFactory() |
||
409 | |||
410 | /** |
||
411 | * Создает экземпляр класса и проверяет то что созданный объект имплементирует заданный интерфейс |
||
412 | * |
||
413 | * @param $className |
||
414 | * @param $interface |
||
415 | * |
||
416 | * @return mixed |
||
417 | * |
||
418 | * @throws Exception\WorkflowDispatchEventException |
||
419 | */ |
||
420 | protected function factoryClassName($className, $interface) |
||
433 | |||
434 | /** |
||
435 | * @return WorkflowService |
||
436 | */ |
||
437 | public function getWorkflowService() |
||
441 | |||
442 | /** |
||
443 | * @param WorkflowService $workflowService |
||
444 | * |
||
445 | * @return $this |
||
446 | */ |
||
447 | public function setWorkflowService(WorkflowService $workflowService) |
||
453 | |||
454 | /** |
||
455 | * @return ReaderInterface |
||
456 | */ |
||
457 | public function getMetadataReader() |
||
461 | |||
462 | /** |
||
463 | * @param ReaderInterface $metadataReader |
||
464 | * |
||
465 | * @return $this |
||
466 | */ |
||
467 | public function setMetadataReader(ReaderInterface $metadataReader) |
||
473 | |||
474 | /** |
||
475 | * @return ValidatorPluginManager |
||
476 | */ |
||
477 | public function getValidatorManager() |
||
481 | |||
482 | /** |
||
483 | * @param ValidatorPluginManager $validatorManager |
||
484 | * |
||
485 | * @return $this |
||
486 | */ |
||
487 | public function setValidatorManager(ValidatorPluginManager $validatorManager) |
||
493 | |||
494 | /** |
||
495 | * @return string |
||
496 | */ |
||
497 | public function getWorkflowDispatchEventClassName() |
||
501 | |||
502 | /** |
||
503 | * @param string $workflowDispatchEventClassName |
||
504 | * |
||
505 | * @return $this |
||
506 | */ |
||
507 | public function setWorkflowDispatchEventClassName($workflowDispatchEventClassName) |
||
513 | |||
514 | /** |
||
515 | * @return string |
||
516 | */ |
||
517 | public function getTransientVarsClassName() |
||
521 | |||
522 | /** |
||
523 | * @param string $transientVarsClassName |
||
524 | * |
||
525 | * @return $this |
||
526 | */ |
||
527 | public function setTransientVarsClassName($transientVarsClassName) |
||
533 | } |
||
534 |
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.