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 |
||
| 42 | class Query |
||
| 43 | { |
||
| 44 | /** @var string */ |
||
| 45 | private $query; |
||
| 46 | /** @var int */ |
||
| 47 | private $skip = 0; |
||
| 48 | /** @var int */ |
||
| 49 | private $pageSize = 10; |
||
| 50 | /** @var string */ |
||
| 51 | private $collection; |
||
| 52 | /** @var string */ |
||
| 53 | private $area; |
||
| 54 | /** @var string */ |
||
| 55 | private $biasingProfile; |
||
| 56 | /** @var string */ |
||
| 57 | private $language; |
||
| 58 | /** @var MSort[] */ |
||
| 59 | private $sort; |
||
| 60 | /** @var CustomUrlParam[] */ |
||
| 61 | private $customUrlParams = array(); |
||
| 62 | /** @var Navigation[] */ |
||
| 63 | private $navigations = array(); |
||
| 64 | /** @var string[] */ |
||
| 65 | private $includedNavigations = array(); |
||
| 66 | /** @var string[] */ |
||
| 67 | private $excludedNavigations = array(); |
||
| 68 | /** @var string[] */ |
||
| 69 | private $fields = array(); |
||
| 70 | /** @var string[] */ |
||
| 71 | private $orFields = array(); |
||
| 72 | /** @var bool */ |
||
| 73 | private $pruneRefinements = true; |
||
| 74 | /** @var bool */ |
||
| 75 | private $disableAutocorrection = false; |
||
| 76 | /** @var bool */ |
||
| 77 | private $wildcardSearchEnabled = false; |
||
| 78 | // Removed until CBOR support for serialization / de-serialization improves |
||
| 79 | // /** @var bool */ |
||
| 80 | // private $returnBinary = false; |
||
| 81 | /** @var RestrictNavigation */ |
||
| 82 | private $restrictNavigation; |
||
| 83 | /** @var MBiasing */ |
||
| 84 | private $biasing; |
||
| 85 | |||
| 86 | /** @var Serializer */ |
||
| 87 | private $serializer; |
||
| 88 | |||
| 89 | const TILDE_REGEX = "/~((?=[\\w]*[=:]))/"; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param mixed $request |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | private function requestToJson($request) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $clientKey Your client key. |
||
| 110 | * |
||
| 111 | * @return string JSON representation of request to Bridge. |
||
| 112 | */ |
||
| 113 | public function getBridgeJson($clientKey) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $clientKey Your client key. |
||
| 121 | * @param string $navigationName Name of the navigation to get refinements for |
||
| 122 | * |
||
| 123 | * @return string JSON representation of request to Bridge. |
||
| 124 | */ |
||
| 125 | public function getBridgeRefinementsJson($clientKey, $navigationName) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $clientKey |
||
| 135 | * |
||
| 136 | * @return Request |
||
| 137 | */ |
||
| 138 | private function populateRequest($clientKey) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param Navigation[] $navigations |
||
| 198 | * |
||
| 199 | * @return Refinement[] |
||
| 200 | */ |
||
| 201 | private function generateSelectedRefinements($navigations) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $clientKey Your client key. |
||
| 240 | * |
||
| 241 | * @return string JSON representation of request to Bridge. |
||
| 242 | */ |
||
| 243 | public function getBridgeJsonRefinementSearch($clientKey) |
||
| 258 | |||
| 259 | public function __construct() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return string The current search string. |
||
| 266 | */ |
||
| 267 | public function getQuery() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $query The search string. |
||
| 274 | */ |
||
| 275 | public function setQuery($query) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return string The data sub-collection. |
||
| 282 | */ |
||
| 283 | public function getCollection() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $collection The string representation of a collection query. |
||
| 290 | */ |
||
| 291 | public function setCollection($collection) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return string The area name. |
||
| 298 | */ |
||
| 299 | public function getArea() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $area The area name. |
||
| 306 | */ |
||
| 307 | public function setArea($area) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return string[] A list of metadata fields that will be returned by the search engine. |
||
| 314 | */ |
||
| 315 | public function getFields() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return string[] A list of the fields that the search service will treat as OR'able. |
||
| 322 | */ |
||
| 323 | public function getOrFields() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param string[] $fields A list of case-sensitive names of the attributes to return. |
||
| 330 | */ |
||
| 331 | public function addFields($fields) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return string[] A list of which navigations to return from the bridge. |
||
| 338 | */ |
||
| 339 | public function getIncludedNavigations() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param string[] $navigations A list of which navigations to return from the bridge. |
||
| 346 | */ |
||
| 347 | public function addIncludedNavigations($navigations) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return string[] A list of which navigations to not return from the bridge. |
||
| 354 | */ |
||
| 355 | public function getExcludedNavigations() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param string[] $navigations A list of which navigations to not return from the bridge. |
||
| 362 | */ |
||
| 363 | public function addExcludedNavigations($navigations) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return Navigation[] |
||
| 370 | */ |
||
| 371 | public function &getNavigations() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param Navigation[] $navigations |
||
| 378 | */ |
||
| 379 | public function setNavigations($navigations) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param string $name The case-sensitive name of the attribute to return. |
||
| 386 | */ |
||
| 387 | public function addField($name) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param string $name Field that should be treated as OR. |
||
| 394 | */ |
||
| 395 | public function addOrField($name) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param string[] $fields A list of fields that should be treated as OR. |
||
| 402 | */ |
||
| 403 | public function addOrFields($fields) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param string $name The parameter name. |
||
| 410 | * @param string $value The parameter value. |
||
| 411 | */ |
||
| 412 | public function addCustomUrlParamByName($name, $value) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param CustomUrlParam $param Set an additional parameter that can be used to trigger rules. |
||
| 420 | */ |
||
| 421 | public function addCustomUrlParam($param) |
||
| 425 | |||
| 426 | public function splitRefinements($refinementString) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $refinementString A tilde separated list of refinements. |
||
| 436 | */ |
||
| 437 | public function addRefinementsByString($refinementString) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @param string $navigationName The name of the Navigation. |
||
| 483 | * @param Refinement $refinement A RefinementRange or RefinementValue object. |
||
| 484 | */ |
||
| 485 | public function addRefinement($navigationName, $refinement) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param string $navigationName The name of the refinement. |
||
| 502 | * @param mixed $low The low value. |
||
| 503 | * @param mixed $high The high value. |
||
| 504 | * @param bool $exclude True if the results should exclude this range refinement, false otherwise. |
||
| 505 | */ |
||
| 506 | public function addRangeRefinement($navigationName, $low, $high, $exclude = false) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param string $navigationName The name of the refinement. |
||
| 514 | * @param mixed $value The refinement value. |
||
| 515 | * @param bool $exclude True if the results should exclude this value refinement, false otherwise. |
||
| 516 | */ |
||
| 517 | public function addValueRefinement($navigationName, $value, $exclude = false) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @return bool Are refinements with zero counts being removed. |
||
| 525 | */ |
||
| 526 | public function isPruneRefinements() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @param bool $pruneRefinements Specifies whether refinements should be pruned. |
||
| 533 | */ |
||
| 534 | public function setPruneRefinements($pruneRefinements) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @return MSort[] The current list of sort parameters. |
||
| 541 | */ |
||
| 542 | public function &getSort() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param MSort[] $sort Any number of sort criteria. |
||
| 549 | */ |
||
| 550 | public function setSort($sort) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @return int The number of documents to skip. |
||
| 557 | */ |
||
| 558 | public function getSkip() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @param int $skip The number of documents to skip. |
||
| 565 | */ |
||
| 566 | public function setSkip($skip) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @return CustomUrlParam[] A list of custom url params. |
||
| 573 | */ |
||
| 574 | public function getCustomUrlParams() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param CustomUrlParam[] $customUrlParams Set the custom url params. |
||
| 581 | */ |
||
| 582 | public function setCustomUrlParams($customUrlParams) |
||
| 586 | |||
| 587 | // /** |
||
| 588 | // * @return bool Is return JSON set to true. |
||
| 589 | // */ |
||
| 590 | // public function isReturnBinary() |
||
| 591 | // { |
||
| 592 | // return $this->returnBinary; |
||
| 593 | // } |
||
| 594 | // |
||
| 595 | // /** |
||
| 596 | // * @param bool $returnBinary Whether to tell the bridge to return binary data rather than JSON. |
||
| 597 | // */ |
||
| 598 | // public function setReturnBinary($returnBinary) |
||
| 599 | // { |
||
| 600 | // $this->returnBinary = $returnBinary; |
||
| 601 | // } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @return string The current language restrict value. |
||
| 605 | */ |
||
| 606 | public function getLanguage() |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @param string $language The value for language restrict. |
||
| 613 | */ |
||
| 614 | public function setLanguage($language) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @return string The current biasing profile name. |
||
| 621 | */ |
||
| 622 | public function getBiasingProfile() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @param string $biasingProfile Override the biasing profile used for this query. |
||
| 629 | */ |
||
| 630 | public function setBiasingProfile($biasingProfile) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @return int The current page size. |
||
| 637 | */ |
||
| 638 | public function getPageSize() |
||
| 642 | |||
| 643 | /** |
||
| 644 | * @param int $pageSize The number of records to return with the query. |
||
| 645 | */ |
||
| 646 | public function setPageSize($pageSize) |
||
| 650 | |||
| 651 | /** |
||
| 652 | * @return boolean |
||
| 653 | */ |
||
| 654 | public function isDisableAutocorrection() |
||
| 658 | |||
| 659 | /** |
||
| 660 | * @param boolean $disableAutocorrection Specifies whether the auto-correction behavior should be disabled. |
||
| 661 | * By default, when no results are returned for the given query (and there is |
||
| 662 | * a did-you-mean available), the first did-you-mean is automatically queried |
||
| 663 | * instead. |
||
| 664 | */ |
||
| 665 | public function setDisableAutocorrection($disableAutocorrection) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @return boolean |
||
| 672 | */ |
||
| 673 | public function isWildcardSearchEnabled() |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @param boolean $wildcardSearchEnabled Indicate if the *(star) character in the search string should be treated |
||
| 680 | * as a wildcard prefix search. For example, `sta*` will match `star` and |
||
| 681 | * `start`. |
||
| 682 | */ |
||
| 683 | public function setWildcardSearchEnabled($wildcardSearchEnabled) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * <b>Warning</b> This will count as two queries against your search index. |
||
| 690 | * |
||
| 691 | * Typically, this feature is used when you have a large number of navigation items that will overwhelm the end |
||
| 692 | * user. It works by using one of the existing navigation items to decide what the query is about and fires a second |
||
| 693 | * query to restrict the navigation to the most relevant set of navigation items for this search term. |
||
| 694 | * |
||
| 695 | * For example, if you pass in a search of `paper` and a restrict navigation of `category:2` |
||
| 696 | * |
||
| 697 | * The bridge will find the category navigation refinements in the first query and fire a second query for the top 2 |
||
| 698 | * most populous categories. Therefore, a search for something generic like "paper" will bring back top category |
||
| 699 | * matches like copy paper (1,030), paper pads (567). The bridge will fire off the second query with the search |
||
| 700 | * term, plus an OR refinement with the most likely categories. The navigation items in the first query are |
||
| 701 | * entirely replaced with the navigation items in the second query, except for the navigation that was used for the |
||
| 702 | * restriction so that users still have the ability to navigate by all category types. |
||
| 703 | * |
||
| 704 | * @param RestrictNavigation $restrictNavigation Restriction criteria |
||
| 705 | */ |
||
| 706 | public function setRestrictNavigation($restrictNavigation) |
||
| 710 | |||
| 711 | /** @return RestrictNavigation */ |
||
| 712 | public function getRestrictNavigation() |
||
| 716 | |||
| 717 | /** |
||
| 718 | * @return MBiasing |
||
| 719 | */ |
||
| 720 | public function getBiasing() |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Add a biasing profile, which is defined at query time. |
||
| 727 | * |
||
| 728 | * @param MBiasing $biasing |
||
| 729 | */ |
||
| 730 | public function setBiasing($biasing) |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @param string[] $bringToTop |
||
| 737 | */ |
||
| 738 | public function setBringToTop($bringToTop) { |
||
| 741 | |||
| 742 | /** |
||
| 743 | * @return string A string representation of all of the currently set refinements. |
||
| 744 | */ |
||
| 745 | View Code Duplication | public function getRefinementString() |
|
| 760 | |||
| 761 | /** |
||
| 762 | * @return string A string representation of all of the currently set custom url parameters. |
||
| 763 | */ |
||
| 764 | View Code Duplication | public function getCustomUrlParamsString() |
|
| 777 | |||
| 778 | /** |
||
| 779 | * @param MSort $sort |
||
| 780 | * |
||
| 781 | * @return RSort |
||
| 782 | */ |
||
| 783 | protected static function convertSort($sort) |
||
| 801 | |||
| 802 | /** |
||
| 803 | * @param MMatchStrategy $strategy |
||
| 804 | * |
||
| 805 | * @return RMatchStrategy |
||
| 806 | */ |
||
| 807 | protected static function convertPartialMatchStrategy($strategy) |
||
| 824 | |||
| 825 | /** |
||
| 826 | * @param MPartialMatchRule $rule |
||
| 827 | * |
||
| 828 | * @return RPartialMatchRule |
||
| 829 | */ |
||
| 830 | protected static function convertPartialMatchRule($rule) |
||
| 843 | |||
| 844 | /** |
||
| 845 | * @param MBiasing $biasing |
||
| 846 | * |
||
| 847 | * @return Biasing |
||
| 848 | */ |
||
| 849 | protected static function convertBiasing($biasing) |
||
| 859 | |||
| 860 | } |
||
| 861 |