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 |
||
36 | class NodePagesConfiguration implements SearchConfigurationInterface |
||
37 | { |
||
38 | /** @var string */ |
||
39 | protected $indexName; |
||
40 | |||
41 | /** @var string */ |
||
42 | protected $indexType; |
||
43 | |||
44 | /** @var SearchProviderInterface */ |
||
45 | protected $searchProvider; |
||
46 | |||
47 | /** @var array */ |
||
48 | protected $locales = []; |
||
49 | |||
50 | /** @var array */ |
||
51 | protected $analyzerLanguages; |
||
52 | |||
53 | /** @var EntityManager */ |
||
54 | protected $em; |
||
55 | |||
56 | /** @var array */ |
||
57 | protected $documents = []; |
||
58 | |||
59 | /** @var ContainerInterface */ |
||
60 | protected $container; |
||
61 | |||
62 | /** @var AclProviderInterface */ |
||
63 | protected $aclProvider = null; |
||
64 | |||
65 | /** @var LoggerInterface */ |
||
66 | protected $logger = null; |
||
67 | |||
68 | /** @var IndexablePagePartsService */ |
||
69 | protected $indexablePagePartsService; |
||
70 | |||
71 | /** @var DomainConfigurationInterface */ |
||
72 | protected $domainConfiguration; |
||
73 | |||
74 | /** @var array */ |
||
75 | protected $properties = []; |
||
76 | |||
77 | /** @var int */ |
||
78 | protected $numberOfShards; |
||
79 | |||
80 | /** @var int */ |
||
81 | protected $numberOfReplicas; |
||
82 | |||
83 | /** @var Node */ |
||
84 | protected $currentTopNode = null; |
||
85 | |||
86 | /** @var array */ |
||
87 | protected $nodeRefs = []; |
||
88 | |||
89 | /** |
||
90 | * @param ContainerInterface $container |
||
91 | * @param SearchProviderInterface $searchProvider |
||
92 | * @param string $name |
||
93 | * @param string $type |
||
94 | */ |
||
95 | public function __construct($container, $searchProvider, $name, $type, $numberOfShards = 1, $numberOfReplicas = 0) |
||
108 | |||
109 | /** |
||
110 | * @param AclProviderInterface $aclProvider |
||
111 | */ |
||
112 | public function setAclProvider(AclProviderInterface $aclProvider) |
||
116 | |||
117 | /** |
||
118 | * @param IndexablePagePartsService $indexablePagePartsService |
||
119 | */ |
||
120 | public function setIndexablePagePartsService(IndexablePagePartsService $indexablePagePartsService) |
||
124 | |||
125 | /** |
||
126 | * @param array $properties |
||
127 | */ |
||
128 | public function setDefaultProperties(array $properties) |
||
132 | |||
133 | /** |
||
134 | * @param LoggerInterface $logger |
||
135 | */ |
||
136 | public function setLogger(LoggerInterface $logger) |
||
140 | |||
141 | /** |
||
142 | * @return array |
||
143 | */ |
||
144 | public function getLanguagesNotAnalyzed() |
||
159 | |||
160 | /** |
||
161 | * Create node index |
||
162 | */ |
||
163 | public function createIndex() |
||
192 | |||
193 | /** |
||
194 | * Populate node index |
||
195 | */ |
||
196 | public function populateIndex() |
||
213 | |||
214 | /** |
||
215 | * Index a node (including its children) - for the specified language only |
||
216 | * |
||
217 | * @param Node $node |
||
218 | * @param string $lang |
||
219 | */ |
||
220 | public function indexNode(Node $node, $lang) |
||
229 | |||
230 | /** |
||
231 | * Add documents for the node translation (and children) to the index |
||
232 | * |
||
233 | * @param Node $node |
||
234 | * @param string $lang |
||
235 | */ |
||
236 | public function createNodeDocuments(Node $node, $lang) |
||
243 | |||
244 | /** |
||
245 | * Index all children of the specified node (only for the specified |
||
246 | * language) |
||
247 | * |
||
248 | * @param Node $node |
||
249 | * @param string $lang |
||
250 | */ |
||
251 | public function indexChildren(Node $node, $lang) |
||
257 | |||
258 | /** |
||
259 | * Index a node translation |
||
260 | * |
||
261 | * @param NodeTranslation $nodeTranslation |
||
262 | * @param bool $add Add node immediately to index? |
||
263 | * |
||
264 | * @return bool Return true if the document has been indexed |
||
265 | */ |
||
266 | public function indexNodeTranslation(NodeTranslation $nodeTranslation, $add = false) |
||
298 | |||
299 | /** |
||
300 | * Return if the page is indexable - by default all pages are indexable, |
||
301 | * you can override this by implementing the IndexableInterface on your |
||
302 | * page entity and returning false in the isIndexable method. |
||
303 | * |
||
304 | * @param HasNodeInterface $page |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | protected function isIndexable(HasNodeInterface $page) |
||
312 | |||
313 | /** |
||
314 | * Remove the specified node translation from the index |
||
315 | * |
||
316 | * @param NodeTranslation $nodeTranslation |
||
317 | */ |
||
318 | public function deleteNodeTranslation(NodeTranslation $nodeTranslation) |
||
324 | |||
325 | /** |
||
326 | * Delete the specified index |
||
327 | */ |
||
328 | public function deleteIndex() |
||
334 | |||
335 | /** |
||
336 | * Apply the analysis factory to the index |
||
337 | * |
||
338 | * @param Index $index |
||
339 | * @param AnalysisFactoryInterface $analysis |
||
340 | */ |
||
341 | public function setAnalysis(Index $index, AnalysisFactoryInterface $analysis) |
||
377 | |||
378 | /** |
||
379 | * Return default search fields mapping for node translations |
||
380 | * |
||
381 | * @param Index $index |
||
382 | * @param string $lang |
||
383 | * |
||
384 | * @return Mapping|\Elastica\Mapping |
||
385 | */ |
||
386 | protected function createDefaultSearchFieldsMapping(Index $index, $lang = 'en') |
||
399 | |||
400 | /** |
||
401 | * Initialize the index with the default search fields mapping |
||
402 | * |
||
403 | * @param Index $index |
||
404 | * @param string $lang |
||
405 | */ |
||
406 | protected function setMapping(Index $index, $lang = 'en') |
||
416 | |||
417 | /** |
||
418 | * Create a search document for a page |
||
419 | * |
||
420 | * @param NodeTranslation $nodeTranslation |
||
421 | * @param Node $node |
||
422 | * @param NodeVersion $publicNodeVersion |
||
423 | * @param HasNodeInterface $page |
||
424 | */ |
||
425 | protected function addPageToIndex( |
||
483 | |||
484 | /** |
||
485 | * Add view permissions to the index document |
||
486 | * |
||
487 | * @param Node $node |
||
488 | * @param array $doc |
||
489 | * |
||
490 | * @return array |
||
491 | */ |
||
492 | protected function addPermissions(Node $node, &$doc) |
||
502 | |||
503 | /** |
||
504 | * Add type to the index document |
||
505 | * |
||
506 | * @param object $page |
||
507 | * @param array $doc |
||
508 | * |
||
509 | * @return array |
||
510 | */ |
||
511 | protected function addSearchType($page, &$doc) |
||
515 | |||
516 | /** |
||
517 | * Add parent nodes to the index document |
||
518 | * |
||
519 | * @param Node $node |
||
520 | * @param array $doc |
||
521 | * |
||
522 | * @return array |
||
523 | */ |
||
524 | protected function addParentAndAncestors($node, &$doc) |
||
538 | |||
539 | /** |
||
540 | * Add page content to the index document |
||
541 | * |
||
542 | * @param NodeTranslation $nodeTranslation |
||
543 | * @param HasNodeInterface $page |
||
544 | * @param array $doc |
||
545 | */ |
||
546 | protected function addPageContent(NodeTranslation $nodeTranslation, $page, &$doc) |
||
577 | |||
578 | /** |
||
579 | * Enter request scope if it is not active yet... |
||
580 | * |
||
581 | * @param string $lang |
||
582 | */ |
||
583 | protected function enterRequestScope($lang) |
||
602 | |||
603 | /** |
||
604 | * Render a custom search view |
||
605 | * |
||
606 | * @param NodeTranslation $nodeTranslation |
||
607 | * @param SearchViewTemplateInterface $page |
||
608 | * @param EngineInterface $renderer |
||
609 | * |
||
610 | * @return string |
||
611 | */ |
||
612 | protected function renderCustomSearchView( |
||
639 | |||
640 | /** |
||
641 | * Render default search view (all indexable pageparts in the main context |
||
642 | * of the page) |
||
643 | * |
||
644 | * @param NodeTranslation $nodeTranslation |
||
645 | * @param HasPagePartsInterface $page |
||
646 | * @param EngineInterface $renderer |
||
647 | * |
||
648 | * @return string |
||
649 | */ |
||
650 | protected function renderDefaultSearchView( |
||
671 | |||
672 | /** |
||
673 | * Add custom data to index document (you can override to add custom fields |
||
674 | * to the search index) |
||
675 | * |
||
676 | * @param HasNodeInterface $page |
||
677 | * @param array $doc |
||
678 | */ |
||
679 | protected function addCustomData(HasNodeInterface $page, &$doc) |
||
690 | |||
691 | /** |
||
692 | * Convert a DateTime to UTC equivalent... |
||
693 | * |
||
694 | * @param \DateTime $dateTime |
||
695 | * |
||
696 | * @return \DateTime |
||
697 | */ |
||
698 | protected function getUTCDateTime(\DateTime $dateTime) |
||
705 | |||
706 | /** |
||
707 | * Removes all HTML markup & decode HTML entities |
||
708 | * |
||
709 | * @param $text |
||
710 | * |
||
711 | * @return string |
||
712 | */ |
||
713 | protected function removeHtml($text) |
||
737 | |||
738 | /** |
||
739 | * Fetch ACL permissions for the specified entity |
||
740 | * |
||
741 | * @param object $object |
||
742 | * |
||
743 | * @return array |
||
744 | */ |
||
745 | protected function getAclPermissions($object) |
||
773 | |||
774 | /** |
||
775 | * @param $publicNodeVersion |
||
776 | * |
||
777 | * @return mixed |
||
778 | */ |
||
779 | private function getNodeRefPage(NodeVersion $publicNodeVersion) |
||
789 | } |
||
790 |
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..