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 EntryIdResolver 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 EntryIdResolver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class EntryIdResolver extends AbstractListenerAggregate |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Имя параметра в конфиги модуля, по которому можно получить имя менеджера wf |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | const WORKFLOW_MANAGER_NAME = 'workflowManagerName'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Имя параметра в конфиге модуля, по которому можно получить имя wf |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | const WORKFLOW_NAME = 'workflowName'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Имя параметра в конфиге модуля, по которому можно конфиг описывающий какие классы и какие имена параметров роуетера |
||
| 43 | * используется, для получения entryId |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | const MAP = 'map'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Имя параметра в карте(@see const MAP), по которому можно получить класс сущности |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | const ENTITY_CLASS_NAME = 'entityClassName'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Имя параметра в карте(@see const MAP), по которому можно получить имя параметра роутера содержащего id сущности |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | const ROUTER_PARAM_NAME = 'routerParamName'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Сервис реализующий функционал, для привязки процессов wf и информации о объектаъ |
||
| 65 | * |
||
| 66 | * @var DoctrineWorkflowStoryService |
||
| 67 | */ |
||
| 68 | protected $doctrineWorkflowStoryService; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Настройки модуля |
||
| 72 | * |
||
| 73 | * @var ModuleOptions |
||
| 74 | */ |
||
| 75 | protected $moduleOptions; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Индекс для маппинга |
||
| 79 | * |
||
| 80 | * @var null|array |
||
| 81 | */ |
||
| 82 | protected $indexMetadata; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Сервис для работы с wf |
||
| 86 | * |
||
| 87 | * @var WorkflowService |
||
| 88 | */ |
||
| 89 | protected $workflowService; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var MvcEvent |
||
| 93 | */ |
||
| 94 | protected $mvcEvent; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var Serializer |
||
| 98 | */ |
||
| 99 | protected $serializer; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * EntryIdResolver constructor. |
||
| 103 | * |
||
| 104 | * @param array $options |
||
| 105 | */ |
||
| 106 | public function __construct(array $options = []) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param DoctrineWorkflowStoryService $doctrineWorkflowStoryService |
||
| 120 | * @param ModuleOptions $moduleOptions |
||
| 121 | * @param WorkflowService $workflowService |
||
| 122 | * @param MvcEvent $mvcEvent |
||
| 123 | * @param Serializer $serializer |
||
| 124 | */ |
||
| 125 | protected function init( |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param EventManagerInterface $events |
||
| 141 | */ |
||
| 142 | public function attach(EventManagerInterface $events) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Обработчик содержащий логику получения entryId |
||
| 149 | * |
||
| 150 | * @param ResolveEntryIdEventInterface $resolveEntryIdEvent |
||
| 151 | * |
||
| 152 | * @return null|void |
||
| 153 | * @throws Exception\InvalidWorkflowEntryToObjectMetadataException |
||
| 154 | * @throws \OldTown\Workflow\ZF2\ServiceEngine\Exception\InvalidManagerNameException |
||
| 155 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
| 156 | * @throws \OldTown\Workflow\ZF2\ServiceEngine\Exception\InvalidWorkflowManagerException |
||
| 157 | * @throws \OldTown\Workflow\Spi\Doctrine\Exception\DoctrineRuntimeException |
||
| 158 | * @throws \Doctrine\ORM\Mapping\MappingException |
||
| 159 | * @throws \Zend\Serializer\Exception\ExceptionInterface |
||
| 160 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
| 161 | * @throws \Doctrine\ORM\NoResultException |
||
| 162 | */ |
||
| 163 | public function onResolveEntryId(ResolveEntryIdEventInterface $resolveEntryIdEvent) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return array|null |
||
| 224 | * |
||
| 225 | * @throws Exception\InvalidWorkflowEntryToObjectMetadataException |
||
| 226 | */ |
||
| 227 | public function getIndexMetadata() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param array|null $indexMetadata |
||
| 293 | * |
||
| 294 | * @return $this |
||
| 295 | */ |
||
| 296 | public function setIndexMetadata(array $indexMetadata = null) |
||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * Сервис реализующий функционал, для привязки процессов wf и информации о объектаъ |
||
| 306 | * |
||
| 307 | * @return DoctrineWorkflowStoryService |
||
| 308 | */ |
||
| 309 | public function getDoctrineWorkflowStoryService() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Устанавливает сервис реализующий функционал, для привязки процессов wf и информации о объектаъ |
||
| 316 | * |
||
| 317 | * @param DoctrineWorkflowStoryService $doctrineWorkflowStoryService |
||
| 318 | * |
||
| 319 | * @return $this |
||
| 320 | */ |
||
| 321 | public function setDoctrineWorkflowStoryService(DoctrineWorkflowStoryService $doctrineWorkflowStoryService) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Настройки модуля |
||
| 330 | * |
||
| 331 | * @return ModuleOptions |
||
| 332 | */ |
||
| 333 | public function getModuleOptions() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Устанавливает настройки модуля |
||
| 340 | * |
||
| 341 | * @param ModuleOptions $moduleOptions |
||
| 342 | * |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | public function setModuleOptions(ModuleOptions $moduleOptions) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return WorkflowService |
||
| 354 | */ |
||
| 355 | public function getWorkflowService() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param WorkflowService $workflowService |
||
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | public function setWorkflowService(WorkflowService $workflowService) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @return MvcEvent |
||
| 374 | */ |
||
| 375 | public function getMvcEvent() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @param MvcEvent $mvcEvent |
||
| 382 | * |
||
| 383 | * @return $this |
||
| 384 | */ |
||
| 385 | public function setMvcEvent(MvcEvent $mvcEvent) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return Serializer |
||
| 394 | */ |
||
| 395 | public function getSerializer() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param Serializer $serializer |
||
| 402 | * |
||
| 403 | * @return $this |
||
| 404 | */ |
||
| 405 | public function setSerializer(Serializer $serializer) |
||
| 411 | } |
||
| 412 |
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.