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 NodePagesConfiguration 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 NodePagesConfiguration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class NodePagesConfiguration implements SearchConfigurationInterface |
||
39 | { |
||
40 | /** @var string */ |
||
41 | protected $indexName; |
||
42 | |||
43 | /** @var string */ |
||
44 | protected $indexType; |
||
45 | |||
46 | /** @var SearchProviderInterface */ |
||
47 | protected $searchProvider; |
||
48 | |||
49 | /** @var array */ |
||
50 | protected $locales = []; |
||
51 | |||
52 | /** @var array */ |
||
53 | protected $analyzerLanguages; |
||
54 | |||
55 | /** @var EntityManager */ |
||
56 | protected $em; |
||
57 | |||
58 | /** @var array */ |
||
59 | protected $documents = []; |
||
60 | |||
61 | /** @var ContainerInterface */ |
||
62 | protected $container; |
||
63 | |||
64 | /** @var AclProviderInterface */ |
||
65 | protected $aclProvider = null; |
||
66 | |||
67 | /** @var LoggerInterface */ |
||
68 | protected $logger = null; |
||
69 | |||
70 | /** @var IndexablePagePartsService */ |
||
71 | protected $indexablePagePartsService; |
||
72 | |||
73 | /** @var DomainConfigurationInterface */ |
||
74 | protected $domainConfiguration; |
||
75 | |||
76 | /** @var array */ |
||
77 | protected $properties = []; |
||
78 | |||
79 | /** @var int */ |
||
80 | protected $numberOfShards; |
||
81 | |||
82 | /** @var int */ |
||
83 | protected $numberOfReplicas; |
||
84 | |||
85 | /** @var Node */ |
||
86 | protected $currentTopNode = null; |
||
87 | |||
88 | /** @var array */ |
||
89 | protected $nodeRefs = []; |
||
90 | |||
91 | /** |
||
92 | * @param ContainerInterface $container |
||
93 | * @param SearchProviderInterface $searchProvider |
||
94 | * @param string $name |
||
95 | * @param string $type |
||
96 | */ |
||
97 | public function __construct($container, $searchProvider, $name, $type, $numberOfShards = 1, $numberOfReplicas = 0) |
||
110 | |||
111 | /** |
||
112 | * @param AclProviderInterface $aclProvider |
||
113 | */ |
||
114 | public function setAclProvider(AclProviderInterface $aclProvider) |
||
118 | |||
119 | /** |
||
120 | * @param IndexablePagePartsService $indexablePagePartsService |
||
121 | */ |
||
122 | public function setIndexablePagePartsService(IndexablePagePartsService $indexablePagePartsService) |
||
126 | |||
127 | /** |
||
128 | * @param array $properties |
||
129 | */ |
||
130 | public function setDefaultProperties(array $properties) |
||
134 | |||
135 | /** |
||
136 | * @param LoggerInterface $logger |
||
137 | */ |
||
138 | public function setLogger(LoggerInterface $logger) |
||
142 | |||
143 | /** |
||
144 | * @return array |
||
145 | */ |
||
146 | public function getLanguagesNotAnalyzed() |
||
161 | |||
162 | /** |
||
163 | * Create node index |
||
164 | */ |
||
165 | public function createIndex() |
||
194 | |||
195 | /** |
||
196 | * Populate node index |
||
197 | */ |
||
198 | public function populateIndex() |
||
215 | |||
216 | /** |
||
217 | * Index a node (including its children) - for the specified language only |
||
218 | * |
||
219 | * @param Node $node |
||
220 | * @param string $lang |
||
221 | */ |
||
222 | public function indexNode(Node $node, $lang) |
||
231 | |||
232 | /** |
||
233 | * Add documents for the node translation (and children) to the index |
||
234 | * |
||
235 | * @param Node $node |
||
236 | * @param string $lang |
||
237 | */ |
||
238 | public function createNodeDocuments(Node $node, $lang) |
||
245 | |||
246 | /** |
||
247 | * Index all children of the specified node (only for the specified |
||
248 | * language) |
||
249 | * |
||
250 | * @param Node $node |
||
251 | * @param string $lang |
||
252 | */ |
||
253 | public function indexChildren(Node $node, $lang) |
||
259 | |||
260 | /** |
||
261 | * Index a node translation |
||
262 | * |
||
263 | * @param NodeTranslation $nodeTranslation |
||
264 | * @param bool $add Add node immediately to index? |
||
265 | * |
||
266 | * @return bool Return true if the document has been indexed |
||
267 | */ |
||
268 | public function indexNodeTranslation(NodeTranslation $nodeTranslation, $add = false) |
||
300 | |||
301 | /** |
||
302 | * Return if the page is indexable - by default all pages are indexable, |
||
303 | * you can override this by implementing the IndexableInterface on your |
||
304 | * page entity and returning false in the isIndexable method. |
||
305 | * |
||
306 | * @param HasNodeInterface $page |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | protected function isIndexable(HasNodeInterface $page) |
||
314 | |||
315 | /** |
||
316 | * Remove the specified node translation from the index |
||
317 | * |
||
318 | * @param NodeTranslation $nodeTranslation |
||
319 | */ |
||
320 | public function deleteNodeTranslation(NodeTranslation $nodeTranslation) |
||
326 | |||
327 | /** |
||
328 | * Delete the specified index |
||
329 | */ |
||
330 | public function deleteIndex() |
||
336 | |||
337 | /** |
||
338 | * Apply the analysis factory to the index |
||
339 | * |
||
340 | * @param Index $index |
||
341 | * @param AnalysisFactoryInterface $analysis |
||
342 | */ |
||
343 | public function setAnalysis(Index $index, AnalysisFactoryInterface $analysis) |
||
353 | |||
354 | /** |
||
355 | * Return default search fields mapping for node translations |
||
356 | * |
||
357 | * @param Index $index |
||
358 | * @param string $lang |
||
359 | * |
||
360 | * @return Mapping |
||
361 | */ |
||
362 | protected function createDefaultSearchFieldsMapping(Index $index, $lang = 'en') |
||
371 | |||
372 | /** |
||
373 | * Initialize the index with the default search fields mapping |
||
374 | * |
||
375 | * @param Index $index |
||
376 | * @param string $lang |
||
377 | */ |
||
378 | protected function setMapping(Index $index, $lang = 'en') |
||
384 | |||
385 | /** |
||
386 | * Create a search document for a page |
||
387 | * |
||
388 | * @param NodeTranslation $nodeTranslation |
||
389 | * @param Node $node |
||
390 | * @param NodeVersion $publicNodeVersion |
||
391 | * @param HasNodeInterface $page |
||
392 | */ |
||
393 | protected function addPageToIndex( |
||
451 | |||
452 | /** |
||
453 | * Add view permissions to the index document |
||
454 | * |
||
455 | * @param Node $node |
||
456 | * @param array $doc |
||
457 | * |
||
458 | * @return array |
||
459 | */ |
||
460 | protected function addPermissions(Node $node, &$doc) |
||
470 | |||
471 | /** |
||
472 | * Add type to the index document |
||
473 | * |
||
474 | * @param object $page |
||
475 | * @param array $doc |
||
476 | * |
||
477 | * @return array |
||
478 | */ |
||
479 | protected function addSearchType($page, &$doc) |
||
483 | |||
484 | /** |
||
485 | * Add parent nodes to the index document |
||
486 | * |
||
487 | * @param Node $node |
||
488 | * @param array $doc |
||
489 | * |
||
490 | * @return array |
||
491 | */ |
||
492 | protected function addParentAndAncestors($node, &$doc) |
||
506 | |||
507 | /** |
||
508 | * Add page content to the index document |
||
509 | * |
||
510 | * @param NodeTranslation $nodeTranslation |
||
511 | * @param HasNodeInterface $page |
||
512 | * @param array $doc |
||
513 | */ |
||
514 | protected function addPageContent(NodeTranslation $nodeTranslation, $page, &$doc) |
||
545 | |||
546 | /** |
||
547 | * Enter request scope if it is not active yet... |
||
548 | * |
||
549 | * @param string $lang |
||
550 | */ |
||
551 | protected function enterRequestScope($lang) |
||
569 | |||
570 | /** |
||
571 | * Render a custom search view |
||
572 | * |
||
573 | * @param NodeTranslation $nodeTranslation |
||
574 | * @param SearchViewTemplateInterface $page |
||
575 | * @param EngineInterface $renderer |
||
576 | * |
||
577 | * @return string |
||
578 | */ |
||
579 | protected function renderCustomSearchView( |
||
612 | |||
613 | /** |
||
614 | * Render default search view (all indexable pageparts in the main context |
||
615 | * of the page) |
||
616 | * |
||
617 | * @param NodeTranslation $nodeTranslation |
||
618 | * @param HasPagePartsInterface $page |
||
619 | * @param EngineInterface $renderer |
||
620 | * |
||
621 | * @return string |
||
622 | */ |
||
623 | protected function renderDefaultSearchView( |
||
644 | |||
645 | /** |
||
646 | * Add custom data to index document (you can override to add custom fields |
||
647 | * to the search index) |
||
648 | * |
||
649 | * @param HasNodeInterface $page |
||
650 | * @param array $doc |
||
651 | */ |
||
652 | protected function addCustomData(HasNodeInterface $page, &$doc) |
||
663 | |||
664 | /** |
||
665 | * Convert a DateTime to UTC equivalent... |
||
666 | * |
||
667 | * @param \DateTime $dateTime |
||
668 | * |
||
669 | * @return \DateTime |
||
670 | */ |
||
671 | protected function getUTCDateTime(\DateTime $dateTime) |
||
678 | |||
679 | /** |
||
680 | * Removes all HTML markup & decode HTML entities |
||
681 | * |
||
682 | * @param $text |
||
683 | * |
||
684 | * @return string |
||
685 | */ |
||
686 | protected function removeHtml($text) |
||
710 | |||
711 | /** |
||
712 | * Fetch ACL permissions for the specified entity |
||
713 | * |
||
714 | * @param object $object |
||
715 | * |
||
716 | * @return array |
||
717 | */ |
||
718 | protected function getAclPermissions($object) |
||
746 | |||
747 | /** |
||
748 | * @param $publicNodeVersion |
||
749 | * |
||
750 | * @return mixed |
||
751 | */ |
||
752 | private function getNodeRefPage(NodeVersion $publicNodeVersion) |
||
762 | |||
763 | private function getEventDispatcher() |
||
767 | } |
||
768 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..