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 |
||
| 45 | class Query |
||
| 46 | { |
||
| 47 | /** @var string */ |
||
| 48 | private $query; |
||
| 49 | /** @var int */ |
||
| 50 | private $skip = 0; |
||
| 51 | /** @var int */ |
||
| 52 | private $pageSize = 10; |
||
| 53 | /** @var string */ |
||
| 54 | private $collection; |
||
| 55 | /** @var string */ |
||
| 56 | private $area; |
||
| 57 | /** @var string */ |
||
| 58 | private $biasingProfile; |
||
| 59 | /** @var string */ |
||
| 60 | private $language; |
||
| 61 | /** @var MSort[] */ |
||
| 62 | private $sort; |
||
| 63 | /** @var CustomUrlParam[] */ |
||
| 64 | private $customUrlParams = array(); |
||
| 65 | /** @var Navigation[] */ |
||
| 66 | private $navigations = array(); |
||
| 67 | /** @var string[] */ |
||
| 68 | private $includedNavigations = array(); |
||
| 69 | /** @var string[] */ |
||
| 70 | private $excludedNavigations = array(); |
||
| 71 | /** @var string[] */ |
||
| 72 | private $fields = array(); |
||
| 73 | /** @var string[] */ |
||
| 74 | private $orFields = array(); |
||
| 75 | /** @var bool */ |
||
| 76 | private $pruneRefinements = true; |
||
| 77 | /** @var bool */ |
||
| 78 | private $disableAutocorrection = false; |
||
| 79 | /** @var bool */ |
||
| 80 | private $wildcardSearchEnabled = false; |
||
| 81 | // Removed until CBOR support for serialization / de-serialization improves |
||
| 82 | // /** @var bool */ |
||
| 83 | // private $returnBinary = false; |
||
| 84 | /** @var RestrictNavigation */ |
||
| 85 | private $restrictNavigation; |
||
| 86 | /** @var MBiasing */ |
||
| 87 | private $biasing; |
||
| 88 | |||
| 89 | /** @var Serializer */ |
||
| 90 | private $serializer; |
||
| 91 | |||
| 92 | const TILDE_REGEX = "/~((?=[\\w]*[=:]))/"; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param mixed $request |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | private function requestToJson($request) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $clientKey Your client key. |
||
| 113 | * |
||
| 114 | * @return string JSON representation of request to Bridge. |
||
| 115 | */ |
||
| 116 | public function getBridgeJson($clientKey) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $clientKey Your client key. |
||
| 124 | * @param string $navigationName Name of the navigation to get refinements for |
||
| 125 | * |
||
| 126 | * @return string JSON representation of request to Bridge. |
||
| 127 | */ |
||
| 128 | public function getBridgeRefinementsJson($clientKey, $navigationName) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param string $clientKey |
||
| 138 | * |
||
| 139 | * @return Request |
||
| 140 | */ |
||
| 141 | private function populateRequest($clientKey) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param Navigation[] $navigations |
||
| 201 | * |
||
| 202 | * @return Refinement[] |
||
| 203 | */ |
||
| 204 | private function generateSelectedRefinements($navigations) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param string $clientKey Your client key. |
||
| 243 | * |
||
| 244 | * @return string JSON representation of request to Bridge. |
||
| 245 | */ |
||
| 246 | public function getBridgeJsonRefinementSearch($clientKey) |
||
| 261 | |||
| 262 | public function __construct() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return string The current search string. |
||
| 269 | */ |
||
| 270 | public function getQuery() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param string $query The search string. |
||
| 277 | */ |
||
| 278 | public function setQuery($query) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return string The data sub-collection. |
||
| 285 | */ |
||
| 286 | public function getCollection() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $collection The string representation of a collection query. |
||
| 293 | */ |
||
| 294 | public function setCollection($collection) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return string The area name. |
||
| 301 | */ |
||
| 302 | public function getArea() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param string $area The area name. |
||
| 309 | */ |
||
| 310 | public function setArea($area) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return string[] A list of metadata fields that will be returned by the search engine. |
||
| 317 | */ |
||
| 318 | public function getFields() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return string[] A list of the fields that the search service will treat as OR'able. |
||
| 325 | */ |
||
| 326 | public function getOrFields() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string[] $fields A list of case-sensitive names of the attributes to return. |
||
| 333 | */ |
||
| 334 | public function addFields($fields) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @return string[] A list of which navigations to return from the bridge. |
||
| 341 | */ |
||
| 342 | public function getIncludedNavigations() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string[] $navigations A list of which navigations to return from the bridge. |
||
| 349 | */ |
||
| 350 | public function addIncludedNavigations($navigations) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return string[] A list of which navigations to not return from the bridge. |
||
| 357 | */ |
||
| 358 | public function getExcludedNavigations() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param string[] $navigations A list of which navigations to not return from the bridge. |
||
| 365 | */ |
||
| 366 | public function addExcludedNavigations($navigations) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @return Navigation[] |
||
| 373 | */ |
||
| 374 | public function &getNavigations() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param Navigation[] $navigations |
||
| 381 | */ |
||
| 382 | public function setNavigations($navigations) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param string $name The case-sensitive name of the attribute to return. |
||
| 389 | */ |
||
| 390 | public function addField($name) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param string $name Field that should be treated as OR. |
||
| 397 | */ |
||
| 398 | public function addOrField($name) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param string[] $fields A list of fields that should be treated as OR. |
||
| 405 | */ |
||
| 406 | public function addOrFields($fields) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param string $name The parameter name. |
||
| 413 | * @param string $value The parameter value. |
||
| 414 | */ |
||
| 415 | public function addCustomUrlParamByName($name, $value) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param CustomUrlParam $param Set an additional parameter that can be used to trigger rules. |
||
| 423 | */ |
||
| 424 | public function addCustomUrlParam($param) |
||
| 428 | |||
| 429 | public function splitRefinements($refinementString) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param string $refinementString A tilde separated list of refinements. |
||
| 439 | */ |
||
| 440 | public function addRefinementsByString($refinementString) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param string $navigationName The name of the Navigation. |
||
| 486 | * @param Refinement $refinement A RefinementRange or RefinementValue object. |
||
| 487 | */ |
||
| 488 | public function addRefinement($navigationName, $refinement) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @param string $navigationName The name of the refinement. |
||
| 505 | * @param mixed $low The low value. |
||
| 506 | * @param mixed $high The high value. |
||
| 507 | * @param bool $exclude True if the results should exclude this range refinement, false otherwise. |
||
| 508 | */ |
||
| 509 | public function addRangeRefinement($navigationName, $low, $high, $exclude = false) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @param string $navigationName The name of the refinement. |
||
| 517 | * @param mixed $value The refinement value. |
||
| 518 | * @param bool $exclude True if the results should exclude this value refinement, false otherwise. |
||
| 519 | */ |
||
| 520 | public function addValueRefinement($navigationName, $value, $exclude = false) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @return bool Are refinements with zero counts being removed. |
||
| 528 | */ |
||
| 529 | public function isPruneRefinements() |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param bool $pruneRefinements Specifies whether refinements should be pruned. |
||
| 536 | */ |
||
| 537 | public function setPruneRefinements($pruneRefinements) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @return MSort[] The current list of sort parameters. |
||
| 544 | */ |
||
| 545 | public function &getSort() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param MSort[] $sort Any number of sort criteria. |
||
| 552 | */ |
||
| 553 | public function setSort($sort) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @return int The number of documents to skip. |
||
| 560 | */ |
||
| 561 | public function getSkip() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @param int $skip The number of documents to skip. |
||
| 568 | */ |
||
| 569 | public function setSkip($skip) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @return CustomUrlParam[] A list of custom url params. |
||
| 576 | */ |
||
| 577 | public function getCustomUrlParams() |
||
| 581 | |||
| 582 | /** |
||
| 583 | * @param CustomUrlParam[] $customUrlParams Set the custom url params. |
||
| 584 | */ |
||
| 585 | public function setCustomUrlParams($customUrlParams) |
||
| 589 | |||
| 590 | // /** |
||
| 591 | // * @return bool Is return JSON set to true. |
||
| 592 | // */ |
||
| 593 | // public function isReturnBinary() |
||
| 594 | // { |
||
| 595 | // return $this->returnBinary; |
||
| 596 | // } |
||
| 597 | // |
||
| 598 | // /** |
||
| 599 | // * @param bool $returnBinary Whether to tell the bridge to return binary data rather than JSON. |
||
| 600 | // */ |
||
| 601 | // public function setReturnBinary($returnBinary) |
||
| 602 | // { |
||
| 603 | // $this->returnBinary = $returnBinary; |
||
| 604 | // } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * @return string The current language restrict value. |
||
| 608 | */ |
||
| 609 | public function getLanguage() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @param string $language The value for language restrict. |
||
| 616 | */ |
||
| 617 | public function setLanguage($language) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * @return string The current biasing profile name. |
||
| 624 | */ |
||
| 625 | public function getBiasingProfile() |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @param string $biasingProfile Override the biasing profile used for this query. |
||
| 632 | */ |
||
| 633 | public function setBiasingProfile($biasingProfile) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @return int The current page size. |
||
| 640 | */ |
||
| 641 | public function getPageSize() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @param int $pageSize The number of records to return with the query. |
||
| 648 | */ |
||
| 649 | public function setPageSize($pageSize) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * @return boolean |
||
| 656 | */ |
||
| 657 | public function isDisableAutocorrection() |
||
| 661 | |||
| 662 | /** |
||
| 663 | * @param boolean $disableAutocorrection Specifies whether the auto-correction behavior should be disabled. |
||
| 664 | * By default, when no results are returned for the given query (and there is |
||
| 665 | * a did-you-mean available), the first did-you-mean is automatically queried |
||
| 666 | * instead. |
||
| 667 | */ |
||
| 668 | public function setDisableAutocorrection($disableAutocorrection) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @return boolean |
||
| 675 | */ |
||
| 676 | public function isWildcardSearchEnabled() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @param boolean $wildcardSearchEnabled Indicate if the *(star) character in the search string should be treated |
||
| 683 | * as a wildcard prefix search. For example, `sta*` will match `star` and |
||
| 684 | * `start`. |
||
| 685 | */ |
||
| 686 | public function setWildcardSearchEnabled($wildcardSearchEnabled) |
||
| 690 | |||
| 691 | /** |
||
| 692 | * <b>Warning</b> This will count as two queries against your search index. |
||
| 693 | * |
||
| 694 | * Typically, this feature is used when you have a large number of navigation items that will overwhelm the end |
||
| 695 | * user. It works by using one of the existing navigation items to decide what the query is about and fires a second |
||
| 696 | * query to restrict the navigation to the most relevant set of navigation items for this search term. |
||
| 697 | * |
||
| 698 | * For example, if you pass in a search of `paper` and a restrict navigation of `category:2` |
||
| 699 | * |
||
| 700 | * The bridge will find the category navigation refinements in the first query and fire a second query for the top 2 |
||
| 701 | * most populous categories. Therefore, a search for something generic like "paper" will bring back top category |
||
| 702 | * matches like copy paper (1,030), paper pads (567). The bridge will fire off the second query with the search |
||
| 703 | * term, plus an OR refinement with the most likely categories. The navigation items in the first query are |
||
| 704 | * entirely replaced with the navigation items in the second query, except for the navigation that was used for the |
||
| 705 | * restriction so that users still have the ability to navigate by all category types. |
||
| 706 | * |
||
| 707 | * @param RestrictNavigation $restrictNavigation Restriction criteria |
||
| 708 | */ |
||
| 709 | public function setRestrictNavigation($restrictNavigation) |
||
| 713 | |||
| 714 | /** @return RestrictNavigation */ |
||
| 715 | public function getRestrictNavigation() |
||
| 719 | |||
| 720 | /** |
||
| 721 | * @return MBiasing |
||
| 722 | */ |
||
| 723 | public function getBiasing() |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Add a biasing profile, which is defined at query time. |
||
| 730 | * |
||
| 731 | * @param MBiasing $biasing |
||
| 732 | */ |
||
| 733 | public function setBiasing($biasing) |
||
| 737 | |||
| 738 | /** |
||
| 739 | * @param string[] $bringToTop |
||
| 740 | */ |
||
| 741 | public function setBringToTop($bringToTop) { |
||
| 747 | |||
| 748 | /** |
||
| 749 | * @param boolean $augment |
||
| 750 | */ |
||
| 751 | public function setBiasingAugment($augment) { |
||
| 757 | |||
| 758 | /** |
||
| 759 | * @param float $influence |
||
| 760 | */ |
||
| 761 | public function setInfluence($influence) { |
||
| 767 | |||
| 768 | /** |
||
| 769 | * @return string A string representation of all of the currently set refinements. |
||
| 770 | */ |
||
| 771 | View Code Duplication | public function getRefinementString() |
|
| 786 | |||
| 787 | /** |
||
| 788 | * @return string A string representation of all of the currently set custom url parameters. |
||
| 789 | */ |
||
| 790 | View Code Duplication | public function getCustomUrlParamsString() |
|
| 803 | |||
| 804 | /** |
||
| 805 | * @param MSort $sort |
||
| 806 | * |
||
| 807 | * @return RSort |
||
| 808 | */ |
||
| 809 | protected static function convertSort($sort) |
||
| 827 | |||
| 828 | /** |
||
| 829 | * @param MMatchStrategy $strategy |
||
| 830 | * |
||
| 831 | * @return RMatchStrategy |
||
| 832 | */ |
||
| 833 | protected static function convertPartialMatchStrategy($strategy) |
||
| 850 | |||
| 851 | /** |
||
| 852 | * @param MPartialMatchRule $rule |
||
| 853 | * |
||
| 854 | * @return RPartialMatchRule |
||
| 855 | */ |
||
| 856 | protected static function convertPartialMatchRule($rule) |
||
| 869 | |||
| 870 | /** |
||
| 871 | * @param MBias $bias |
||
| 872 | * |
||
| 873 | * @return Bias |
||
| 874 | */ |
||
| 875 | protected static function convertBias($bias) |
||
| 879 | |||
| 880 | /** |
||
| 881 | * @param MBias[] $biases |
||
| 882 | * |
||
| 883 | * @return Bias[] |
||
| 884 | */ |
||
| 885 | protected static function convertBiases($biases) |
||
| 889 | |||
| 890 | /** |
||
| 891 | * @param MBiasing $biasing |
||
| 892 | * |
||
| 893 | * @return Biasing |
||
| 894 | */ |
||
| 895 | protected static function convertBiasing($biasing) |
||
| 921 | |||
| 922 | } |
||
| 923 |