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