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 ArticleIndexer 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 ArticleIndexer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class ArticleIndexer implements IndexerInterface |
||
| 45 | { |
||
| 46 | use StructureTagTrait; |
||
| 47 | use ArticleViewDocumentIdTrait; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var StructureMetadataFactoryInterface |
||
| 51 | */ |
||
| 52 | protected $structureMetadataFactory; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var UserManager |
||
| 56 | */ |
||
| 57 | protected $userManager; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var ContactRepository |
||
| 61 | */ |
||
| 62 | protected $contactRepository; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var DocumentFactoryInterface |
||
| 66 | */ |
||
| 67 | protected $documentFactory; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Manager |
||
| 71 | */ |
||
| 72 | protected $manager; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var ExcerptFactory |
||
| 76 | */ |
||
| 77 | protected $excerptFactory; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var SeoFactory |
||
| 81 | */ |
||
| 82 | protected $seoFactory; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var EventDispatcherInterface |
||
| 86 | */ |
||
| 87 | protected $eventDispatcher; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var TranslatorInterface |
||
| 91 | */ |
||
| 92 | protected $translator; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $typeConfiguration; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param StructureMetadataFactoryInterface $structureMetadataFactory |
||
| 101 | * @param UserManager $userManager |
||
| 102 | * @param ContactRepository $contactRepository |
||
| 103 | * @param DocumentFactoryInterface $documentFactory |
||
| 104 | * @param Manager $manager |
||
| 105 | * @param ExcerptFactory $excerptFactory |
||
| 106 | * @param SeoFactory $seoFactory |
||
| 107 | * @param EventDispatcherInterface $eventDispatcher |
||
| 108 | * @param TranslatorInterface $translator |
||
| 109 | 53 | * @param array $typeConfiguration |
|
| 110 | */ |
||
| 111 | public function __construct( |
||
| 112 | StructureMetadataFactoryInterface $structureMetadataFactory, |
||
| 113 | UserManager $userManager, |
||
| 114 | ContactRepository $contactRepository, |
||
| 115 | DocumentFactoryInterface $documentFactory, |
||
| 116 | Manager $manager, |
||
| 117 | ExcerptFactory $excerptFactory, |
||
| 118 | SeoFactory $seoFactory, |
||
| 119 | EventDispatcherInterface $eventDispatcher, |
||
| 120 | TranslatorInterface $translator, |
||
| 121 | 53 | array $typeConfiguration |
|
| 122 | 53 | ) { |
|
| 123 | 53 | $this->structureMetadataFactory = $structureMetadataFactory; |
|
| 124 | 53 | $this->userManager = $userManager; |
|
| 125 | 53 | $this->contactRepository = $contactRepository; |
|
| 126 | 53 | $this->documentFactory = $documentFactory; |
|
| 127 | 53 | $this->manager = $manager; |
|
| 128 | 53 | $this->excerptFactory = $excerptFactory; |
|
| 129 | 53 | $this->seoFactory = $seoFactory; |
|
| 130 | 53 | $this->eventDispatcher = $eventDispatcher; |
|
| 131 | 53 | $this->translator = $translator; |
|
| 132 | $this->typeConfiguration = $typeConfiguration; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns translation for given article type. |
||
| 137 | * |
||
| 138 | * @param string $type |
||
| 139 | * |
||
| 140 | 52 | * @return string |
|
| 141 | */ |
||
| 142 | 52 | private function getTypeTranslation($type) |
|
| 143 | 52 | { |
|
| 144 | if (!array_key_exists($type, $this->typeConfiguration)) { |
||
| 145 | return ucfirst($type); |
||
| 146 | } |
||
| 147 | |||
| 148 | $typeTranslationKey = $this->typeConfiguration[$type]['translation_key']; |
||
| 149 | |||
| 150 | return $this->translator->trans($typeTranslationKey, [], 'backend'); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param ArticleDocument $document |
||
| 155 | 52 | * @param ArticleViewDocumentInterface $article |
|
| 156 | */ |
||
| 157 | 52 | protected function dispatchIndexEvent(ArticleDocument $document, ArticleViewDocumentInterface $article) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param ArticleDocument $document |
||
| 164 | * @param string $locale |
||
| 165 | * @param string $localizationState |
||
| 166 | * |
||
| 167 | 52 | * @return ArticleViewDocumentInterface |
|
| 168 | */ |
||
| 169 | protected function createOrUpdateArticle( |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Returns view-document from index or create a new one. |
||
| 244 | * |
||
| 245 | * @param ArticleDocument $document |
||
| 246 | * @param string $locale |
||
| 247 | 52 | * @param string $localizationState |
|
| 248 | * |
||
| 249 | 52 | * @return ArticleViewDocumentInterface |
|
| 250 | */ |
||
| 251 | 52 | protected function findOrCreateViewDocument(ArticleDocument $document, $locale, $localizationState) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Maps pages from document to view-document. |
||
| 278 | 52 | * |
|
| 279 | * @param ArticleDocument $document |
||
| 280 | 52 | * @param ArticleViewDocumentInterface $article |
|
| 281 | 52 | */ |
|
| 282 | 8 | private function mapPages(ArticleDocument $document, ArticleViewDocumentInterface $article) |
|
| 298 | |||
| 299 | 52 | /** |
|
| 300 | * Set parent-page-uuid to view-document. |
||
| 301 | * |
||
| 302 | * @param StructureMetadata $metadata |
||
| 303 | * @param ArticleDocument $document |
||
| 304 | 52 | * @param ArticleViewDocumentInterface $article |
|
| 305 | 52 | */ |
|
| 306 | 38 | private function setParentPageUuid( |
|
| 328 | |||
| 329 | 52 | /** |
|
| 330 | * Returns property-metadata for route-path property. |
||
| 331 | 52 | * |
|
| 332 | * @param StructureMetadata $metadata |
||
| 333 | * |
||
| 334 | * @return PropertyMetadata |
||
| 335 | 52 | */ |
|
| 336 | 38 | View Code Duplication | private function getRoutePathProperty(StructureMetadata $metadata) |
| 348 | |||
| 349 | /** |
||
| 350 | * @param string $id |
||
| 351 | */ |
||
| 352 | protected function removeArticle($id) |
||
| 364 | 2 | ||
| 365 | 2 | /** |
|
| 366 | 2 | * {@inheritdoc} |
|
| 367 | 2 | */ |
|
| 368 | 2 | public function remove($document) |
|
| 378 | 52 | ||
| 379 | /** |
||
| 380 | * {@inheritdoc} |
||
| 381 | */ |
||
| 382 | public function flush() |
||
| 386 | 18 | ||
| 387 | 18 | /** |
|
| 388 | 18 | * {@inheritdoc} |
|
| 389 | 18 | */ |
|
| 390 | public function clear() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * {@inheritdoc} |
||
| 413 | */ |
||
| 414 | public function setUnpublished($uuid, $locale) |
||
| 429 | 19 | ||
| 430 | /** |
||
| 431 | * {@inheritdoc} |
||
| 432 | */ |
||
| 433 | public function index(ArticleDocument $document) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * {@inheritdoc} |
||
| 442 | */ |
||
| 443 | public function dropIndex() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * {@inheritdoc} |
||
| 450 | */ |
||
| 451 | public function createIndex() |
||
| 459 | } |
||
| 460 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: