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 |
||
| 46 | class Query |
||
|
|
|||
| 47 | { |
||
| 48 | /** @var string */ |
||
| 49 | private $userId; |
||
| 50 | /** @var string */ |
||
| 51 | private $query; |
||
| 52 | /** @var int */ |
||
| 53 | private $skip = 0; |
||
| 54 | /** @var int */ |
||
| 55 | private $pageSize = 10; |
||
| 56 | /** @var string */ |
||
| 57 | private $collection; |
||
| 58 | /** @var string */ |
||
| 59 | private $area; |
||
| 60 | /** @var string */ |
||
| 61 | private $biasingProfile; |
||
| 62 | /** @var string */ |
||
| 63 | private $language; |
||
| 64 | /** @var MSort[] */ |
||
| 65 | private $sort; |
||
| 66 | /** @var CustomUrlParam[] */ |
||
| 67 | private $customUrlParams = array(); |
||
| 68 | /** @var Navigation[] */ |
||
| 69 | private $navigations = array(); |
||
| 70 | /** @var string[] */ |
||
| 71 | private $includedNavigations = array(); |
||
| 72 | /** @var string[] */ |
||
| 73 | private $excludedNavigations = array(); |
||
| 74 | /** @var string[] */ |
||
| 75 | private $fields = array(); |
||
| 76 | /** @var string[] */ |
||
| 77 | private $orFields = array(); |
||
| 78 | /** @var bool */ |
||
| 79 | private $pruneRefinements = true; |
||
| 80 | /** @var bool */ |
||
| 81 | private $disableAutocorrection = false; |
||
| 82 | /** @var bool */ |
||
| 83 | private $wildcardSearchEnabled = false; |
||
| 84 | // Removed until CBOR support for serialization / de-serialization improves |
||
| 85 | // /** @var bool */ |
||
| 86 | // private $returnBinary = false; |
||
| 87 | /** @var RestrictNavigation */ |
||
| 88 | private $restrictNavigation; |
||
| 89 | /** @var MBiasing */ |
||
| 90 | private $biasing; |
||
| 91 | |||
| 92 | /** @var Serializer */ |
||
| 93 | private $serializer; |
||
| 94 | |||
| 95 | const TILDE_REGEX = "/~((?=[\\w]*[=:]))/"; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param mixed $request |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | private function requestToJson($request) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $clientKey Your client key. |
||
| 116 | * |
||
| 117 | * @return string JSON representation of request to Bridge. |
||
| 118 | */ |
||
| 119 | public function getBridgeJson($clientKey) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $clientKey Your client key. |
||
| 127 | * @param string $navigationName Name of the navigation to get refinements for |
||
| 128 | * |
||
| 129 | * @return string JSON representation of request to Bridge. |
||
| 130 | */ |
||
| 131 | public function getBridgeRefinementsJson($clientKey, $navigationName) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $clientKey |
||
| 141 | * |
||
| 142 | * @return Request |
||
| 143 | */ |
||
| 144 | private function populateRequest($clientKey) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param Navigation[] $navigations |
||
| 205 | * |
||
| 206 | * @return Refinement[] |
||
| 207 | */ |
||
| 208 | private function generateSelectedRefinements($navigations) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $clientKey Your client key. |
||
| 247 | * |
||
| 248 | * @return string JSON representation of request to Bridge. |
||
| 249 | */ |
||
| 250 | public function getBridgeJsonRefinementSearch($clientKey) |
||
| 265 | |||
| 266 | public function __construct() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @return string The current search string. |
||
| 273 | */ |
||
| 274 | public function getQuery() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $query The search string. |
||
| 281 | */ |
||
| 282 | public function setQuery($query) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return string The data sub-collection. |
||
| 289 | */ |
||
| 290 | public function getCollection() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $collection The string representation of a collection query. |
||
| 297 | */ |
||
| 298 | public function setCollection($collection) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return string The area name. |
||
| 305 | */ |
||
| 306 | public function getArea() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $area The area name. |
||
| 313 | */ |
||
| 314 | public function setArea($area) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return string[] A list of metadata fields that will be returned by the search engine. |
||
| 321 | */ |
||
| 322 | public function getFields() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return string[] A list of the fields that the search service will treat as OR'able. |
||
| 329 | */ |
||
| 330 | public function getOrFields() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param string[] $fields A list of case-sensitive names of the attributes to return. |
||
| 337 | */ |
||
| 338 | public function addFields($fields) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return string[] A list of which navigations to return from the bridge. |
||
| 345 | */ |
||
| 346 | public function getIncludedNavigations() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param string[] $navigations A list of which navigations to return from the bridge. |
||
| 353 | */ |
||
| 354 | public function addIncludedNavigations($navigations) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return string[] A list of which navigations to not return from the bridge. |
||
| 361 | */ |
||
| 362 | public function getExcludedNavigations() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string[] $navigations A list of which navigations to not return from the bridge. |
||
| 369 | */ |
||
| 370 | public function addExcludedNavigations($navigations) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return Navigation[] |
||
| 377 | */ |
||
| 378 | public function &getNavigations() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param Navigation[] $navigations |
||
| 385 | */ |
||
| 386 | public function setNavigations($navigations) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $name The case-sensitive name of the attribute to return. |
||
| 393 | */ |
||
| 394 | public function addField($name) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param string $name Field that should be treated as OR. |
||
| 401 | */ |
||
| 402 | public function addOrField($name) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param string[] $fields A list of fields that should be treated as OR. |
||
| 409 | */ |
||
| 410 | public function addOrFields($fields) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param string $name The parameter name. |
||
| 417 | * @param string $value The parameter value. |
||
| 418 | */ |
||
| 419 | public function addCustomUrlParamByName($name, $value) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param CustomUrlParam $param Set an additional parameter that can be used to trigger rules. |
||
| 427 | */ |
||
| 428 | public function addCustomUrlParam($param) |
||
| 432 | |||
| 433 | public function splitRefinements($refinementString) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param string $refinementString A tilde separated list of refinements. |
||
| 443 | */ |
||
| 444 | public function addRefinementsByString($refinementString) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @param string $navigationName The name of the Navigation. |
||
| 490 | * @param Refinement $refinement A RefinementRange or RefinementValue object. |
||
| 491 | */ |
||
| 492 | public function addRefinement($navigationName, $refinement) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param string $navigationName The name of the refinement. |
||
| 509 | * @param mixed $low The low value. |
||
| 510 | * @param mixed $high The high value. |
||
| 511 | * @param bool $exclude True if the results should exclude this range refinement, false otherwise. |
||
| 512 | */ |
||
| 513 | public function addRangeRefinement($navigationName, $low, $high, $exclude = false) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param string $navigationName The name of the refinement. |
||
| 521 | * @param mixed $value The refinement value. |
||
| 522 | * @param bool $exclude True if the results should exclude this value refinement, false otherwise. |
||
| 523 | */ |
||
| 524 | public function addValueRefinement($navigationName, $value, $exclude = false) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @return bool Are refinements with zero counts being removed. |
||
| 532 | */ |
||
| 533 | public function isPruneRefinements() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param bool $pruneRefinements Specifies whether refinements should be pruned. |
||
| 540 | */ |
||
| 541 | public function setPruneRefinements($pruneRefinements) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @return MSort[] The current list of sort parameters. |
||
| 548 | */ |
||
| 549 | public function &getSort() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param MSort[] $sort Any number of sort criteria. |
||
| 556 | */ |
||
| 557 | public function setSort($sort) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @return int The number of documents to skip. |
||
| 564 | */ |
||
| 565 | public function getSkip() |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @param int $skip The number of documents to skip. |
||
| 572 | */ |
||
| 573 | public function setSkip($skip) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @return CustomUrlParam[] A list of custom url params. |
||
| 580 | */ |
||
| 581 | public function getCustomUrlParams() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * @param CustomUrlParam[] $customUrlParams Set the custom url params. |
||
| 588 | */ |
||
| 589 | public function setCustomUrlParams($customUrlParams) |
||
| 593 | |||
| 594 | // /** |
||
| 595 | // * @return bool Is return JSON set to true. |
||
| 596 | // */ |
||
| 597 | // public function isReturnBinary() |
||
| 598 | // { |
||
| 599 | // return $this->returnBinary; |
||
| 600 | // } |
||
| 601 | // |
||
| 602 | // /** |
||
| 603 | // * @param bool $returnBinary Whether to tell the bridge to return binary data rather than JSON. |
||
| 604 | // */ |
||
| 605 | // public function setReturnBinary($returnBinary) |
||
| 606 | // { |
||
| 607 | // $this->returnBinary = $returnBinary; |
||
| 608 | // } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @return string The current language restrict value. |
||
| 612 | */ |
||
| 613 | public function getLanguage() |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @param string $language The value for language restrict. |
||
| 620 | */ |
||
| 621 | public function setLanguage($language) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @return string The current biasing profile name. |
||
| 628 | */ |
||
| 629 | public function getBiasingProfile() |
||
| 633 | |||
| 634 | /** |
||
| 635 | * @param string $biasingProfile Override the biasing profile used for this query. |
||
| 636 | */ |
||
| 637 | public function setBiasingProfile($biasingProfile) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * @return int The current page size. |
||
| 644 | */ |
||
| 645 | public function getPageSize() |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @param int $pageSize The number of records to return with the query. |
||
| 652 | */ |
||
| 653 | public function setPageSize($pageSize) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @return boolean |
||
| 660 | */ |
||
| 661 | public function isDisableAutocorrection() |
||
| 665 | |||
| 666 | /** |
||
| 667 | * @param boolean $disableAutocorrection Specifies whether the auto-correction behavior should be disabled. |
||
| 668 | * By default, when no results are returned for the given query (and there is |
||
| 669 | * a did-you-mean available), the first did-you-mean is automatically queried |
||
| 670 | * instead. |
||
| 671 | */ |
||
| 672 | public function setDisableAutocorrection($disableAutocorrection) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * @return boolean |
||
| 679 | */ |
||
| 680 | public function isWildcardSearchEnabled() |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @param boolean $wildcardSearchEnabled Indicate if the *(star) character in the search string should be treated |
||
| 687 | * as a wildcard prefix search. For example, `sta*` will match `star` and |
||
| 688 | * `start`. |
||
| 689 | */ |
||
| 690 | public function setWildcardSearchEnabled($wildcardSearchEnabled) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * <b>Warning</b> This will count as two queries against your search index. |
||
| 697 | * |
||
| 698 | * Typically, this feature is used when you have a large number of navigation items that will overwhelm the end |
||
| 699 | * user. It works by using one of the existing navigation items to decide what the query is about and fires a second |
||
| 700 | * query to restrict the navigation to the most relevant set of navigation items for this search term. |
||
| 701 | * |
||
| 702 | * For example, if you pass in a search of `paper` and a restrict navigation of `category:2` |
||
| 703 | * |
||
| 704 | * The bridge will find the category navigation refinements in the first query and fire a second query for the top 2 |
||
| 705 | * most populous categories. Therefore, a search for something generic like "paper" will bring back top category |
||
| 706 | * matches like copy paper (1,030), paper pads (567). The bridge will fire off the second query with the search |
||
| 707 | * term, plus an OR refinement with the most likely categories. The navigation items in the first query are |
||
| 708 | * entirely replaced with the navigation items in the second query, except for the navigation that was used for the |
||
| 709 | * restriction so that users still have the ability to navigate by all category types. |
||
| 710 | * |
||
| 711 | * @param RestrictNavigation $restrictNavigation Restriction criteria |
||
| 712 | */ |
||
| 713 | public function setRestrictNavigation($restrictNavigation) |
||
| 717 | |||
| 718 | /** @return RestrictNavigation */ |
||
| 719 | public function getRestrictNavigation() |
||
| 723 | |||
| 724 | /** |
||
| 725 | * @return MBiasing |
||
| 726 | */ |
||
| 727 | public function getBiasing() |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Add a biasing profile, which is defined at query time. |
||
| 734 | * |
||
| 735 | * @param MBiasing $biasing |
||
| 736 | */ |
||
| 737 | public function setBiasing($biasing) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * @param string[] $bringToTop |
||
| 744 | */ |
||
| 745 | public function setBringToTop($bringToTop) |
||
| 752 | |||
| 753 | /** |
||
| 754 | * @param boolean $augment |
||
| 755 | */ |
||
| 756 | public function setBiasingAugment($augment) |
||
| 763 | |||
| 764 | /** |
||
| 765 | * @param float $influence |
||
| 766 | */ |
||
| 767 | public function setInfluence($influence) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * @return string A string representation of all of the currently set refinements. |
||
| 777 | */ |
||
| 778 | View Code Duplication | public function getRefinementString() |
|
| 793 | |||
| 794 | /** |
||
| 795 | * @return string A string representation of all of the currently set custom url parameters. |
||
| 796 | */ |
||
| 797 | View Code Duplication | public function getCustomUrlParamsString() |
|
| 810 | |||
| 811 | /** |
||
| 812 | * @param MSort $sort |
||
| 813 | * |
||
| 814 | * @return RSort |
||
| 815 | */ |
||
| 816 | protected static function convertSort($sort) |
||
| 834 | |||
| 835 | /** |
||
| 836 | * @param MMatchStrategy $strategy |
||
| 837 | * |
||
| 838 | * @return RMatchStrategy |
||
| 839 | */ |
||
| 840 | protected static function convertPartialMatchStrategy($strategy) |
||
| 857 | |||
| 858 | /** |
||
| 859 | * @param MPartialMatchRule $rule |
||
| 860 | * |
||
| 861 | * @return RPartialMatchRule |
||
| 862 | */ |
||
| 863 | protected static function convertPartialMatchRule($rule) |
||
| 876 | |||
| 877 | /** |
||
| 878 | * @param MBias $bias |
||
| 879 | * |
||
| 880 | * @return Bias |
||
| 881 | */ |
||
| 882 | protected static function convertBias($bias) |
||
| 886 | |||
| 887 | /** |
||
| 888 | * @param MBias[] $biases |
||
| 889 | * |
||
| 890 | * @return Bias[] |
||
| 891 | */ |
||
| 892 | protected static function convertBiases($biases) |
||
| 896 | |||
| 897 | /** |
||
| 898 | * @param MNumericBoost $boost |
||
| 899 | * |
||
| 900 | * @return RNumericBoost |
||
| 901 | */ |
||
| 902 | protected static function convertNumericBoost($boost) { |
||
| 905 | |||
| 906 | /** |
||
| 907 | * @param MNumericBoost[] $boosts |
||
| 908 | * |
||
| 909 | * @return RNumericBoost[] |
||
| 910 | */ |
||
| 911 | protected static function convertNumericBoosts($boosts) { |
||
| 914 | |||
| 915 | /** |
||
| 916 | * @param MBiasing $biasing |
||
| 917 | * |
||
| 918 | * @return Biasing |
||
| 919 | */ |
||
| 920 | protected static function convertBiasing($biasing) |
||
| 949 | |||
| 950 | /** |
||
| 951 | * @return string |
||
| 952 | */ |
||
| 953 | public function getUserId() |
||
| 957 | |||
| 958 | /** |
||
| 959 | * @param string $userId |
||
| 960 | * |
||
| 961 | * @return Query |
||
| 962 | */ |
||
| 963 | public function setUserId($userId) |
||
| 968 | |||
| 969 | } |
||
| 970 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.