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) |
||
| 547 | { |
||
| 548 | $this->enterRequestScope($nodeTranslation->getLang()); |
||
| 549 | if ($this->logger) { |
||
| 550 | $this->logger->debug( |
||
| 551 | sprintf( |
||
| 552 | 'Indexing page "%s" / lang : %s / type : %s / id : %d / node id : %d', |
||
| 553 | $page->getTitle(), |
||
| 554 | $nodeTranslation->getLang(), |
||
| 555 | \get_class($page), |
||
| 556 | $page->getId(), |
||
| 557 | $nodeTranslation->getNode()->getId() |
||
| 558 | ) |
||
| 559 | ); |
||
| 560 | } |
||
| 561 | |||
| 562 | $doc['content'] = ''; |
||
| 563 | View Code Duplication | if ($page instanceof SearchViewTemplateInterface) { |
|
| 564 | if ($this->isMethodOverridden('renderCustomSearchView')) { |
||
| 565 | @trigger_error(sprintf('Overriding the "%s" method is deprecated since KunstmaanNodeSearchBundle 5.7 and will be removed in KunstmaanNodeSearchBundle 6.0. Override the "renderCustomSearchView" method of the "%s" service instead.', __METHOD__, SearchViewRenderer::class), E_USER_DEPRECATED); |
||
| 566 | |||
| 567 | $doc['content'] = $this->renderCustomSearchView($nodeTranslation, $page, $this->container->get('templating')); |
||
| 568 | } else { |
||
| 569 | $searchViewRenderer = $this->container->get('kunstmaan_node_search.service.search_view_renderer'); |
||
| 570 | |||
| 571 | $doc['content'] = $searchViewRenderer->renderCustomSearchView($nodeTranslation, $page, $this->container); |
||
| 572 | } |
||
| 573 | |||
| 574 | return null; |
||
| 575 | } |
||
| 576 | |||
| 577 | View Code Duplication | if ($page instanceof HasPagePartsInterface) { |
|
| 578 | if ($this->isMethodOverridden('renderDefaultSearchView')) { |
||
| 579 | @trigger_error(sprintf('Overriding the "%s" method is deprecated since KunstmaanNodeSearchBundle 5.7 and will be removed in KunstmaanNodeSearchBundle 6.0. Override the "renderDefaultSearchView" method of the "%s" service instead.', __METHOD__, SearchViewRenderer::class), E_USER_DEPRECATED); |
||
| 580 | |||
| 581 | $doc['content'] = $this->renderDefaultSearchView($nodeTranslation, $page, $this->container->get('templating')); |
||
| 582 | } else { |
||
| 583 | $searchViewRenderer = $this->container->get('kunstmaan_node_search.service.search_view_renderer'); |
||
| 584 | |||
| 585 | $doc['content'] = $searchViewRenderer->renderDefaultSearchView($nodeTranslation, $page); |
||
| 586 | } |
||
| 587 | |||
| 588 | return null; |
||
| 589 | } |
||
| 590 | } |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Enter request scope if it is not active yet... |
||
| 594 | * |
||
| 595 | * @param string $lang |
||
| 596 | */ |
||
| 597 | protected function enterRequestScope($lang) |
||
| 598 | { |
||
| 599 | $locale = null; |
||
| 600 | $requestStack = $this->container->get('request_stack'); |
||
| 601 | // If there already is a request, get the locale from it. |
||
| 602 | if ($requestStack->getCurrentRequest()) { |
||
| 603 | $locale = $requestStack->getCurrentRequest()->getLocale(); |
||
| 604 | } |
||
| 605 | // If we don't have a request or the current request locale is different from the node langauge |
||
| 606 | if (!$requestStack->getCurrentRequest() || ($locale && $locale !== $lang)) { |
||
| 607 | $request = new Request(); |
||
| 608 | $request->setLocale($lang); |
||
| 609 | |||
| 610 | $context = $this->container->get('router')->getContext(); |
||
| 611 | $context->setParameter('_locale', $lang); |
||
| 612 | |||
| 613 | $requestStack->push($request); |
||
| 614 | } |
||
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Render a custom search view |
||
| 619 | * |
||
| 620 | * @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. |
||
| 621 | * |
||
| 622 | * @param NodeTranslation $nodeTranslation |
||
| 623 | * @param SearchViewTemplateInterface $page |
||
| 624 | * @param EngineInterface $renderer |
||
| 625 | * |
||
| 626 | * @return string |
||
| 627 | */ |
||
| 628 | protected function renderCustomSearchView( |
||
| 629 | NodeTranslation $nodeTranslation, |
||
| 630 | SearchViewTemplateInterface $page, |
||
| 631 | EngineInterface $renderer |
||
| 632 | ) { |
||
| 633 | @trigger_error(sprintf('The "%s" method is deprecated since KunstmaanNodeSearchBundle 5.7 and will be removed in KunstmaanNodeSearchBundle 6.0. Use the "%s" service with method "renderCustomSearchView" instead.', __METHOD__, SearchViewRenderer::class), E_USER_DEPRECATED); |
||
| 634 | |||
| 635 | $view = $page->getSearchView(); |
||
| 636 | $renderContext = new RenderContext([ |
||
| 637 | 'locale' => $nodeTranslation->getLang(), |
||
| 638 | 'page' => $page, |
||
| 639 | 'indexMode' => true, |
||
| 640 | 'nodetranslation' => $nodeTranslation, |
||
| 641 | ]); |
||
| 642 | |||
| 643 | if ($page instanceof PageInterface) { |
||
| 644 | $request = $this->container->get('request_stack')->getCurrentRequest(); |
||
| 645 | $page->service($this->container, $request, $renderContext); |
||
| 646 | } |
||
| 647 | |||
| 648 | $content = $this->removeHtml( |
||
| 649 | $renderer->render( |
||
| 650 | $view, |
||
| 651 | $renderContext->getArrayCopy() |
||
| 652 | ) |
||
| 653 | ); |
||
| 654 | |||
| 655 | return $content; |
||
| 656 | } |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Render default search view (all indexable pageparts in the main context |
||
| 660 | * of the page) |
||
| 661 | * |
||
| 662 | * @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. |
||
| 663 | * |
||
| 664 | * @param NodeTranslation $nodeTranslation |
||
| 665 | * @param HasPagePartsInterface $page |
||
| 666 | * @param EngineInterface $renderer |
||
| 667 | * |
||
| 668 | * @return string |
||
| 669 | */ |
||
| 670 | protected function renderDefaultSearchView( |
||
| 671 | NodeTranslation $nodeTranslation, |
||
| 672 | HasPagePartsInterface $page, |
||
| 673 | EngineInterface $renderer |
||
| 674 | ) { |
||
| 675 | @trigger_error(sprintf('The "%s" method is deprecated since KunstmaanNodeSearchBundle 5.7 and will be removed in KunstmaanNodeSearchBundle 6.0. Use the "%s" service with method "renderDefaultSearchView" instead.', __METHOD__, SearchViewRenderer::class), E_USER_DEPRECATED); |
||
| 676 | |||
| 677 | $pageparts = $this->indexablePagePartsService->getIndexablePageParts($page); |
||
| 678 | $view = '@KunstmaanNodeSearch/PagePart/view.html.twig'; |
||
| 679 | $content = $this->removeHtml( |
||
| 680 | $renderer->render( |
||
| 681 | $view, |
||
| 682 | array( |
||
| 683 | 'locale' => $nodeTranslation->getLang(), |
||
| 684 | 'page' => $page, |
||
| 685 | 'pageparts' => $pageparts, |
||
| 686 | 'indexMode' => true, |
||
| 687 | ) |
||
| 688 | ) |
||
| 689 | ); |
||
| 690 | |||
| 691 | return $content; |
||
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Add custom data to index document (you can override to add custom fields |
||
| 696 | * to the search index) |
||
| 697 | * |
||
| 698 | * @param HasNodeInterface $page |
||
| 699 | * @param array $doc |
||
| 700 | */ |
||
| 701 | protected function addCustomData(HasNodeInterface $page, &$doc) |
||
| 702 | { |
||
| 703 | $event = new IndexNodeEvent($page, $doc); |
||
| 704 | $this->container->get('event_dispatcher')->dispatch(IndexNodeEvent::EVENT_INDEX_NODE, $event); |
||
| 705 | |||
| 706 | $doc = $event->doc; |
||
| 707 | |||
| 708 | if ($page instanceof HasCustomSearchDataInterface) { |
||
| 709 | $doc += $page->getCustomSearchData($doc); |
||
| 710 | } |
||
| 711 | } |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Convert a DateTime to UTC equivalent... |
||
| 715 | * |
||
| 716 | * @param \DateTime $dateTime |
||
| 717 | * |
||
| 718 | * @return \DateTime |
||
| 719 | */ |
||
| 720 | protected function getUTCDateTime(\DateTime $dateTime) |
||
| 721 | { |
||
| 722 | $result = clone $dateTime; |
||
| 723 | $result->setTimezone(new \DateTimeZone('UTC')); |
||
| 724 | |||
| 725 | return $result; |
||
| 726 | } |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Removes all HTML markup & decode HTML entities |
||
| 730 | * |
||
| 731 | * @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. |
||
| 732 | * |
||
| 733 | * @param $text |
||
| 734 | * |
||
| 735 | * @return string |
||
| 736 | */ |
||
| 737 | protected function removeHtml($text) |
||
| 738 | { |
||
| 739 | @trigger_error(sprintf('The "%s" method is deprecated since KunstmaanNodeSearchBundle 5.7 and will be removed in KunstmaanNodeSearchBundle 6.0. Use the "removeHtml" method of the "%s" service instead.', __METHOD__, SearchViewRenderer::class), E_USER_DEPRECATED); |
||
| 740 | |||
| 741 | $searchViewRenderer = $this->container->get('kunstmaan_node_search.service.search_view_renderer'); |
||
| 742 | |||
| 743 | return $searchViewRenderer->removeHtml($text); |
||
| 744 | } |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Fetch ACL permissions for the specified entity |
||
| 748 | * |
||
| 749 | * @param object $object |
||
| 750 | * |
||
| 751 | * @return array |
||
| 752 | */ |
||
| 753 | protected function getAclPermissions($object) |
||
| 781 | |||
| 782 | /** |
||
| 783 | * @param $publicNodeVersion |
||
| 784 | * |
||
| 785 | * @return mixed |
||
| 786 | */ |
||
| 787 | private function getNodeRefPage(NodeVersion $publicNodeVersion) |
||
| 797 | |||
| 798 | private function isMethodOverridden(string $method) |
||
| 799 | { |
||
| 800 | $reflector = new \ReflectionMethod($this, $method); |
||
| 801 | |||
| 802 | return $reflector->getDeclaringClass()->getName() !== __CLASS__; |
||
| 803 | } |
||
| 804 | } |
||
| 805 |
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..