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 $userId; |
||
| 48 | /** @var string */ |
||
| 49 | private $query; |
||
| 50 | /** @var int */ |
||
| 51 | private $skip = 0; |
||
| 52 | /** @var int */ |
||
| 53 | private $pageSize = 10; |
||
| 54 | /** @var string */ |
||
| 55 | private $collection; |
||
| 56 | /** @var string */ |
||
| 57 | private $area; |
||
| 58 | /** @var string */ |
||
| 59 | private $biasingProfile; |
||
| 60 | /** @var string */ |
||
| 61 | private $language; |
||
| 62 | /** @var MSort[] */ |
||
| 63 | private $sort; |
||
| 64 | /** @var CustomUrlParam[] */ |
||
| 65 | private $customUrlParams = array(); |
||
| 66 | /** @var Navigation[] */ |
||
| 67 | private $navigations = array(); |
||
| 68 | /** @var string[] */ |
||
| 69 | private $includedNavigations = array(); |
||
| 70 | /** @var string[] */ |
||
| 71 | private $excludedNavigations = array(); |
||
| 72 | /** @var string[] */ |
||
| 73 | private $fields = array(); |
||
| 74 | /** @var string[] */ |
||
| 75 | private $orFields = array(); |
||
| 76 | /** @var bool */ |
||
| 77 | private $pruneRefinements = true; |
||
| 78 | /** @var bool */ |
||
| 79 | private $disableAutocorrection = false; |
||
| 80 | /** @var bool */ |
||
| 81 | private $wildcardSearchEnabled = false; |
||
| 82 | // Removed until CBOR support for serialization / de-serialization improves |
||
| 83 | // /** @var bool */ |
||
| 84 | // private $returnBinary = false; |
||
| 85 | /** @var RestrictNavigation */ |
||
| 86 | private $restrictNavigation; |
||
| 87 | /** @var MBiasing */ |
||
| 88 | private $biasing; |
||
| 89 | |||
| 90 | /** @var Serializer */ |
||
| 91 | private $serializer; |
||
| 92 | |||
| 93 | const TILDE_REGEX = "/~((?=[\\w]*[=:]))/"; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param mixed $request |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | private function requestToJson($request) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $clientKey Your client key. |
||
| 114 | * |
||
| 115 | * @return string JSON representation of request to Bridge. |
||
| 116 | */ |
||
| 117 | public function getBridgeJson($clientKey) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $clientKey Your client key. |
||
| 125 | * @param string $navigationName Name of the navigation to get refinements for |
||
| 126 | * |
||
| 127 | * @return string JSON representation of request to Bridge. |
||
| 128 | */ |
||
| 129 | public function getBridgeRefinementsJson($clientKey, $navigationName) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $clientKey |
||
| 139 | * |
||
| 140 | * @return Request |
||
| 141 | */ |
||
| 142 | private function populateRequest($clientKey) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param Navigation[] $navigations |
||
| 203 | * |
||
| 204 | * @return Refinement[] |
||
| 205 | */ |
||
| 206 | private function generateSelectedRefinements($navigations) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $clientKey Your client key. |
||
| 245 | * |
||
| 246 | * @return string JSON representation of request to Bridge. |
||
| 247 | */ |
||
| 248 | public function getBridgeJsonRefinementSearch($clientKey) |
||
| 263 | |||
| 264 | public function __construct() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return string The current search string. |
||
| 271 | */ |
||
| 272 | public function getQuery() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $query The search string. |
||
| 279 | */ |
||
| 280 | public function setQuery($query) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return string The data sub-collection. |
||
| 287 | */ |
||
| 288 | public function getCollection() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $collection The string representation of a collection query. |
||
| 295 | */ |
||
| 296 | public function setCollection($collection) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return string The area name. |
||
| 303 | */ |
||
| 304 | public function getArea() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $area The area name. |
||
| 311 | */ |
||
| 312 | public function setArea($area) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return string[] A list of metadata fields that will be returned by the search engine. |
||
| 319 | */ |
||
| 320 | public function getFields() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return string[] A list of the fields that the search service will treat as OR'able. |
||
| 327 | */ |
||
| 328 | public function getOrFields() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string[] $fields A list of case-sensitive names of the attributes to return. |
||
| 335 | */ |
||
| 336 | public function addFields($fields) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @return string[] A list of which navigations to return from the bridge. |
||
| 343 | */ |
||
| 344 | public function getIncludedNavigations() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param string[] $navigations A list of which navigations to return from the bridge. |
||
| 351 | */ |
||
| 352 | public function addIncludedNavigations($navigations) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return string[] A list of which navigations to not return from the bridge. |
||
| 359 | */ |
||
| 360 | public function getExcludedNavigations() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param string[] $navigations A list of which navigations to not return from the bridge. |
||
| 367 | */ |
||
| 368 | public function addExcludedNavigations($navigations) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return Navigation[] |
||
| 375 | */ |
||
| 376 | public function &getNavigations() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param Navigation[] $navigations |
||
| 383 | */ |
||
| 384 | public function setNavigations($navigations) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $name The case-sensitive name of the attribute to return. |
||
| 391 | */ |
||
| 392 | public function addField($name) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $name Field that should be treated as OR. |
||
| 399 | */ |
||
| 400 | public function addOrField($name) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string[] $fields A list of fields that should be treated as OR. |
||
| 407 | */ |
||
| 408 | public function addOrFields($fields) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param string $name The parameter name. |
||
| 415 | * @param string $value The parameter value. |
||
| 416 | */ |
||
| 417 | public function addCustomUrlParamByName($name, $value) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param CustomUrlParam $param Set an additional parameter that can be used to trigger rules. |
||
| 425 | */ |
||
| 426 | public function addCustomUrlParam($param) |
||
| 430 | |||
| 431 | public function splitRefinements($refinementString) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param string $refinementString A tilde separated list of refinements. |
||
| 441 | */ |
||
| 442 | public function addRefinementsByString($refinementString) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param string $navigationName The name of the Navigation. |
||
| 488 | * @param Refinement $refinement A RefinementRange or RefinementValue object. |
||
| 489 | */ |
||
| 490 | public function addRefinement($navigationName, $refinement) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @param string $navigationName The name of the refinement. |
||
| 507 | * @param mixed $low The low value. |
||
| 508 | * @param mixed $high The high value. |
||
| 509 | * @param bool $exclude True if the results should exclude this range refinement, false otherwise. |
||
| 510 | */ |
||
| 511 | public function addRangeRefinement($navigationName, $low, $high, $exclude = false) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @param string $navigationName The name of the refinement. |
||
| 519 | * @param mixed $value The refinement value. |
||
| 520 | * @param bool $exclude True if the results should exclude this value refinement, false otherwise. |
||
| 521 | */ |
||
| 522 | public function addValueRefinement($navigationName, $value, $exclude = false) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @return bool Are refinements with zero counts being removed. |
||
| 530 | */ |
||
| 531 | public function isPruneRefinements() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @param bool $pruneRefinements Specifies whether refinements should be pruned. |
||
| 538 | */ |
||
| 539 | public function setPruneRefinements($pruneRefinements) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @return MSort[] The current list of sort parameters. |
||
| 546 | */ |
||
| 547 | public function &getSort() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @param MSort[] $sort Any number of sort criteria. |
||
| 554 | */ |
||
| 555 | public function setSort($sort) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @return int The number of documents to skip. |
||
| 562 | */ |
||
| 563 | public function getSkip() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param int $skip The number of documents to skip. |
||
| 570 | */ |
||
| 571 | public function setSkip($skip) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @return CustomUrlParam[] A list of custom url params. |
||
| 578 | */ |
||
| 579 | public function getCustomUrlParams() |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @param CustomUrlParam[] $customUrlParams Set the custom url params. |
||
| 586 | */ |
||
| 587 | public function setCustomUrlParams($customUrlParams) |
||
| 591 | |||
| 592 | // /** |
||
| 593 | // * @return bool Is return JSON set to true. |
||
| 594 | // */ |
||
| 595 | // public function isReturnBinary() |
||
| 596 | // { |
||
| 597 | // return $this->returnBinary; |
||
| 598 | // } |
||
| 599 | // |
||
| 600 | // /** |
||
| 601 | // * @param bool $returnBinary Whether to tell the bridge to return binary data rather than JSON. |
||
| 602 | // */ |
||
| 603 | // public function setReturnBinary($returnBinary) |
||
| 604 | // { |
||
| 605 | // $this->returnBinary = $returnBinary; |
||
| 606 | // } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @return string The current language restrict value. |
||
| 610 | */ |
||
| 611 | public function getLanguage() |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @param string $language The value for language restrict. |
||
| 618 | */ |
||
| 619 | public function setLanguage($language) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * @return string The current biasing profile name. |
||
| 626 | */ |
||
| 627 | public function getBiasingProfile() |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @param string $biasingProfile Override the biasing profile used for this query. |
||
| 634 | */ |
||
| 635 | public function setBiasingProfile($biasingProfile) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @return int The current page size. |
||
| 642 | */ |
||
| 643 | public function getPageSize() |
||
| 647 | |||
| 648 | /** |
||
| 649 | * @param int $pageSize The number of records to return with the query. |
||
| 650 | */ |
||
| 651 | public function setPageSize($pageSize) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @return boolean |
||
| 658 | */ |
||
| 659 | public function isDisableAutocorrection() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param boolean $disableAutocorrection Specifies whether the auto-correction behavior should be disabled. |
||
| 666 | * By default, when no results are returned for the given query (and there is |
||
| 667 | * a did-you-mean available), the first did-you-mean is automatically queried |
||
| 668 | * instead. |
||
| 669 | */ |
||
| 670 | public function setDisableAutocorrection($disableAutocorrection) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @return boolean |
||
| 677 | */ |
||
| 678 | public function isWildcardSearchEnabled() |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @param boolean $wildcardSearchEnabled Indicate if the *(star) character in the search string should be treated |
||
| 685 | * as a wildcard prefix search. For example, `sta*` will match `star` and |
||
| 686 | * `start`. |
||
| 687 | */ |
||
| 688 | public function setWildcardSearchEnabled($wildcardSearchEnabled) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * <b>Warning</b> This will count as two queries against your search index. |
||
| 695 | * |
||
| 696 | * Typically, this feature is used when you have a large number of navigation items that will overwhelm the end |
||
| 697 | * user. It works by using one of the existing navigation items to decide what the query is about and fires a second |
||
| 698 | * query to restrict the navigation to the most relevant set of navigation items for this search term. |
||
| 699 | * |
||
| 700 | * For example, if you pass in a search of `paper` and a restrict navigation of `category:2` |
||
| 701 | * |
||
| 702 | * The bridge will find the category navigation refinements in the first query and fire a second query for the top 2 |
||
| 703 | * most populous categories. Therefore, a search for something generic like "paper" will bring back top category |
||
| 704 | * matches like copy paper (1,030), paper pads (567). The bridge will fire off the second query with the search |
||
| 705 | * term, plus an OR refinement with the most likely categories. The navigation items in the first query are |
||
| 706 | * entirely replaced with the navigation items in the second query, except for the navigation that was used for the |
||
| 707 | * restriction so that users still have the ability to navigate by all category types. |
||
| 708 | * |
||
| 709 | * @param RestrictNavigation $restrictNavigation Restriction criteria |
||
| 710 | */ |
||
| 711 | public function setRestrictNavigation($restrictNavigation) |
||
| 715 | |||
| 716 | /** @return RestrictNavigation */ |
||
| 717 | public function getRestrictNavigation() |
||
| 721 | |||
| 722 | /** |
||
| 723 | * @return MBiasing |
||
| 724 | */ |
||
| 725 | public function getBiasing() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Add a biasing profile, which is defined at query time. |
||
| 732 | * |
||
| 733 | * @param MBiasing $biasing |
||
| 734 | */ |
||
| 735 | public function setBiasing($biasing) |
||
| 739 | |||
| 740 | /** |
||
| 741 | * @param string[] $bringToTop |
||
| 742 | */ |
||
| 743 | public function setBringToTop($bringToTop) { |
||
| 749 | |||
| 750 | /** |
||
| 751 | * @param boolean $augment |
||
| 752 | */ |
||
| 753 | public function setBiasingAugment($augment) { |
||
| 759 | |||
| 760 | /** |
||
| 761 | * @param float $influence |
||
| 762 | */ |
||
| 763 | public function setInfluence($influence) { |
||
| 769 | |||
| 770 | /** |
||
| 771 | * @return string A string representation of all of the currently set refinements. |
||
| 772 | */ |
||
| 773 | View Code Duplication | public function getRefinementString() |
|
| 788 | |||
| 789 | /** |
||
| 790 | * @return string A string representation of all of the currently set custom url parameters. |
||
| 791 | */ |
||
| 792 | View Code Duplication | public function getCustomUrlParamsString() |
|
| 805 | |||
| 806 | /** |
||
| 807 | * @param MSort $sort |
||
| 808 | * |
||
| 809 | * @return RSort |
||
| 810 | */ |
||
| 811 | protected static function convertSort($sort) |
||
| 829 | |||
| 830 | /** |
||
| 831 | * @param MMatchStrategy $strategy |
||
| 832 | * |
||
| 833 | * @return RMatchStrategy |
||
| 834 | */ |
||
| 835 | protected static function convertPartialMatchStrategy($strategy) |
||
| 852 | |||
| 853 | /** |
||
| 854 | * @param MPartialMatchRule $rule |
||
| 855 | * |
||
| 856 | * @return RPartialMatchRule |
||
| 857 | */ |
||
| 858 | protected static function convertPartialMatchRule($rule) |
||
| 871 | |||
| 872 | /** |
||
| 873 | * @param MBias $bias |
||
| 874 | * |
||
| 875 | * @return Bias |
||
| 876 | */ |
||
| 877 | protected static function convertBias($bias) |
||
| 881 | |||
| 882 | /** |
||
| 883 | * @param MBias[] $biases |
||
| 884 | * |
||
| 885 | * @return Bias[] |
||
| 886 | */ |
||
| 887 | protected static function convertBiases($biases) |
||
| 891 | |||
| 892 | /** |
||
| 893 | * @param MBiasing $biasing |
||
| 894 | * |
||
| 895 | * @return Biasing |
||
| 896 | */ |
||
| 897 | protected static function convertBiasing($biasing) |
||
| 923 | |||
| 924 | /** |
||
| 925 | * @return string |
||
| 926 | */ |
||
| 927 | public function getUserId() |
||
| 931 | |||
| 932 | /** |
||
| 933 | * @param string $userId |
||
| 934 | * |
||
| 935 | * @return Query |
||
| 936 | */ |
||
| 937 | public function setUserId($userId) |
||
| 942 | |||
| 943 | } |
||
| 944 |