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 ArticleSubscriber 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 ArticleSubscriber, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class ArticleSubscriber implements EventSubscriberInterface |
||
| 42 | { |
||
| 43 | const PAGES_PROPERTY = 'suluPages'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var IndexerInterface |
||
| 47 | */ |
||
| 48 | private $indexer; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var IndexerInterface |
||
| 52 | */ |
||
| 53 | private $liveIndexer; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var DocumentManagerInterface |
||
| 57 | */ |
||
| 58 | private $documentManager; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var DocumentInspector |
||
| 62 | */ |
||
| 63 | private $documentInspector; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var PropertyEncoder |
||
| 67 | */ |
||
| 68 | private $propertyEncoder; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $documents = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | private $liveDocuments = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param IndexerInterface $indexer |
||
| 82 | * @param IndexerInterface $liveIndexer |
||
| 83 | * @param DocumentManagerInterface $documentManager |
||
| 84 | * @param DocumentInspector $documentInspector |
||
| 85 | 66 | * @param PropertyEncoder $propertyEncoder |
|
| 86 | */ |
||
| 87 | public function __construct( |
||
| 100 | |||
| 101 | /** |
||
| 102 | 52 | * {@inheritdoc} |
|
| 103 | */ |
||
| 104 | public static function getSubscribedEvents() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Schedule article document for index. |
||
| 139 | 52 | * |
|
| 140 | * @param AbstractMappingEvent $event |
||
| 141 | 52 | */ |
|
| 142 | 52 | View Code Duplication | public function handleScheduleIndex(AbstractMappingEvent $event) |
| 158 | |||
| 159 | /** |
||
| 160 | * Schedule article document for live index. |
||
| 161 | 24 | * |
|
| 162 | * @param AbstractMappingEvent $event |
||
| 163 | 24 | */ |
|
| 164 | 24 | View Code Duplication | public function handleScheduleIndexLive(AbstractMappingEvent $event) |
| 180 | |||
| 181 | /** |
||
| 182 | * Syncs children between live and draft. |
||
| 183 | 26 | * |
|
| 184 | * @param PublishEvent $event |
||
| 185 | 26 | */ |
|
| 186 | 26 | public function synchronizeChildren(PublishEvent $event) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Returns children of given node. |
||
| 207 | * |
||
| 208 | * @param NodeInterface $node |
||
| 209 | 20 | * |
|
| 210 | * @return NodeInterface[] |
||
| 211 | 20 | */ |
|
| 212 | 20 | private function getChildren(NodeInterface $node) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Publish pages when article will be published. |
||
| 224 | 23 | * |
|
| 225 | * @param PublishEvent $event |
||
| 226 | 23 | */ |
|
| 227 | 23 | public function publishChildren(PublishEvent $event) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Persist page-data for reordering children. |
||
| 244 | 51 | * |
|
| 245 | * @param ReorderEvent $event |
||
| 246 | 51 | */ |
|
| 247 | 51 | public function persistPageDataOnReorder(ReorderEvent $event) |
|
| 259 | |||
| 260 | 50 | /** |
|
| 261 | 8 | * Persist page-data. |
|
| 262 | 8 | * |
|
| 263 | * @param PersistEvent|PublishEvent $event |
||
| 264 | 8 | */ |
|
| 265 | 8 | public function persistPageData($event) |
|
| 274 | 50 | ||
| 275 | 50 | /** |
|
| 276 | 50 | * Set page-data for given document on given node. |
|
| 277 | * |
||
| 278 | 50 | * @param ArticleDocument $document |
|
| 279 | * @param NodeInterface $node |
||
| 280 | * @param string $locale |
||
| 281 | */ |
||
| 282 | private function setPageData(ArticleDocument $document, NodeInterface $node, $locale) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Hydrate page-data. |
||
| 317 | 2 | * |
|
| 318 | 1 | * @param HydrateEvent $event |
|
| 319 | */ |
||
| 320 | 1 | public function hydratePageData(HydrateEvent $event) |
|
| 333 | 51 | ||
| 334 | 10 | /** |
|
| 335 | * Remove draft from children. |
||
| 336 | * |
||
| 337 | 50 | * @param RemoveDraftEvent $event |
|
| 338 | 50 | */ |
|
| 339 | 50 | public function removeDraftChildren(RemoveDraftEvent $event) |
|
| 360 | 18 | ||
| 361 | /** |
||
| 362 | 18 | * Index all scheduled article documents with default indexer. |
|
| 363 | * |
||
| 364 | 18 | * @param FlushEvent $event |
|
| 365 | 18 | */ |
|
| 366 | 18 | View Code Duplication | public function handleFlush(FlushEvent $event) |
| 381 | |||
| 382 | /** |
||
| 383 | * Index all scheduled article documents with live indexer. |
||
| 384 | * |
||
| 385 | * @param FlushEvent $event |
||
| 386 | */ |
||
| 387 | View Code Duplication | public function handleFlushLive(FlushEvent $event) |
|
| 402 | |||
| 403 | 1 | /** |
|
| 404 | * Removes document from live index and unpublish document in default index. |
||
| 405 | * |
||
| 406 | * @param UnpublishEvent $event |
||
| 407 | */ |
||
| 408 | public function handleUnpublish(UnpublishEvent $event) |
||
| 420 | 1 | ||
| 421 | /** |
||
| 422 | 1 | * Reindex article if a page was removed. |
|
| 423 | * |
||
| 424 | * @param RemoveEvent $event |
||
| 425 | */ |
||
| 426 | public function handleRemovePage(RemoveEvent $event) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Reindex article live if a page was removed. |
||
| 442 | * |
||
| 443 | * @param RemoveEvent $event |
||
| 444 | */ |
||
| 445 | 5 | public function handleRemovePageLive(RemoveEvent $event) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Removes article-document. |
||
| 461 | * |
||
| 462 | * @param RemoveEvent $event |
||
| 463 | */ |
||
| 464 | public function handleRemove(RemoveEvent $event) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Removes article-document. |
||
| 477 | * |
||
| 478 | * @param RemoveEvent|UnpublishEvent $event |
||
| 479 | */ |
||
| 480 | 51 | public function handleRemoveLive($event) |
|
| 490 | |||
| 491 | 2 | /** |
|
| 492 | 8 | * Schedule document to index. |
|
| 493 | * |
||
| 494 | * @param CopyEvent $event |
||
| 495 | 50 | */ |
|
| 496 | public function handleCopy(CopyEvent $event) |
||
| 509 | 49 | ||
| 510 | /** |
||
| 511 | 49 | * Set structure-type to pages. |
|
| 512 | * |
||
| 513 | * @param PersistEvent $event |
||
| 514 | */ |
||
| 515 | 49 | public function setChildrenStructureType(PersistEvent $event) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Extend metadata for article-page. |
||
| 534 | * |
||
| 535 | * @param MetadataLoadEvent $event |
||
| 536 | */ |
||
| 537 | public function handleMetadataLoad(MetadataLoadEvent $event) |
||
| 551 | } |
||
| 552 |
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.