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 Query 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 Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Query |
||
41 | { |
||
42 | /** @var string */ |
||
43 | private $query; |
||
44 | /** @var int */ |
||
45 | private $skip = 0; |
||
46 | /** @var int */ |
||
47 | private $pageSize = 10; |
||
48 | /** @var string */ |
||
49 | private $collection; |
||
50 | /** @var string */ |
||
51 | private $area; |
||
52 | /** @var string */ |
||
53 | private $biasingProfile; |
||
54 | /** @var string */ |
||
55 | private $language; |
||
56 | /** @var MSort[] */ |
||
57 | private $sort; |
||
58 | /** @var CustomUrlParam[] */ |
||
59 | private $customUrlParams = array(); |
||
60 | /** @var Navigation[] */ |
||
61 | private $navigations = array(); |
||
62 | /** @var string[] */ |
||
63 | private $includedNavigations = array(); |
||
64 | /** @var string[] */ |
||
65 | private $excludedNavigations = array(); |
||
66 | /** @var string[] */ |
||
67 | private $fields = array(); |
||
68 | /** @var string[] */ |
||
69 | private $orFields = array(); |
||
70 | /** @var bool */ |
||
71 | private $pruneRefinements = true; |
||
72 | /** @var bool */ |
||
73 | private $disableAutocorrection = false; |
||
74 | /** @var bool */ |
||
75 | private $wildcardSearchEnabled = false; |
||
76 | // Removed until CBOR support for serialization / de-serialization improves |
||
77 | // /** @var bool */ |
||
78 | // private $returnBinary = false; |
||
79 | /** @var RestrictNavigation */ |
||
80 | private $restrictNavigation; |
||
81 | |||
82 | /** @var Serializer */ |
||
83 | private $serializer; |
||
84 | |||
85 | const TILDE_REGEX = "/~((?=[\\w]*[=:]))/"; |
||
86 | |||
87 | /** |
||
88 | * @param mixed $request |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | private function requestToJson($request) |
||
103 | |||
104 | /** |
||
105 | * @param string $clientKey Your client key. |
||
106 | * |
||
107 | * @return string JSON representation of request to Bridge. |
||
108 | */ |
||
109 | public function getBridgeJson($clientKey) |
||
114 | |||
115 | /** |
||
116 | * @param string $clientKey Your client key. |
||
117 | * @param string $navigationName Name of the navigation to get refinements for |
||
118 | * |
||
119 | * @return string JSON representation of request to Bridge. |
||
120 | */ |
||
121 | public function getBridgeRefinementsJson($clientKey, $navigationName) |
||
128 | |||
129 | /** |
||
130 | * @param string $clientKey |
||
131 | * |
||
132 | * @return Request |
||
133 | */ |
||
134 | private function populateRequest($clientKey) |
||
187 | |||
188 | /** |
||
189 | * @param Navigation[] $navigations |
||
190 | * |
||
191 | * @return Refinement[] |
||
192 | */ |
||
193 | private function generateSelectedRefinements($navigations) |
||
229 | |||
230 | /** |
||
231 | * @param string $clientKey Your client key. |
||
232 | * |
||
233 | * @return string JSON representation of request to Bridge. |
||
234 | */ |
||
235 | public function getBridgeJsonRefinementSearch($clientKey) |
||
250 | |||
251 | public function __construct() |
||
255 | |||
256 | /** |
||
257 | * @return string The current search string. |
||
258 | */ |
||
259 | public function getQuery() |
||
263 | |||
264 | /** |
||
265 | * @param string $query The search string. |
||
266 | */ |
||
267 | public function setQuery($query) |
||
271 | |||
272 | /** |
||
273 | * @return string The data sub-collection. |
||
274 | */ |
||
275 | public function getCollection() |
||
279 | |||
280 | /** |
||
281 | * @param string $collection The string representation of a collection query. |
||
282 | */ |
||
283 | public function setCollection($collection) |
||
287 | |||
288 | /** |
||
289 | * @return string The area name. |
||
290 | */ |
||
291 | public function getArea() |
||
295 | |||
296 | /** |
||
297 | * @param string $area The area name. |
||
298 | */ |
||
299 | public function setArea($area) |
||
303 | |||
304 | /** |
||
305 | * @return string[] A list of metadata fields that will be returned by the search engine. |
||
306 | */ |
||
307 | public function getFields() |
||
311 | |||
312 | /** |
||
313 | * @return string[] A list of the fields that the search service will treat as OR'able. |
||
314 | */ |
||
315 | public function getOrFields() |
||
319 | |||
320 | /** |
||
321 | * @param string[] $fields A list of case-sensitive names of the attributes to return. |
||
322 | */ |
||
323 | public function addFields($fields) |
||
327 | |||
328 | /** |
||
329 | * @return string[] A list of which navigations to return from the bridge. |
||
330 | */ |
||
331 | public function getIncludedNavigations() |
||
335 | |||
336 | /** |
||
337 | * @param string[] $navigations A list of which navigations to return from the bridge. |
||
338 | */ |
||
339 | public function addIncludedNavigations($navigations) |
||
343 | |||
344 | /** |
||
345 | * @return string[] A list of which navigations to not return from the bridge. |
||
346 | */ |
||
347 | public function getExcludedNavigations() |
||
351 | |||
352 | /** |
||
353 | * @param string[] $navigations A list of which navigations to not return from the bridge. |
||
354 | */ |
||
355 | public function addExcludedNavigations($navigations) |
||
359 | |||
360 | /** |
||
361 | * @return Navigation[] |
||
362 | */ |
||
363 | public function &getNavigations() |
||
367 | |||
368 | /** |
||
369 | * @param Navigation[] $navigations |
||
370 | */ |
||
371 | public function setNavigations($navigations) |
||
375 | |||
376 | /** |
||
377 | * @param string $name The case-sensitive name of the attribute to return. |
||
378 | */ |
||
379 | public function addField($name) |
||
383 | |||
384 | /** |
||
385 | * @param string $name Field that should be treated as OR. |
||
386 | */ |
||
387 | public function addOrField($name) |
||
391 | |||
392 | /** |
||
393 | * @param string[] $fields A list of fields that should be treated as OR. |
||
394 | */ |
||
395 | public function addOrFields($fields) |
||
399 | |||
400 | /** |
||
401 | * @param string $name The parameter name. |
||
402 | * @param string $value The parameter value. |
||
403 | */ |
||
404 | public function addCustomUrlParamByName($name, $value) |
||
409 | |||
410 | /** |
||
411 | * @param CustomUrlParam $param Set an additional parameter that can be used to trigger rules. |
||
412 | */ |
||
413 | public function addCustomUrlParam($param) |
||
417 | |||
418 | public function splitRefinements($refinementString) |
||
425 | |||
426 | /** |
||
427 | * @param string $refinementString A tilde separated list of refinements. |
||
428 | */ |
||
429 | public function addRefinementsByString($refinementString) |
||
472 | |||
473 | /** |
||
474 | * @param string $navigationName The name of the Navigation. |
||
475 | * @param Refinement $refinement A RefinementRange or RefinementValue object. |
||
476 | */ |
||
477 | public function addRefinement($navigationName, $refinement) |
||
491 | |||
492 | /** |
||
493 | * @param string $navigationName The name of the refinement. |
||
494 | * @param mixed $low The low value. |
||
495 | * @param mixed $high The high value. |
||
496 | * @param bool $exclude True if the results should exclude this range refinement, false otherwise. |
||
497 | */ |
||
498 | public function addRangeRefinement($navigationName, $low, $high, $exclude = false) |
||
503 | |||
504 | /** |
||
505 | * @param string $navigationName The name of the refinement. |
||
506 | * @param mixed $value The refinement value. |
||
507 | * @param bool $exclude True if the results should exclude this value refinement, false otherwise. |
||
508 | */ |
||
509 | public function addValueRefinement($navigationName, $value, $exclude = false) |
||
514 | |||
515 | /** |
||
516 | * @return bool Are refinements with zero counts being removed. |
||
517 | */ |
||
518 | public function isPruneRefinements() |
||
522 | |||
523 | /** |
||
524 | * @param bool $pruneRefinements Specifies whether refinements should be pruned. |
||
525 | */ |
||
526 | public function setPruneRefinements($pruneRefinements) |
||
530 | |||
531 | /** |
||
532 | * @return MSort[] The current list of sort parameters. |
||
533 | */ |
||
534 | public function &getSort() |
||
538 | |||
539 | /** |
||
540 | * @param MSort[] $sort Any number of sort criteria. |
||
541 | */ |
||
542 | public function setSort($sort) |
||
546 | |||
547 | /** |
||
548 | * @return int The number of documents to skip. |
||
549 | */ |
||
550 | public function getSkip() |
||
554 | |||
555 | /** |
||
556 | * @param int $skip The number of documents to skip. |
||
557 | */ |
||
558 | public function setSkip($skip) |
||
562 | |||
563 | /** |
||
564 | * @return CustomUrlParam[] A list of custom url params. |
||
565 | */ |
||
566 | public function getCustomUrlParams() |
||
570 | |||
571 | /** |
||
572 | * @param CustomUrlParam[] $customUrlParams Set the custom url params. |
||
573 | */ |
||
574 | public function setCustomUrlParams($customUrlParams) |
||
578 | |||
579 | // /** |
||
580 | // * @return bool Is return JSON set to true. |
||
581 | // */ |
||
582 | // public function isReturnBinary() |
||
583 | // { |
||
584 | // return $this->returnBinary; |
||
585 | // } |
||
586 | // |
||
587 | // /** |
||
588 | // * @param bool $returnBinary Whether to tell the bridge to return binary data rather than JSON. |
||
589 | // */ |
||
590 | // public function setReturnBinary($returnBinary) |
||
591 | // { |
||
592 | // $this->returnBinary = $returnBinary; |
||
593 | // } |
||
594 | |||
595 | /** |
||
596 | * @return string The current language restrict value. |
||
597 | */ |
||
598 | public function getLanguage() |
||
602 | |||
603 | /** |
||
604 | * @param string $language The value for language restrict. |
||
605 | */ |
||
606 | public function setLanguage($language) |
||
610 | |||
611 | /** |
||
612 | * @return string The current biasing profile name. |
||
613 | */ |
||
614 | public function getBiasingProfile() |
||
618 | |||
619 | /** |
||
620 | * @param string $biasingProfile Override the biasing profile used for this query. |
||
621 | */ |
||
622 | public function setBiasingProfile($biasingProfile) |
||
626 | |||
627 | /** |
||
628 | * @return int The current page size. |
||
629 | */ |
||
630 | public function getPageSize() |
||
634 | |||
635 | /** |
||
636 | * @param int $pageSize The number of records to return with the query. |
||
637 | */ |
||
638 | public function setPageSize($pageSize) |
||
642 | |||
643 | /** |
||
644 | * @return boolean |
||
645 | */ |
||
646 | public function isDisableAutocorrection() |
||
650 | |||
651 | /** |
||
652 | * @param boolean $disableAutocorrection Specifies whether the auto-correction behavior should be disabled. |
||
653 | * By default, when no results are returned for the given query (and there is |
||
654 | * a did-you-mean available), the first did-you-mean is automatically queried |
||
655 | * instead. |
||
656 | */ |
||
657 | public function setDisableAutocorrection($disableAutocorrection) |
||
661 | |||
662 | /** |
||
663 | * @return boolean |
||
664 | */ |
||
665 | public function isWildcardSearchEnabled() |
||
669 | |||
670 | /** |
||
671 | * @param boolean $wildcardSearchEnabled Indicate if the *(star) character in the search string should be treated |
||
672 | * as a wildcard prefix search. For example, `sta*` will match `star` and |
||
673 | * `start`. |
||
674 | */ |
||
675 | public function setWildcardSearchEnabled($wildcardSearchEnabled) |
||
679 | |||
680 | /** |
||
681 | * <b>Warning</b> This will count as two queries against your search index. |
||
682 | * |
||
683 | * Typically, this feature is used when you have a large number of navigation items that will overwhelm the end |
||
684 | * user. It works by using one of the existing navigation items to decide what the query is about and fires a second |
||
685 | * query to restrict the navigation to the most relevant set of navigation items for this search term. |
||
686 | * |
||
687 | * For example, if you pass in a search of `paper` and a restrict navigation of `category:2` |
||
688 | * |
||
689 | * The bridge will find the category navigation refinements in the first query and fire a second query for the top 2 |
||
690 | * most populous categories. Therefore, a search for something generic like "paper" will bring back top category |
||
691 | * matches like copy paper (1,030), paper pads (567). The bridge will fire off the second query with the search |
||
692 | * term, plus an OR refinement with the most likely categories. The navigation items in the first query are |
||
693 | * entirely replaced with the navigation items in the second query, except for the navigation that was used for the |
||
694 | * restriction so that users still have the ability to navigate by all category types. |
||
695 | * |
||
696 | * @param RestrictNavigation $restrictNavigation Restriction criteria |
||
697 | */ |
||
698 | public function setRestrictNavigation($restrictNavigation) |
||
702 | |||
703 | /** @return RestrictNavigation */ |
||
704 | public function getRestrictNavigation() |
||
708 | |||
709 | /** |
||
710 | * @return string A string representation of all of the currently set refinements. |
||
711 | */ |
||
712 | View Code Duplication | public function getRefinementString() |
|
727 | |||
728 | /** |
||
729 | * @return string A string representation of all of the currently set custom url parameters. |
||
730 | */ |
||
731 | View Code Duplication | public function getCustomUrlParamsString() |
|
744 | |||
745 | /** |
||
746 | * @param MSort $sort |
||
747 | * |
||
748 | * @return RSort |
||
749 | */ |
||
750 | protected static function convertSort($sort) |
||
768 | |||
769 | /** |
||
770 | * @param MMatchStrategy $strategy |
||
771 | * |
||
772 | * @return RMatchStrategy |
||
773 | */ |
||
774 | protected static function convertPartialMatchStrategy($strategy) |
||
791 | |||
792 | /** |
||
793 | * @param MPartialMatchRule $rule |
||
794 | * |
||
795 | * @return RPartialMatchRule |
||
796 | */ |
||
797 | protected static function convertPartialMatchRule($rule) |
||
810 | |||
811 | } |
||
812 |