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 |
||
| 42 | class ArticleIndexer implements IndexerInterface |
||
| 43 | { |
||
| 44 | use ArticleTypeTrait; |
||
| 45 | use ArticleViewDocumentIdTrait; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var StructureMetadataFactoryInterface |
||
| 49 | */ |
||
| 50 | protected $structureMetadataFactory; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var UserManager |
||
| 54 | */ |
||
| 55 | protected $userManager; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var ContactRepository |
||
| 59 | */ |
||
| 60 | protected $contactRepository; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var DocumentFactoryInterface |
||
| 64 | */ |
||
| 65 | protected $documentFactory; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var Manager |
||
| 69 | */ |
||
| 70 | protected $manager; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var ExcerptFactory |
||
| 74 | */ |
||
| 75 | protected $excerptFactory; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var SeoFactory |
||
| 79 | */ |
||
| 80 | protected $seoFactory; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var EventDispatcherInterface |
||
| 84 | */ |
||
| 85 | protected $eventDispatcher; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var TranslatorInterface |
||
| 89 | */ |
||
| 90 | protected $translator; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $typeConfiguration; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param StructureMetadataFactoryInterface $structureMetadataFactory |
||
| 99 | * @param UserManager $userManager |
||
| 100 | * @param ContactRepository $contactRepository |
||
| 101 | * @param DocumentFactoryInterface $documentFactory |
||
| 102 | * @param Manager $manager |
||
| 103 | * @param ExcerptFactory $excerptFactory |
||
| 104 | * @param SeoFactory $seoFactory |
||
| 105 | 51 | * @param EventDispatcherInterface $eventDispatcher |
|
| 106 | * @param TranslatorInterface $translator |
||
| 107 | * @param array $typeConfiguration |
||
| 108 | */ |
||
| 109 | public function __construct( |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns translation for given article type. |
||
| 135 | * |
||
| 136 | 50 | * @param string $type |
|
| 137 | * |
||
| 138 | 50 | * @return string |
|
| 139 | 50 | */ |
|
| 140 | private function getTypeTranslation($type) |
||
| 141 | { |
||
| 142 | if (!array_key_exists($type, $this->typeConfiguration)) { |
||
| 143 | return ucfirst($type); |
||
| 144 | } |
||
| 145 | |||
| 146 | $typeTranslationKey = $this->typeConfiguration[$type]['translation_key']; |
||
| 147 | |||
| 148 | return $this->translator->trans($typeTranslationKey, [], 'backend'); |
||
| 149 | } |
||
| 150 | |||
| 151 | 50 | /** |
|
| 152 | * @param ArticleDocument $document |
||
| 153 | 50 | * @param ArticleViewDocumentInterface $article |
|
| 154 | 50 | */ |
|
| 155 | protected function dispatchIndexEvent(ArticleDocument $document, ArticleViewDocumentInterface $article) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param ArticleDocument $document |
||
| 162 | * @param string $locale |
||
| 163 | 50 | * @param string $localizationState |
|
| 164 | * |
||
| 165 | * @return ArticleViewDocumentInterface |
||
| 166 | */ |
||
| 167 | protected function createOrUpdateArticle( |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Returns view-document from index or create a new one. |
||
| 240 | * |
||
| 241 | * @param ArticleDocument $document |
||
| 242 | 50 | * @param string $locale |
|
| 243 | * @param string $localizationState |
||
| 244 | 50 | * |
|
| 245 | * @return ArticleViewDocumentInterface |
||
| 246 | 50 | */ |
|
| 247 | protected function findOrCreateViewDocument(ArticleDocument $document, $locale, $localizationState) |
||
| 271 | |||
| 272 | /** |
||
| 273 | 50 | * Maps pages from document to view-document. |
|
| 274 | * |
||
| 275 | 50 | * @param ArticleDocument $document |
|
| 276 | 50 | * @param ArticleViewDocumentInterface $article |
|
| 277 | 8 | */ |
|
| 278 | 8 | private function mapPages(ArticleDocument $document, ArticleViewDocumentInterface $article) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Set parent-page-uuid to view-document. |
||
| 294 | * |
||
| 295 | * @param StructureMetadata $metadata |
||
| 296 | * @param ArticleDocument $document |
||
| 297 | * @param ArticleViewDocumentInterface $article |
||
| 298 | */ |
||
| 299 | private function setParentPageUuid( |
||
| 321 | |||
| 322 | 50 | /** |
|
| 323 | 50 | * Returns property-metadata for route-path property. |
|
| 324 | * |
||
| 325 | * @param StructureMetadata $metadata |
||
| 326 | * |
||
| 327 | * @return PropertyMetadata |
||
| 328 | 17 | */ |
|
| 329 | View Code Duplication | private function getRoutePathProperty(StructureMetadata $metadata) |
|
| 341 | |||
| 342 | 17 | /** |
|
| 343 | 17 | * @param string $id |
|
| 344 | */ |
||
| 345 | 17 | protected function removeArticle($id) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * {@inheritdoc} |
||
| 360 | */ |
||
| 361 | public function remove($document) |
||
| 371 | 17 | ||
| 372 | 17 | /** |
|
| 373 | 17 | * {@inheritdoc} |
|
| 374 | 17 | */ |
|
| 375 | public function flush() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * {@inheritdoc} |
||
| 382 | */ |
||
| 383 | public function clear() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * {@inheritdoc} |
||
| 406 | */ |
||
| 407 | public function setUnpublished($uuid) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * {@inheritdoc} |
||
| 423 | */ |
||
| 424 | public function index(ArticleDocument $document) |
||
| 430 | } |
||
| 431 |
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: