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) |
||
| 98 | { |
||
| 99 | $this->container = $container; |
||
| 100 | $this->indexName = $name; |
||
| 101 | $this->indexType = $type; |
||
| 102 | $this->searchProvider = $searchProvider; |
||
| 103 | $this->domainConfiguration = $this->container->get('kunstmaan_admin.domain_configuration'); |
||
| 104 | $this->locales = $this->domainConfiguration->getBackendLocales(); |
||
| 105 | $this->analyzerLanguages = $this->container->getParameter('analyzer_languages'); |
||
|
|
|||
| 106 | $this->em = $this->container->get('doctrine')->getManager(); |
||
| 107 | $this->numberOfShards = $numberOfShards; |
||
| 108 | $this->numberOfReplicas = $numberOfReplicas; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param AclProviderInterface $aclProvider |
||
| 113 | */ |
||
| 114 | public function setAclProvider(AclProviderInterface $aclProvider) |
||
| 115 | { |
||
| 116 | $this->aclProvider = $aclProvider; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param IndexablePagePartsService $indexablePagePartsService |
||
| 121 | */ |
||
| 122 | public function setIndexablePagePartsService(IndexablePagePartsService $indexablePagePartsService) |
||
| 123 | { |
||
| 124 | $this->indexablePagePartsService = $indexablePagePartsService; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param array $properties |
||
| 129 | */ |
||
| 130 | public function setDefaultProperties(array $properties) |
||
| 131 | { |
||
| 132 | $this->properties = array_merge($this->properties, $properties); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param LoggerInterface $logger |
||
| 137 | */ |
||
| 138 | public function setLogger(LoggerInterface $logger) |
||
| 139 | { |
||
| 140 | $this->logger = $logger; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function getLanguagesNotAnalyzed() |
||
| 147 | { |
||
| 148 | $notAnalyzed = []; |
||
| 149 | foreach ($this->locales as $locale) { |
||
| 150 | if (preg_match('/[a-z]{2}_?+[a-zA-Z]{2}/', $locale)) { |
||
| 151 | $locale = strtolower($locale); |
||
| 152 | } |
||
| 153 | |||
| 154 | if (false === \array_key_exists($locale, $this->analyzerLanguages)) { |
||
| 155 | $notAnalyzed[] = $locale; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | return $notAnalyzed; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Create node index |
||
| 164 | */ |
||
| 165 | public function createIndex() |
||
| 166 | { |
||
| 167 | //create analysis |
||
| 168 | $analysis = $this->container->get( |
||
| 169 | 'kunstmaan_search.search.factory.analysis' |
||
| 170 | ); |
||
| 171 | |||
| 172 | foreach ($this->locales as $locale) { |
||
| 173 | // Multilanguage check |
||
| 174 | if (preg_match('/[a-z]{2}_?+[a-zA-Z]{2}/', $locale)) { |
||
| 175 | $locale = strtolower($locale); |
||
| 176 | } |
||
| 177 | |||
| 178 | // Build new index |
||
| 179 | $index = $this->searchProvider->createIndex($this->indexName . '_' . $locale); |
||
| 180 | |||
| 181 | if (\array_key_exists($locale, $this->analyzerLanguages)) { |
||
| 182 | $localeAnalysis = clone $analysis; |
||
| 183 | $language = $this->analyzerLanguages[$locale]['analyzer']; |
||
| 184 | |||
| 185 | // Create index with analysis |
||
| 186 | $this->setAnalysis($index, $localeAnalysis->setupLanguage($language)); |
||
| 187 | } else { |
||
| 188 | $index->create(); |
||
| 189 | } |
||
| 190 | |||
| 191 | $this->setMapping($index, $locale); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Populate node index |
||
| 197 | */ |
||
| 198 | public function populateIndex() |
||
| 199 | { |
||
| 200 | $nodeRepository = $this->em->getRepository(Node::class); |
||
| 201 | $nodes = $nodeRepository->getAllTopNodes(); |
||
| 202 | |||
| 203 | foreach ($nodes as $node) { |
||
| 204 | $this->currentTopNode = $node; |
||
| 205 | foreach ($this->locales as $lang) { |
||
| 206 | $this->createNodeDocuments($node, $lang); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | if (!empty($this->documents)) { |
||
| 211 | $this->searchProvider->addDocuments($this->documents); |
||
| 212 | $this->documents = []; |
||
| 213 | } |
||
| 214 | } |
||
| 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) |
||
| 223 | { |
||
| 224 | $this->createNodeDocuments($node, $lang); |
||
| 225 | |||
| 226 | if (!empty($this->documents)) { |
||
| 227 | $this->searchProvider->addDocuments($this->documents); |
||
| 228 | $this->documents = []; |
||
| 229 | } |
||
| 230 | } |
||
| 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) |
||
| 239 | { |
||
| 240 | $nodeTranslation = $node->getNodeTranslation($lang, true); |
||
| 241 | if ($nodeTranslation && $this->indexNodeTranslation($nodeTranslation)) { |
||
| 242 | $this->indexChildren($node, $lang); |
||
| 243 | } |
||
| 244 | } |
||
| 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) |
||
| 344 | { |
||
| 345 | $analysers = $analysis->build(); |
||
| 346 | $args = [ |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Return default search fields mapping for node translations |
||
| 382 | * |
||
| 383 | * @param Index $index |
||
| 384 | * @param string $lang |
||
| 385 | * |
||
| 386 | * @return Mapping|\Elastica\Mapping |
||
| 387 | */ |
||
| 388 | protected function createDefaultSearchFieldsMapping(Index $index, $lang = 'en') |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Initialize the index with the default search fields mapping |
||
| 404 | * |
||
| 405 | * @param Index $index |
||
| 406 | * @param string $lang |
||
| 407 | */ |
||
| 408 | protected function setMapping(Index $index, $lang = 'en') |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Create a search document for a page |
||
| 421 | * |
||
| 422 | * @param NodeTranslation $nodeTranslation |
||
| 423 | * @param Node $node |
||
| 424 | * @param NodeVersion $publicNodeVersion |
||
| 425 | * @param HasNodeInterface $page |
||
| 426 | */ |
||
| 427 | protected function addPageToIndex( |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Add view permissions to the index document |
||
| 488 | * |
||
| 489 | * @param Node $node |
||
| 490 | * @param array $doc |
||
| 491 | * |
||
| 492 | * @return array |
||
| 493 | */ |
||
| 494 | protected function addPermissions(Node $node, &$doc) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Add type to the index document |
||
| 507 | * |
||
| 508 | * @param object $page |
||
| 509 | * @param array $doc |
||
| 510 | * |
||
| 511 | * @return array |
||
| 512 | */ |
||
| 513 | protected function addSearchType($page, &$doc) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Add parent nodes to the index document |
||
| 520 | * |
||
| 521 | * @param Node $node |
||
| 522 | * @param array $doc |
||
| 523 | * |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | protected function addParentAndAncestors($node, &$doc) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Add page content to the index document |
||
| 543 | * |
||
| 544 | * @param NodeTranslation $nodeTranslation |
||
| 545 | * @param HasNodeInterface $page |
||
| 546 | * @param array $doc |
||
| 547 | */ |
||
| 548 | protected function addPageContent(NodeTranslation $nodeTranslation, $page, &$doc) |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Enter request scope if it is not active yet... |
||
| 582 | * |
||
| 583 | * @param string $lang |
||
| 584 | */ |
||
| 585 | protected function enterRequestScope($lang) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Render a custom search view |
||
| 607 | * |
||
| 608 | * @param NodeTranslation $nodeTranslation |
||
| 609 | * @param SearchViewTemplateInterface $page |
||
| 610 | * @param EngineInterface $renderer |
||
| 611 | * |
||
| 612 | * @return string |
||
| 613 | */ |
||
| 614 | protected function renderCustomSearchView( |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Render default search view (all indexable pageparts in the main context |
||
| 650 | * of the page) |
||
| 651 | * |
||
| 652 | * @param NodeTranslation $nodeTranslation |
||
| 653 | * @param HasPagePartsInterface $page |
||
| 654 | * @param EngineInterface $renderer |
||
| 655 | * |
||
| 656 | * @return string |
||
| 657 | */ |
||
| 658 | protected function renderDefaultSearchView( |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Add custom data to index document (you can override to add custom fields |
||
| 682 | * to the search index) |
||
| 683 | * |
||
| 684 | * @param HasNodeInterface $page |
||
| 685 | * @param array $doc |
||
| 686 | */ |
||
| 687 | protected function addCustomData(HasNodeInterface $page, &$doc) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Convert a DateTime to UTC equivalent... |
||
| 701 | * |
||
| 702 | * @param \DateTime $dateTime |
||
| 703 | * |
||
| 704 | * @return \DateTime |
||
| 705 | */ |
||
| 706 | protected function getUTCDateTime(\DateTime $dateTime) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Removes all HTML markup & decode HTML entities |
||
| 716 | * |
||
| 717 | * @param $text |
||
| 718 | * |
||
| 719 | * @return string |
||
| 720 | */ |
||
| 721 | protected function removeHtml($text) |
||
| 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 getEventDispatcher() |
||
| 802 | } |
||
| 803 |
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..