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 |
||
37 | class NodePagesConfiguration implements SearchConfigurationInterface |
||
38 | { |
||
39 | /** @var string */ |
||
40 | protected $indexName; |
||
41 | |||
42 | /** @var string */ |
||
43 | protected $indexType; |
||
44 | |||
45 | /** @var SearchProviderInterface */ |
||
46 | protected $searchProvider; |
||
47 | |||
48 | /** @var array */ |
||
49 | protected $locales = []; |
||
50 | |||
51 | /** @var array */ |
||
52 | protected $analyzerLanguages; |
||
53 | |||
54 | /** @var EntityManager */ |
||
55 | protected $em; |
||
56 | |||
57 | /** @var array */ |
||
58 | protected $documents = []; |
||
59 | |||
60 | /** @var ContainerInterface */ |
||
61 | protected $container; |
||
62 | |||
63 | /** @var AclProviderInterface */ |
||
64 | protected $aclProvider = null; |
||
65 | |||
66 | /** @var LoggerInterface */ |
||
67 | protected $logger = null; |
||
68 | |||
69 | /** @var IndexablePagePartsService */ |
||
70 | protected $indexablePagePartsService; |
||
71 | |||
72 | /** @var DomainConfigurationInterface */ |
||
73 | protected $domainConfiguration; |
||
74 | |||
75 | /** @var array */ |
||
76 | protected $properties = []; |
||
77 | |||
78 | /** @var int */ |
||
79 | protected $numberOfShards; |
||
80 | |||
81 | /** @var int */ |
||
82 | protected $numberOfReplicas; |
||
83 | |||
84 | /** @var Node */ |
||
85 | protected $currentTopNode = null; |
||
86 | |||
87 | /** @var array */ |
||
88 | protected $nodeRefs = []; |
||
89 | |||
90 | /** |
||
91 | * @param ContainerInterface $container |
||
92 | * @param SearchProviderInterface $searchProvider |
||
93 | * @param string $name |
||
94 | * @param string $type |
||
95 | */ |
||
96 | public function __construct($container, $searchProvider, $name, $type, $numberOfShards = 1, $numberOfReplicas = 0) |
||
109 | |||
110 | /** |
||
111 | * @param AclProviderInterface $aclProvider |
||
112 | */ |
||
113 | public function setAclProvider(AclProviderInterface $aclProvider) |
||
117 | |||
118 | /** |
||
119 | * @param IndexablePagePartsService $indexablePagePartsService |
||
120 | */ |
||
121 | public function setIndexablePagePartsService(IndexablePagePartsService $indexablePagePartsService) |
||
125 | |||
126 | /** |
||
127 | * @param array $properties |
||
128 | */ |
||
129 | public function setDefaultProperties(array $properties) |
||
133 | |||
134 | /** |
||
135 | * @param LoggerInterface $logger |
||
136 | */ |
||
137 | public function setLogger(LoggerInterface $logger) |
||
141 | |||
142 | /** |
||
143 | * @return array |
||
144 | */ |
||
145 | public function getLanguagesNotAnalyzed() |
||
160 | |||
161 | /** |
||
162 | * Create node index |
||
163 | */ |
||
164 | public function createIndex() |
||
193 | |||
194 | /** |
||
195 | * Populate node index |
||
196 | */ |
||
197 | public function populateIndex() |
||
214 | |||
215 | /** |
||
216 | * Index a node (including its children) - for the specified language only |
||
217 | * |
||
218 | * @param Node $node |
||
219 | * @param string $lang |
||
220 | */ |
||
221 | public function indexNode(Node $node, $lang) |
||
230 | |||
231 | /** |
||
232 | * Add documents for the node translation (and children) to the index |
||
233 | * |
||
234 | * @param Node $node |
||
235 | * @param string $lang |
||
236 | */ |
||
237 | public function createNodeDocuments(Node $node, $lang) |
||
244 | |||
245 | /** |
||
246 | * Index all children of the specified node (only for the specified |
||
247 | * language) |
||
248 | * |
||
249 | * @param Node $node |
||
250 | * @param string $lang |
||
251 | */ |
||
252 | public function indexChildren(Node $node, $lang) |
||
258 | |||
259 | /** |
||
260 | * Index a node translation |
||
261 | * |
||
262 | * @param NodeTranslation $nodeTranslation |
||
263 | * @param bool $add Add node immediately to index? |
||
264 | * |
||
265 | * @return bool Return true if the document has been indexed |
||
266 | */ |
||
267 | public function indexNodeTranslation(NodeTranslation $nodeTranslation, $add = false) |
||
299 | |||
300 | /** |
||
301 | * Return if the page is indexable - by default all pages are indexable, |
||
302 | * you can override this by implementing the IndexableInterface on your |
||
303 | * page entity and returning false in the isIndexable method. |
||
304 | * |
||
305 | * @param HasNodeInterface $page |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | protected function isIndexable(HasNodeInterface $page) |
||
313 | |||
314 | /** |
||
315 | * Remove the specified node translation from the index |
||
316 | * |
||
317 | * @param NodeTranslation $nodeTranslation |
||
318 | */ |
||
319 | public function deleteNodeTranslation(NodeTranslation $nodeTranslation) |
||
325 | |||
326 | /** |
||
327 | * Delete the specified index |
||
328 | */ |
||
329 | public function deleteIndex() |
||
335 | |||
336 | /** |
||
337 | * Apply the analysis factory to the index |
||
338 | * |
||
339 | * @param Index $index |
||
340 | * @param AnalysisFactoryInterface $analysis |
||
341 | */ |
||
342 | public function setAnalysis(Index $index, AnalysisFactoryInterface $analysis) |
||
378 | |||
379 | /** |
||
380 | * Return default search fields mapping for node translations |
||
381 | * |
||
382 | * @param Index $index |
||
383 | * @param string $lang |
||
384 | * |
||
385 | * @return Mapping|\Elastica\Mapping |
||
386 | */ |
||
387 | protected function createDefaultSearchFieldsMapping(Index $index, $lang = 'en') |
||
400 | |||
401 | /** |
||
402 | * Initialize the index with the default search fields mapping |
||
403 | * |
||
404 | * @param Index $index |
||
405 | * @param string $lang |
||
406 | */ |
||
407 | protected function setMapping(Index $index, $lang = 'en') |
||
417 | |||
418 | /** |
||
419 | * Create a search document for a page |
||
420 | * |
||
421 | * @param NodeTranslation $nodeTranslation |
||
422 | * @param Node $node |
||
423 | * @param NodeVersion $publicNodeVersion |
||
424 | * @param HasNodeInterface $page |
||
425 | */ |
||
426 | protected function addPageToIndex( |
||
484 | |||
485 | /** |
||
486 | * Add view permissions to the index document |
||
487 | * |
||
488 | * @param Node $node |
||
489 | * @param array $doc |
||
490 | * |
||
491 | * @return array |
||
492 | */ |
||
493 | protected function addPermissions(Node $node, &$doc) |
||
503 | |||
504 | /** |
||
505 | * Add type to the index document |
||
506 | * |
||
507 | * @param object $page |
||
508 | * @param array $doc |
||
509 | * |
||
510 | * @return array |
||
511 | */ |
||
512 | protected function addSearchType($page, &$doc) |
||
516 | |||
517 | /** |
||
518 | * Add parent nodes to the index document |
||
519 | * |
||
520 | * @param Node $node |
||
521 | * @param array $doc |
||
522 | * |
||
523 | * @return array |
||
524 | */ |
||
525 | protected function addParentAndAncestors($node, &$doc) |
||
539 | |||
540 | /** |
||
541 | * Add page content to the index document |
||
542 | * |
||
543 | * @param NodeTranslation $nodeTranslation |
||
544 | * @param HasNodeInterface $page |
||
545 | * @param array $doc |
||
546 | */ |
||
547 | protected function addPageContent(NodeTranslation $nodeTranslation, $page, &$doc) |
||
592 | |||
593 | /** |
||
594 | * Enter request scope if it is not active yet... |
||
595 | * |
||
596 | * @param string $lang |
||
597 | */ |
||
598 | protected function enterRequestScope($lang) |
||
617 | |||
618 | /** |
||
619 | * Render a custom search view |
||
620 | * |
||
621 | * @deprecated This method is deprecated since KunstmaanNodeSearchBundle 5.7 and will be removed in KunstmaanNodeSearchBundle 6.0. Use the "renderCustomSearchView" method of the "Kunstmaan\NodeSearchBundle\Services\SearchViewRenderer" instead. |
||
622 | * |
||
623 | * @param NodeTranslation $nodeTranslation |
||
624 | * @param SearchViewTemplateInterface $page |
||
625 | * @param EngineInterface $renderer |
||
626 | * |
||
627 | * @return string |
||
628 | */ |
||
629 | protected function renderCustomSearchView( |
||
658 | |||
659 | /** |
||
660 | * Render default search view (all indexable pageparts in the main context |
||
661 | * of the page) |
||
662 | * |
||
663 | * @deprecated This method is deprecated since KunstmaanNodeSearchBundle 5.7 and will be removed in KunstmaanNodeSearchBundle 6.0. Use the "renderDefaultSearchView" method of the "Kunstmaan\NodeSearchBundle\Services\SearchViewRenderer" instead. |
||
664 | * |
||
665 | * @param NodeTranslation $nodeTranslation |
||
666 | * @param HasPagePartsInterface $page |
||
667 | * @param EngineInterface $renderer |
||
668 | * |
||
669 | * @return string |
||
670 | */ |
||
671 | protected function renderDefaultSearchView( |
||
694 | |||
695 | /** |
||
696 | * Add custom data to index document (you can override to add custom fields |
||
697 | * to the search index) |
||
698 | * |
||
699 | * @param HasNodeInterface $page |
||
700 | * @param array $doc |
||
701 | */ |
||
702 | protected function addCustomData(HasNodeInterface $page, &$doc) |
||
713 | |||
714 | /** |
||
715 | * Convert a DateTime to UTC equivalent... |
||
716 | * |
||
717 | * @param \DateTime $dateTime |
||
718 | * |
||
719 | * @return \DateTime |
||
720 | */ |
||
721 | protected function getUTCDateTime(\DateTime $dateTime) |
||
728 | |||
729 | /** |
||
730 | * Removes all HTML markup & decode HTML entities |
||
731 | * |
||
732 | * @deprecated This method is deprecated since KunstmaanNodeSearchBundle 5.7 and will be removed in KunstmaanNodeSearchBundle 6.0. Use the "removeHtml" method of the "Kunstmaan\NodeSearchBundle\Services\SearchViewRenderer" instead. |
||
733 | * |
||
734 | * @param $text |
||
735 | * |
||
736 | * @return string |
||
737 | */ |
||
738 | protected function removeHtml($text) |
||
746 | |||
747 | /** |
||
748 | * Fetch ACL permissions for the specified entity |
||
749 | * |
||
750 | * @param object $object |
||
751 | * |
||
752 | * @return array |
||
753 | */ |
||
754 | protected function getAclPermissions($object) |
||
782 | |||
783 | /** |
||
784 | * @param $publicNodeVersion |
||
785 | * |
||
786 | * @return mixed |
||
787 | */ |
||
788 | private function getNodeRefPage(NodeVersion $publicNodeVersion) |
||
798 | |||
799 | /** |
||
800 | * @param object $event |
||
801 | * @param string $eventName |
||
802 | * |
||
803 | * @return object |
||
804 | */ |
||
805 | View Code Duplication | private function dispatch($event, string $eventName) |
|
816 | |||
817 | private function isMethodOverridden(string $method) |
||
823 | } |
||
824 |
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..