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 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  | 
            ||
| 28 | class Dispatcher implements DispatcherInterface  | 
            ||
| 29 | { | 
            ||
| 30 | use EventManagerAwareTrait;  | 
            ||
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * @var string  | 
            ||
| 34 | */  | 
            ||
| 35 | const WORKFLOW_DISPATCH_EVENT = 'workflowDispatchEvent';  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * Имя класса события  | 
            ||
| 39 | *  | 
            ||
| 40 | * @var string  | 
            ||
| 41 | */  | 
            ||
| 42 | protected $workflowDispatchEventClassName = WorkflowDispatchEvent::class;  | 
            ||
| 43 | |||
| 44 | /**  | 
            ||
| 45 | * Имя класса события  | 
            ||
| 46 | *  | 
            ||
| 47 | * @var string  | 
            ||
| 48 | */  | 
            ||
| 49 | protected $transientVarsClassName = BaseTransientVars::class;  | 
            ||
| 50 | |||
| 51 | /**  | 
            ||
| 52 | * @var WorkflowService  | 
            ||
| 53 | */  | 
            ||
| 54 | protected $workflowService;  | 
            ||
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * @var ReaderInterface  | 
            ||
| 58 | */  | 
            ||
| 59 | protected $metadataReader;  | 
            ||
| 60 | |||
| 61 | /**  | 
            ||
| 62 | * @var ValidatorPluginManager  | 
            ||
| 63 | */  | 
            ||
| 64 | protected $validatorManager;  | 
            ||
| 65 | |||
| 66 | /**  | 
            ||
| 67 | * @param array $options  | 
            ||
| 68 | */  | 
            ||
| 69 | public function __construct(array $options = [])  | 
            ||
| 73 | |||
| 74 | /**  | 
            ||
| 75 | * @param WorkflowService $workflowService  | 
            ||
| 76 | * @param ReaderInterface $metadataReader  | 
            ||
| 77 | * @param ValidatorPluginManager $validatorManager  | 
            ||
| 78 | */  | 
            ||
| 79 | protected function init(WorkflowService $workflowService, ReaderInterface $metadataReader, ValidatorPluginManager $validatorManager)  | 
            ||
| 85 | |||
| 86 | /**  | 
            ||
| 87 | * Диспетчирезация работы с workflow  | 
            ||
| 88 | *  | 
            ||
| 89 | * @param WorkflowDispatchEventInterface $event  | 
            ||
| 90 | *  | 
            ||
| 91 | * @return void  | 
            ||
| 92 | */  | 
            ||
| 93 | public function dispatch(WorkflowDispatchEventInterface $event)  | 
            ||
| 143 | |||
| 144 | /**  | 
            ||
| 145 | * Добавление подписчиков по умолчанию  | 
            ||
| 146 | *  | 
            ||
| 147 | */  | 
            ||
| 148 | public function attachDefaultListeners()  | 
            ||
| 156 | |||
| 157 | /**  | 
            ||
| 158 | * Получение метаданных  | 
            ||
| 159 | *  | 
            ||
| 160 | * @param WorkflowDispatchEventInterface $e  | 
            ||
| 161 | *  | 
            ||
| 162 | * @return MetadataInterface|null  | 
            ||
| 163 | */  | 
            ||
| 164 | public function onLoadMetadataHandler(WorkflowDispatchEventInterface $e)  | 
            ||
| 189 | |||
| 190 | /**  | 
            ||
| 191 | * @param WorkflowDispatchEventInterface $e  | 
            ||
| 192 | *  | 
            ||
| 193 | * @return mixed|null  | 
            ||
| 194 | *  | 
            ||
| 195 | * @throws Exception\PrepareDataException  | 
            ||
| 196 | */  | 
            ||
| 197 | public function onPrepareDataHandler(WorkflowDispatchEventInterface $e)  | 
            ||
| 235 | |||
| 236 | /**  | 
            ||
| 237 | * Проверка, на то нужно ли запускать workflow  | 
            ||
| 238 | *  | 
            ||
| 239 | * @param WorkflowDispatchEventInterface $e  | 
            ||
| 240 | *  | 
            ||
| 241 | * @return boolean|null  | 
            ||
| 242 | *  | 
            ||
| 243 | * @throws \Zend\Validator\Exception\InvalidArgumentException  | 
            ||
| 244 | * @throws Exception\PrepareDataException  | 
            ||
| 245 | */  | 
            ||
| 246 | public function onCheckRunWorkflowHandler(WorkflowDispatchEventInterface $e)  | 
            ||
| 313 | |||
| 314 | /**  | 
            ||
| 315 | * Запуск workflow  | 
            ||
| 316 | *  | 
            ||
| 317 | * @param WorkflowDispatchEventInterface $e  | 
            ||
| 318 | *  | 
            ||
| 319 | * @return TransitionResultInterface  | 
            ||
| 320 | *  | 
            ||
| 321 | *  | 
            ||
| 322 | * @throws Exception\InvalidArgumentException  | 
            ||
| 323 | * @throws Exception\WorkflowDispatchEventException  | 
            ||
| 324 | */  | 
            ||
| 325 | public function onRunWorkflowHandler(WorkflowDispatchEventInterface $e)  | 
            ||
| 391 | |||
| 392 | /**  | 
            ||
| 393 | *  | 
            ||
| 394 | * @return TransientVarsInterface  | 
            ||
| 395 | *  | 
            ||
| 396 | * @throws Exception\WorkflowDispatchEventException  | 
            ||
| 397 | */  | 
            ||
| 398 | public function factoryTransientVars()  | 
            ||
| 404 | |||
| 405 | /**  | 
            ||
| 406 | * Фабрика для создания событий  | 
            ||
| 407 | *  | 
            ||
| 408 | * @return WorkflowDispatchEventInterface  | 
            ||
| 409 | *  | 
            ||
| 410 | * @throws Exception\WorkflowDispatchEventException  | 
            ||
| 411 | */  | 
            ||
| 412 | public function workflowDispatchEventFactory()  | 
            ||
| 418 | |||
| 419 | /**  | 
            ||
| 420 | * Создает экземпляр класса и проверяет то что созданный объект имплементирует заданный интерфейс  | 
            ||
| 421 | *  | 
            ||
| 422 | * @param $className  | 
            ||
| 423 | * @param $interface  | 
            ||
| 424 | *  | 
            ||
| 425 | * @return mixed  | 
            ||
| 426 | *  | 
            ||
| 427 | * @throws Exception\WorkflowDispatchEventException  | 
            ||
| 428 | */  | 
            ||
| 429 | protected function factoryClassName($className, $interface)  | 
            ||
| 442 | |||
| 443 | /**  | 
            ||
| 444 | * @return WorkflowService  | 
            ||
| 445 | */  | 
            ||
| 446 | public function getWorkflowService()  | 
            ||
| 450 | |||
| 451 | /**  | 
            ||
| 452 | * @param WorkflowService $workflowService  | 
            ||
| 453 | *  | 
            ||
| 454 | * @return $this  | 
            ||
| 455 | */  | 
            ||
| 456 | public function setWorkflowService(WorkflowService $workflowService)  | 
            ||
| 462 | |||
| 463 | /**  | 
            ||
| 464 | * @return ReaderInterface  | 
            ||
| 465 | */  | 
            ||
| 466 | public function getMetadataReader()  | 
            ||
| 470 | |||
| 471 | /**  | 
            ||
| 472 | * @param ReaderInterface $metadataReader  | 
            ||
| 473 | *  | 
            ||
| 474 | * @return $this  | 
            ||
| 475 | */  | 
            ||
| 476 | public function setMetadataReader(ReaderInterface $metadataReader)  | 
            ||
| 482 | |||
| 483 | /**  | 
            ||
| 484 | * @return ValidatorPluginManager  | 
            ||
| 485 | */  | 
            ||
| 486 | public function getValidatorManager()  | 
            ||
| 490 | |||
| 491 | /**  | 
            ||
| 492 | * @param ValidatorPluginManager $validatorManager  | 
            ||
| 493 | *  | 
            ||
| 494 | * @return $this  | 
            ||
| 495 | */  | 
            ||
| 496 | public function setValidatorManager(ValidatorPluginManager $validatorManager)  | 
            ||
| 502 | |||
| 503 | /**  | 
            ||
| 504 | * @return string  | 
            ||
| 505 | */  | 
            ||
| 506 | public function getWorkflowDispatchEventClassName()  | 
            ||
| 510 | |||
| 511 | /**  | 
            ||
| 512 | * @param string $workflowDispatchEventClassName  | 
            ||
| 513 | *  | 
            ||
| 514 | * @return $this  | 
            ||
| 515 | */  | 
            ||
| 516 | public function setWorkflowDispatchEventClassName($workflowDispatchEventClassName)  | 
            ||
| 522 | |||
| 523 | /**  | 
            ||
| 524 | * @return string  | 
            ||
| 525 | */  | 
            ||
| 526 | public function getTransientVarsClassName()  | 
            ||
| 530 | |||
| 531 | /**  | 
            ||
| 532 | * @param string $transientVarsClassName  | 
            ||
| 533 | *  | 
            ||
| 534 | * @return $this  | 
            ||
| 535 | */  | 
            ||
| 536 | public function setTransientVarsClassName($transientVarsClassName)  | 
            ||
| 542 | }  | 
            ||
| 543 | 
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.